diff --git a/.vscode/launch.json b/.vscode/launch.json index 6aba5cf..b8b4ada 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,15 @@ "request": "launch", "mode": "debug", "program": "${workspaceFolder}/cli", - "args": ["replace", "--config", "../example-files/launchExamples/config.json", "--output", "../example-files/launchExamples/output.txt", "--input", "../example-files/launchExamples/input1.txt", "-t", "../example-files/launchExamples/trace.html"], + "args": ["replace", "--config", "../example-files/launchExamples/config.json", "--output", "../example-files/launchExamples/output.txt", "--input", "../example-files/launchExamples/input1.txt", "-t", "../example-files/launchExamples/trace.md"], + }, + { + "name": "read key", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "${workspaceFolder}/cli", + "args": ["read", "key", "--config", "../example-files/launchExamples/config.json", "-t", "../example-files/launchExamples/trace.md", "-o","../example-files/launchExamples/output.yaml", "globals.dbConfigs"], }, ] } \ No newline at end of file diff --git a/cli/actions/key.go b/cli/actions/key.go new file mode 100644 index 0000000..73864f4 --- /dev/null +++ b/cli/actions/key.go @@ -0,0 +1,64 @@ +package actions + +import ( + "errors" + "fmt" + + "github.com/nqd/flat" +) + +func ReadKeyCmd(configFilePath string, key string, outputFilePath string, traceOutFilePath string) (string, error) { + configurationMap, err := ReadConfigurationMap(configFilePath) + + if err != nil { + return "", err + } + + memoryMap, traces, err := ReadMemoryMap(configurationMap) + + if err != nil { + return "", err + } + + if traceOutFilePath != "" { + traceOutputType := findFileType(traceOutFilePath) + flatMemoryMap, err := flat.Flatten(memoryMap, nil) + + if err != nil { + fmt.Println(err) + return "", err + } + table, err := traceToTableForSingleKey(traces, configurationMap.Configs, traceOutputType, key, flatMemoryMap[key]) + + if err != nil { + fmt.Println(err) + return "", err + } + + WriteFile(traceOutFilePath, table) + } + + flatMemoryMap, err := flat.Flatten(memoryMap, nil) + + if err != nil { + return "", err + } + + if outputFilePath == "" { + return flatMemoryMap[key].(string), nil + } else { + outputFileType := findFileType(outputFilePath) + + switch outputFileType { + case "json": + jsonString := "{\"" + key + "\":" + flatMemoryMap[key].(string) + "}" + err = WriteFile(outputFilePath, jsonString) + case "yaml": + yamlString := key + ": " + flatMemoryMap[key].(string) + err = WriteFile(outputFilePath, yamlString) + default: + return "", errors.New("output file type not supported. (json/yaml)") + } + } + return flatMemoryMap[key].(string), nil +} diff --git a/cli/actions/trace.go b/cli/actions/trace.go index dc304f2..94381c8 100644 --- a/cli/actions/trace.go +++ b/cli/actions/trace.go @@ -36,6 +36,49 @@ func TracesToString(traces *[]Trace) string { return result } +func traceToTableForSingleKey(traces *[]Trace, configs []Config, fileType string, key string, finalValue interface{}) (string, error) { + + columns := []string{} + t := table.NewWriter() + // t.SetOutputMirror(os.Stdout) + header := table.Row{""} + for order, config := range configs { + header = append(header, fmt.Sprintf("%s-%d", config.Path, order)) + columns = append(columns, fmt.Sprintf("%s-%d", config.Path, order)) + } + + header = append(header, "PreTemplate") + header = append(header, "PostTemplate") + columns = append(columns, "PreTemplate") + columns = append(columns, "PostTemplate") + + t.AppendHeader(header) + + row := buildRowInfo(traces, columns, key, finalValue) + buildTableBody(Table{Rows: map[string]Row{key: row}}, columns, t, []string{key}) + + t.SetStyle(table.Style{ + Name: "trace", + Color: table.ColorOptions{ + Row: text.Colors{text.BgHiCyan, text.BgBlack}, + RowAlternate: text.Colors{text.BgCyan, text.FgBlack}}, + }) + + switch fileType { + case "md": + return t.RenderMarkdown(), nil + case "html": + return t.RenderHTML(), nil + case "csv": + return t.RenderCSV(), nil + case "txt": + return t.Render(), nil + default: + return t.Render(), errors.New("file type not supported for trace output (supported types: .md, .html, .csv, .txt)") + } + +} + func traceToTable(traces *[]Trace, configs []Config, fileType string, memoryMap map[string]interface{}) (string, error) { columns := []string{} diff --git a/cli/cmd/key.go b/cli/cmd/key.go new file mode 100644 index 0000000..ac53253 --- /dev/null +++ b/cli/cmd/key.go @@ -0,0 +1,52 @@ +/* +Copyright © 2024 NAME HERE +*/ +package cmd + +import ( + "fmt" + + "github.com/mtintes/configamajig/actions" + "github.com/spf13/cobra" +) + +// keyCmd represents the key command +var keyCmd = &cobra.Command{ + Use: "key", + Short: "Find the value of a single key", + Long: `This is for finding the value of a single key`, + Run: func(cmd *cobra.Command, args []string) { + + key := "" + if args[0] == "" { + fmt.Println("Please include the key to be read") + return + } else { + fmt.Println("Key to be read: ", args[0]) + key = args[0] + } + + configFilePath := cmd.Flag("config").Value.String() + outputFilePath := cmd.Flag("output").Value.String() + traceOutFilePath := cmd.Flag("memoryTraceOut").Value.String() + + keyValue, err := actions.ReadKeyCmd(configFilePath, key, outputFilePath, traceOutFilePath) + + if err != nil { + fmt.Println(err) + return + } + + fmt.Println("Key value: ", keyValue) + }, +} + +func init() { + readCmd.AddCommand(keyCmd) + + keyCmd.Flags().StringP("config", "c", "", "Config file defines the mapping of the variables (order, depth, etc.)") + keyCmd.Flags().StringP("output", "o", "", "Optional: output file to be written. (yaml/json) Default is stdout") + keyCmd.Flags().StringP("memoryTraceOut", "t", "", "Changes made during memory map setup") + + keyCmd.MarkFlagRequired("config") +} diff --git a/cli/cmd/read.go b/cli/cmd/read.go new file mode 100644 index 0000000..8bb4693 --- /dev/null +++ b/cli/cmd/read.go @@ -0,0 +1,22 @@ +/* +Copyright © 2024 NAME HERE +*/ +package cmd + +import ( + "github.com/spf13/cobra" +) + +// readCmd represents the read command +var readCmd = &cobra.Command{ + Use: "read", + Short: "", + Long: ``, + Run: func(cmd *cobra.Command, args []string) { + }, +} + +func init() { + rootCmd.AddCommand(readCmd) + +} diff --git a/cli/cmd/replace.go b/cli/cmd/replace.go index 3af5150..4f0097a 100644 --- a/cli/cmd/replace.go +++ b/cli/cmd/replace.go @@ -11,13 +11,8 @@ import ( // replaceCmd represents the replace command var replaceCmd = &cobra.Command{ Use: "replace", - Short: "A brief description of your command", - Long: `A longer description that spans multiple lines and likely contains examples -and usage of using your command. For example: - -Cobra is a CLI library for Go that empowers applications. -This application is a tool to generate the needed files -to quickly create a Cobra application.`, + Short: "Replaces the variables in the input file using a config file", + Long: `This is for replacing the variables in the input file using a config file.`, Run: func(cmd *cobra.Command, args []string) { configMap := cmd.Flag("config").Value.String()