From 5a0227cdabb1bb9e713ec7c48dde429f80e06675 Mon Sep 17 00:00:00 2001 From: lovestaco Date: Wed, 16 Aug 2023 10:05:42 +0530 Subject: [PATCH] Extracting utils related functions, coderefactor --- controller/controller.go | 47 ++-------------------------------------- lama2cmd/lama2cmd.go | 2 +- utils/utils.go | 10 +++++++++ 3 files changed, 13 insertions(+), 46 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index bca08f7e..d215fdc1 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -5,12 +5,11 @@ package contoller import ( - "encoding/json" - "fmt" "os" "github.com/HexmosTech/gabs/v2" "github.com/HexmosTech/httpie-go" + env "github.com/HexmosTech/lama2/Env" "github.com/HexmosTech/lama2/cmdexec" "github.com/HexmosTech/lama2/cmdgen" "github.com/HexmosTech/lama2/codegen" @@ -20,7 +19,6 @@ import ( "github.com/HexmosTech/lama2/preprocess" "github.com/HexmosTech/lama2/prettify" "github.com/HexmosTech/lama2/utils" - trie "github.com/Vivino/go-autocomplete-trie" "github.com/dop251/goja" "github.com/rs/zerolog/log" ) @@ -89,7 +87,7 @@ func Process(version string) { oldDir, _ := os.Getwd() utils.ChangeWorkingDir(dir) - processEnvironmentVariables(o, dir) + env.ProcessEnvironmentVariables(o, dir) preprocess.LoadEnvironments(dir) utils.ChangeWorkingDir(oldDir) @@ -115,44 +113,3 @@ func Process(version string) { log.Debug().Str("Parsed API", parsedAPI.String()).Msg("") HandleParsedFile(parsedAPI, o, dir) } - -func processEnvironmentVariables(o *lama2cmd.Opts, directory string) { - envMap, err := preprocess.GetL2EnvVariables(directory) - if err != nil { - log.Error().Str("Type", "Preprocess").Msg(err.Error()) - os.Exit(0) - } - if o.Env == "" { // -e='' - marshalAndPrintJSON(envMap) - } else if o.Env != "UNSET" { // -e=any non-empty string - relevantEnvs := getRelevantEnvs(envMap, o) - marshalAndPrintJSON(relevantEnvs) - } -} - -func marshalAndPrintJSON(data interface{}) { - filteredJSON, err := json.MarshalIndent(data, "", " ") - if err != nil { - log.Error().Str("Type", "Preprocess").Msg(fmt.Sprintf("Failed to marshal JSON: %v", err)) - os.Exit(0) - } - fmt.Println(string(filteredJSON)) - os.Exit(0) -} - -func getRelevantEnvs(envMap map[string]map[string]interface{}, o *lama2cmd.Opts) map[string]interface{} { - envTrie := trie.New() - for key := range envMap { - envTrie.Insert(key) - } - - searchQuery := o.Env - suggestions := envTrie.SearchAll(searchQuery) - filteredEnvs := make(map[string]interface{}) - for _, suggestion := range suggestions { - if env, found := envMap[suggestion]; found { - filteredEnvs[suggestion] = env - } - } - return filteredEnvs -} diff --git a/lama2cmd/lama2cmd.go b/lama2cmd/lama2cmd.go index aa94eb11..a1e4cafe 100644 --- a/lama2cmd/lama2cmd.go +++ b/lama2cmd/lama2cmd.go @@ -26,7 +26,7 @@ type Opts struct { PostmanFile string `short:"p" long:"postmanfile" description:"JSON export from Postman (Settings -> Data -> Export Data)"` LamaDir string `short:"l" long:"lama2dir" description:"Output directory to put .l2 files after conversion from Postman format"` Help bool `short:"h" long:"help" group:"AddHelp" description:"Usage help for Lama2"` - Env string `short:"e" long:"env" default:"UNSET" description:"Get a JSON of environment variables revelant to input arg"` + Env string `short:"e" long:"env" default:"UNSET_VU5TRVQ" description:"Get a JSON of environment variables revelant to input arg"` Version bool `long:"version" description:"Print Lama2 binary version"` Positional struct { diff --git a/utils/utils.go b/utils/utils.go index 36c412ac..1704eab9 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -138,3 +138,13 @@ func UpdateSelf() { cmd.Stderr = os.Stderr _ = cmd.Run() } + +func MarshalAndPrintJSON(data interface{}) { + filteredJSON, err := json.MarshalIndent(data, "", " ") + if err != nil { + log.Error().Str("Type", "Preprocess").Msg(fmt.Sprintf("Failed to marshal JSON: %v", err)) + os.Exit(0) + } + fmt.Println(string(filteredJSON)) + os.Exit(0) +}