Skip to content

Commit

Permalink
Extracting utils related functions, coderefactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco committed Aug 16, 2023
1 parent 4e04ebb commit 5a0227c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 46 deletions.
47 changes: 2 additions & 45 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
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" 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 {
Expand Down
10 changes: 10 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 5a0227c

Please sign in to comment.