Skip to content

Commit

Permalink
Adding default value to Environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco committed Aug 15, 2023
1 parent ca5fc33 commit ccba626
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
39 changes: 17 additions & 22 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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{} {
Expand Down
2 changes: 1 addition & 1 deletion lama2cmd/lama2cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ccba626

Please sign in to comment.