Skip to content
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

Merged
merged 14 commits into from
Aug 16, 2023
Merged

Origin/suggest relevant envs #32

merged 14 commits into from
Aug 16, 2023

Conversation

lovestaco
Copy link
Contributor

@lovestaco lovestaco commented Aug 13, 2023

What type of MR is this?

  • πŸ• Feature
  • πŸ› Bug Fix
  • πŸ“ Doc Update
  • 🎨 Style
  • πŸ§‘β€πŸ’» Code Refactor
  • πŸ”₯ Performance Improvements
  • βœ… Test
  • πŸ€– Build
  • πŸ” CI
  • ⏩ Revert

Description

  • This MR adds the feature of providing environment variables relevant to input.
  • To provide relevant variables making use of trie.

Issue

More autocomplete problem

Important files to start review from

controller.go

l2 --help

Usage:
  l2 [OPTIONS] [LamaAPIFile]

Application Options:
  -o, --output=      Path to output JSON file to store logs, headers and result
  -v, --verbose      Show verbose debug information
  -b, --prettify     Prettify specified .l2 file
  -c, --convert=     Generate code in given language and library (ex: python.requests); reference: tinyurl.com/l2codegen
  -n, --nocolor      Disable color in httpie output
  -u, --update       Update l2 binary to the latest released version (Linux/MacOS only)
  -p, --postmanfile= JSON export from Postman (Settings -> Data -> Export Data)
  -l, --lama2dir=    Output directory to put .l2 files after conversion from Postman format
  -h, --help         Usage help for Lama2
  -e, --env=         Get a JSON of environment variables revelant to input arg (default: UNSET)
      --version      Print Lama2 binary version

Help Options:
  -h, --help         Show this help message

Added tests?

  • πŸ‘ yes
  • πŸ™… no, because they aren't needed
  • πŸ™‹ no, because I need help

Added to documentation?

If documentation update is there then run make mkdocs once PR is in acceptable state.

  • πŸ““ make mkdocs
  • πŸ“œ README.md
  • πŸ™… no documentation needed

@lovestaco lovestaco self-assigned this Aug 13, 2023
@shrsv
Copy link
Contributor

shrsv commented Aug 14, 2023

Please mention relevant issues in description, so that they get tracked, and eventually closed.

}

if len(o.Env) > 0 {
envMap, err := preprocess.GetL2EnvVariables(dir)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get envMap is common to both the if statements (this and the previous one). So, we should get it outside/before both the if statement

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes extracted it out processEnvironmentVariables()

@@ -130,3 +162,15 @@ func checkBHost(t *testing.T, envMap map[string]EnvData) {
}
}
}

func checkBHostDoesNotExist(t *testing.T, envMap map[string]EnvData) {
if _, ok := envMap["BHOST"]; ok {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is BHOST / AHOST key ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is variables from env file

@shrsv
Copy link
Contributor

shrsv commented Aug 15, 2023

Let's set default value to UNSET_VU5TRVQ just to avoid conflicts with a genuine value such as UNSET

func processEnvironmentVariables(o *lama2cmd.Opts, directory string) {
envMap, err := preprocess.GetL2EnvVariables(directory)
if err != nil {
log.Error().Str("Type", "Preprocess").Msg(err.Error())
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

log.Error().Str("Type", "Preprocess").Msg(err.Error())
os.Exit(0)
}
if o.Env == "" { // -e=''
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of these checks must be simplified:

  1. Is it an -e invocation? ( != unset)
    1. is it empty? handle
    2. is it not empty? handle
  2. Not an -e continue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also if it is a -e request, for efficiency sake, once printing is done, we should simply exit the app, right? We are not doing that now, continuing processing.

Essentially -e is a editor extension support command; we don't expect a normal user to poke into it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

}
}

func marshalAndPrintJSON(data interface{}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should go into utils, not clutter controller.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

os.Exit(0)
}

func getRelevantEnvs(envMap map[string]map[string]interface{}, o *lama2cmd.Opts) map[string]interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All these env related functionalities should goto its own file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it

func runL2CommandAndGetOutput(t *testing.T, cmdArgs ...string) string {

// Get the full path to the l2 binary
l2BinPath := "../build/l2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How do we ensure check is happening on the latest binary? Any mechanism to avoid the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes every test will build a new binary before executing the tests
image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locally when we run make test it builds new binary
image

"github.com/rs/zerolog/log"
)

func runL2CommandAndGetOutput(t *testing.T, cmdArgs ...string) string {
Copy link
Contributor

@shrsv shrsv Aug 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is a utility method (looks like it), we should not be taking in the testing object as a parameter. It confuses the reader.

In functions, as a rule, strive for minimum number of params.

For sophistication, compose functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got it, I have a similar function in anohter file will add both in test utils file

Env/env.go Outdated
@@ -0,0 +1,48 @@
package env
Copy link
Contributor

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

Env/env.go Outdated
os.Exit(0)
}
// Check if it's an -e invocation
if o.Env != "UNSET_VU5TRVQ" {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned earlier, if it is o.Env we should stop processing, and exit the app.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?
MarshalAndPrintJSON()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.exit(0)

Copy link
Contributor

Choose a reason for hiding this comment

The 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:

MarshalAndPrintJSONAndExit :)

It is important to keep meaning and actions same.

Either name should be printExit or something like that, or you should move exit outside.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right :|
Ive added in the conditions

return nil
}

func getL2BinaryPath() (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getLocalL2BinaryPath to differentiate from system binary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right

Copy link
Contributor

@shrsv shrsv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🍾

@shrsv shrsv merged commit b526cf1 into main Aug 16, 2023
1 check passed
LinceMathew pushed a commit that referenced this pull request Dec 28, 2023
LinceMathew pushed a commit that referenced this pull request Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants