-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Origin/suggest relevant envs #32
Changes from 13 commits
d94e88b
768eb9f
a137b34
a11245a
5a0bd89
ca5fc33
ccba626
e1fab89
da128e0
4e04ebb
5a0227c
2b6e35e
c9a2bc1
6689711
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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" { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As mentioned earlier, if it is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this function im exiting, should I remove it from this and use the exit in the conditions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. os.exit(0) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The name and logic doesn't match. What you are actually doing is:
It is important to keep meaning and actions same. Either name should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right :| |
||
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 | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recommend changing the package name to something more non-generic (
env
is a common name, with meaning at systems level).Maybe call it:
l2env