-
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 9 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
package contoller | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
|
@@ -19,6 +20,7 @@ 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" | ||
) | ||
|
@@ -87,16 +89,7 @@ func Process(version string) { | |
oldDir, _ := os.Getwd() | ||
utils.ChangeWorkingDir(dir) | ||
|
||
if o.Env { | ||
jsonEnvs, err := preprocess.GetL2EnvVariables(dir) | ||
if err != nil { | ||
log.Error().Str("Type", "Preprocess").Msg(err.Error()) | ||
return | ||
} | ||
// Frontend can read the stdout for this command and get the JSON of all the env's | ||
fmt.Println(string(jsonEnvs)) | ||
return | ||
} | ||
processEnvironmentVariables(o, dir) | ||
|
||
preprocess.LoadEnvironments(dir) | ||
utils.ChangeWorkingDir(oldDir) | ||
|
@@ -122,3 +115,44 @@ 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='' | ||
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 order of these checks must be simplified:
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. Also if it is a Essentially 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. Got it |
||
marshalAndPrintJSON(envMap) | ||
} else if o.Env != "UNSET" { // -e=any non-empty string | ||
relevantEnvs := getRelevantEnvs(envMap, o) | ||
marshalAndPrintJSON(relevantEnvs) | ||
} | ||
} | ||
|
||
func marshalAndPrintJSON(data interface{}) { | ||
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. Should go into utils, not clutter controller. 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. Yes |
||
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{} { | ||
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. All these env related functionalities should goto its own file. 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. Got it |
||
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.
Leave a comment highlighting what sort of error can be expected here after verification. I think it could only be a JSON marshalling error. Everything else returns an empty map.
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.
Yes