Skip to content

Commit

Permalink
Extracting common functionalities
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco committed Aug 12, 2023
1 parent 768eb9f commit a137b34
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,34 +89,25 @@ func Process(version string) {
oldDir, _ := os.Getwd()
utils.ChangeWorkingDir(dir)

if len(o.Env) > 0 {
if (o.Env) == "" {
envMap, err := preprocess.GetL2EnvVariables(dir)
if err != nil {
log.Error().Str("Type", "Preprocess").Msg(err.Error())
return
}
marshalAndPrintJSON(envMap)
return
}

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
}
}

filteredJSON, err := json.MarshalIndent(filteredEnvs, "", " ")
if len(o.Env) > 0 {
envMap, err := preprocess.GetL2EnvVariables(dir)
if err != nil {
log.Error().Str("Type", "Preprocess").Msg(fmt.Sprintf("Failed to marshal filtered env's to JSON: %v", err))
log.Error().Str("Type", "Preprocess").Msg(err.Error())
return
}

fmt.Println(string(filteredJSON))
filteredEnvs := getRelevantEnvs(envMap, o)
marshalAndPrintJSON(filteredEnvs)
return
}

Expand Down Expand Up @@ -144,3 +135,29 @@ func Process(version string) {
log.Debug().Str("Parsed API", parsedAPI.String()).Msg("")
HandleParsedFile(parsedAPI, o, dir)
}

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
}
fmt.Println(string(filteredJSON))
}

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
}

0 comments on commit a137b34

Please sign in to comment.