-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds code no tests (for shame) (#10)
* adds code no tests (for shame) * clean up descriptions
- Loading branch information
Showing
6 changed files
with
192 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters