From ccba62608524dc946dd94cc3d52aaaac69c4a797 Mon Sep 17 00:00:00 2001 From: lovestaco Date: Tue, 15 Aug 2023 15:30:10 +0530 Subject: [PATCH] Adding default value to Environment variable --- controller/controller.go | 39 +++++++++++++++++---------------------- lama2cmd/lama2cmd.go | 2 +- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index 94813a09..bca08f7e 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -89,27 +89,7 @@ func Process(version string) { oldDir, _ := os.Getwd() utils.ChangeWorkingDir(dir) - if (o.Env) == "" && len(o.Output) == 0 { - envMap, err := preprocess.GetL2EnvVariables(dir) - if err != nil { - log.Error().Str("Type", "Preprocess").Msg(err.Error()) - return - } - marshalAndPrintJSON(envMap) - return - } - - if len(o.Env) > 0 { - envMap, err := preprocess.GetL2EnvVariables(dir) - if err != nil { - log.Error().Str("Type", "Preprocess").Msg(err.Error()) - return - } - - filteredEnvs := getRelevantEnvs(envMap, o) - marshalAndPrintJSON(filteredEnvs) - return - } + processEnvironmentVariables(o, dir) preprocess.LoadEnvironments(dir) utils.ChangeWorkingDir(oldDir) @@ -136,13 +116,28 @@ func Process(version string) { 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)) - return + os.Exit(0) } fmt.Println(string(filteredJSON)) + os.Exit(0) } func getRelevantEnvs(envMap map[string]map[string]interface{}, o *lama2cmd.Opts) map[string]interface{} { diff --git a/lama2cmd/lama2cmd.go b/lama2cmd/lama2cmd.go index 1ddfa487..aa94eb11 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" description:"Get a JSON of environment variables revelant to input arg"` + Env string `short:"e" long:"env" default:"UNSET" description:"Get a JSON of environment variables revelant to input arg"` Version bool `long:"version" description:"Print Lama2 binary version"` Positional struct {