From e6bd9a34ab9fcd5bd2ddbc5ae35af4f98a034610 Mon Sep 17 00:00:00 2001 From: mtintes Date: Thu, 19 Sep 2024 21:07:43 -0500 Subject: [PATCH] makes this more package friendly and removes cruft --- cli/actions/key.go | 7 +---- cli/actions/replace.go | 8 +---- cli/actions/replace_test.go | 28 ++++++++--------- cli/cmd/key.go | 9 +++++- cli/cmd/replace.go | 16 +++++++--- example-files/config.json | 56 ---------------------------------- example-files/dev.json | 10 ------ example-files/global.json | 23 -------------- example-files/input1.txt | 9 ------ example-files/integration.json | 10 ------ example-files/local.json | 19 ------------ example-files/regional.json | 10 ------ 12 files changed, 34 insertions(+), 171 deletions(-) delete mode 100644 example-files/config.json delete mode 100644 example-files/dev.json delete mode 100644 example-files/global.json delete mode 100644 example-files/input1.txt delete mode 100644 example-files/integration.json delete mode 100644 example-files/local.json delete mode 100644 example-files/regional.json diff --git a/cli/actions/key.go b/cli/actions/key.go index 73864f4..d84eb92 100644 --- a/cli/actions/key.go +++ b/cli/actions/key.go @@ -7,12 +7,7 @@ import ( "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 - } +func ReadKeyCmd(configurationMap *ConfigurationMap, key string, outputFilePath string, traceOutFilePath string) (string, error) { memoryMap, traces, err := ReadMemoryMap(configurationMap) diff --git a/cli/actions/replace.go b/cli/actions/replace.go index c6324fb..7c1222a 100644 --- a/cli/actions/replace.go +++ b/cli/actions/replace.go @@ -6,13 +6,7 @@ import ( "github.com/nqd/flat" ) -func ReplaceCmd(configMap string, inputPath string, outputPath string, traceFileOutput string) { - configurationMap, err := ReadConfigurationMap(configMap) - - if err != nil { - fmt.Println(err) - return - } +func ReplaceCmd(configurationMap *ConfigurationMap, inputPath string, outputPath string, traceFileOutput string) { memoryMap, traces, err := ReadMemoryMap(configurationMap) diff --git a/cli/actions/replace_test.go b/cli/actions/replace_test.go index c2cc634..f2396de 100644 --- a/cli/actions/replace_test.go +++ b/cli/actions/replace_test.go @@ -16,30 +16,28 @@ func TestReplaceCmd(t *testing.T) { _ = afero.WriteFile(AppFs, "test.json", []byte(jsonData), 0644) _ = afero.WriteFile(AppFs, "test.yaml", []byte(yamlData), 0644) - ConfigurationMap := `{ - "version": "1.0.0", - "configs": [ + ConfigurationMap := &ConfigurationMap{ + Version: "1.0.0", + Configs: []Config{ { - "path": "test.json", - "mappings": [], - "applyFile": "after" + Path: "test.json", + Mappings: []Mapping{}, + ApplyFile: "after", }, { - "path": "test.yaml", - "mappings": [], - "applyFile": "after" - } - ] - }` - - _ = afero.WriteFile(AppFs, "config.json", []byte(ConfigurationMap), 0644) + Path: "test.yaml", + Mappings: []Mapping{}, + ApplyFile: "after", + }, + }, + } inputFile := "I am {{ .key }}" outputFile := "I am key" _ = afero.WriteFile(AppFs, "test.txt", []byte(inputFile), 0644) - ReplaceCmd("config.json", "test.txt", "output.txt", "") + ReplaceCmd(ConfigurationMap, "test.txt", "output.txt", "") file, _ := afero.ReadFile(AppFs, "output.txt") diff --git a/cli/cmd/key.go b/cli/cmd/key.go index ac53253..9d00ba7 100644 --- a/cli/cmd/key.go +++ b/cli/cmd/key.go @@ -30,7 +30,14 @@ var keyCmd = &cobra.Command{ outputFilePath := cmd.Flag("output").Value.String() traceOutFilePath := cmd.Flag("memoryTraceOut").Value.String() - keyValue, err := actions.ReadKeyCmd(configFilePath, key, outputFilePath, traceOutFilePath) + configurationMap, err := actions.ReadConfigurationMap(configFilePath) + + if err != nil { + fmt.Println(err) + return + } + + keyValue, err := actions.ReadKeyCmd(configurationMap, key, outputFilePath, traceOutFilePath) if err != nil { fmt.Println(err) diff --git a/cli/cmd/replace.go b/cli/cmd/replace.go index 4f0097a..0d31bb4 100644 --- a/cli/cmd/replace.go +++ b/cli/cmd/replace.go @@ -4,6 +4,8 @@ Copyright © 2024 NAME HERE package cmd import ( + "fmt" + "github.com/mtintes/configamajig/actions" "github.com/spf13/cobra" ) @@ -14,14 +16,18 @@ var replaceCmd = &cobra.Command{ 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() - + configMapPath := cmd.Flag("config").Value.String() traceFileOutput := cmd.Flag("memoryTraceOut").Value.String() - inputPath := cmd.Flag("input").Value.String() - outputPath := cmd.Flag("output").Value.String() - actions.ReplaceCmd(configMap, inputPath, outputPath, traceFileOutput) + + configurationMap, err := actions.ReadConfigurationMap(configMapPath) + + if err != nil { + fmt.Println(err) + return + } + actions.ReplaceCmd(configurationMap, inputPath, outputPath, traceFileOutput) }, } diff --git a/example-files/config.json b/example-files/config.json deleted file mode 100644 index 0f5d917..0000000 --- a/example-files/config.json +++ /dev/null @@ -1,56 +0,0 @@ -{ - "configs": [ - { - "path": "example-files/global.json", - "mappings": [ - { - "inPath": "globalAppConfigs", - "toPath": "globals" - }, - { - "inPath": "deploymentConfigs", - "toPath": "deploymentValues" - } - ], - "applyFile": "after" - }, - { - "path": "example-files/regional.json", - "mappings": [ - { - "inPath": "globalAppConfigs", - "toPath": "globals" - }, - { - "inPath": "deploymentConfigs", - "toPath": "deploymentValues" - } - ], - "applyFile": "later" - }, - { - "path": "example-files/integration.json", - "mappings": [ - { - "inPath": "deploymentConfigs", - "toPath": "deploymentValues" - } - ], - "applyFile": "before" - }, - { - "path": "example-files/local.json", - "mappings": [ - { - "inPath": "globalAppConfigs", - "toPath": "globals" - }, - { - "inPath": "deploymentConfigs", - "toPath": "deploymentValues" - } - ] - } - ], - "version": "1.0.0" -} \ No newline at end of file diff --git a/example-files/dev.json b/example-files/dev.json deleted file mode 100644 index cd3163e..0000000 --- a/example-files/dev.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "deployConfigs": { - "stage": "development" - }, - "appConfigs": { - "app1": { - "dbConfigs": "someApp1DevConnectionString" - } - } -} \ No newline at end of file diff --git a/example-files/global.json b/example-files/global.json deleted file mode 100644 index 66aa718..0000000 --- a/example-files/global.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "globalAppConfigs":{ - "dbConfigs": "someGlobalConnectionString", - "someAPIKey": "someAPIKey", - "someOtherGlobalConfigs": "someOtherGlobalConfigs" - }, - "deploymentConfigs":{ - "stage": "development", - "someOtherConfigs": "someOtherConfigs" - }, - "someOtherThings": { - "thing1": "{{ .globals.dbConfigs }}", - "thing2": "thing2" - }, - "deepValues":{ - "deepvalue1":{ - "thing": "thingglobal" - }, - "deepvalue2":{ - "thing": "thingglobal2" - } - } -} \ No newline at end of file diff --git a/example-files/input1.txt b/example-files/input1.txt deleted file mode 100644 index f815c22..0000000 --- a/example-files/input1.txt +++ /dev/null @@ -1,9 +0,0 @@ -I am a {{ .input1.file }} and I have some {{ .input1.text }}. -I am a file and I have some text. - -thinglocal: {{ .deepValues.deepvalue1.thing }} -thingglobal2: {{ .deepValues.deepvalue2.thing }} - -someOtherGlobalConfigs: {{ .globals.someOtherGlobalConfigs }} - -someLocalConnectionString: {{ .someOtherThings.thing1 }} \ No newline at end of file diff --git a/example-files/integration.json b/example-files/integration.json deleted file mode 100644 index e2a22de..0000000 --- a/example-files/integration.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "deploymentConfigs":{ - "stage": "integration" - }, - "appConfigs": { - "app1": { - "dbConfigs": "someApp1IntegrationConnectionString" - } - } -} \ No newline at end of file diff --git a/example-files/local.json b/example-files/local.json deleted file mode 100644 index 0552a77..0000000 --- a/example-files/local.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "globalAppConfigs":{ - "dbConfigs": "someLocalConnectionString", - "someAPIKey": "someAPIKey" - }, - "deploymentConfigs":{ - "stage": "production", - "someOtherConfigs": "someOtherConfigs" - }, - "input1": { - "file": "file", - "text": "text" - }, - "deepValues":{ - "deepvalue1":{ - "thing": "thinglocal" - } - } -} \ No newline at end of file diff --git a/example-files/regional.json b/example-files/regional.json deleted file mode 100644 index 7d90504..0000000 --- a/example-files/regional.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "globalAppConfigs":{ - "dbConfigs": "someRegionalConnectionString", - "someAPIKey": "someAPIKey" - }, - "deploymentConfigs":{ - "stage": "integration", - "someOtherConfigs": "someOtherConfigs" - } -} \ No newline at end of file