Skip to content

Commit

Permalink
adds code no tests (for shame) (#10)
Browse files Browse the repository at this point in the history
* adds code no tests (for shame)

* clean up descriptions
  • Loading branch information
mtintes authored Sep 18, 2024
1 parent 524df5f commit 0c3c67d
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 8 deletions.
10 changes: 9 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
]
}
64 changes: 64 additions & 0 deletions cli/actions/key.go
Original file line number Diff line number Diff line change
@@ -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
}
43 changes: 43 additions & 0 deletions cli/actions/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
52 changes: 52 additions & 0 deletions cli/cmd/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
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")
}
22 changes: 22 additions & 0 deletions cli/cmd/read.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
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)

}
9 changes: 2 additions & 7 deletions cli/cmd/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 0c3c67d

Please sign in to comment.