Skip to content

Commit

Permalink
Extracting env related functions, coderefactor
Browse files Browse the repository at this point in the history
  • Loading branch information
lovestaco committed Aug 16, 2023
1 parent da128e0 commit 4e04ebb
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package env

import (
"os"

"github.com/HexmosTech/lama2/lama2cmd"
"github.com/HexmosTech/lama2/preprocess"
"github.com/HexmosTech/lama2/utils"
trie "github.com/Vivino/go-autocomplete-trie"
"github.com/rs/zerolog/log"
)

func ProcessEnvironmentVariables(o *lama2cmd.Opts, directory string) {
envMap, err := preprocess.GetL2EnvVariables(directory)
if err != nil {
// Potential Errors:
// - JSON marshalling errors.
log.Error().Str("Type", "Preprocess").Msg(err.Error())
os.Exit(0)
}
// Check if it's an -e invocation
if o.Env != "UNSET_VU5TRVQ" {
if o.Env == "" { // Handle empty -e=''
utils.MarshalAndPrintJSON(envMap)
} else { // Handle non-empty -e
relevantEnvs := GetRelevantEnvs(envMap, o)
utils.MarshalAndPrintJSON(relevantEnvs)
}
}
// If not an -e invocation, the function just continues.
}

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 4e04ebb

Please sign in to comment.