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
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 43 additions & 4 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package contoller

import (
"encoding/json"
"fmt"
"os"

Expand All @@ -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"
)
Expand Down Expand Up @@ -87,14 +89,25 @@ func Process(version string) {
oldDir, _ := os.Getwd()
utils.ChangeWorkingDir(dir)

if o.Env {
jsonEnvs, err := preprocess.GetL2EnvVariables(dir)
if (o.Env) == "" && len(o.Output) == 0 {
envMap, 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))
marshalAndPrintJSON(envMap)
return
}

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()

if err != nil {
log.Error().Str("Type", "Preprocess").Msg(err.Error())
return
}

filteredEnvs := getRelevantEnvs(envMap, o)
marshalAndPrintJSON(filteredEnvs)
return
}

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

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

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

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
}
10 changes: 5 additions & 5 deletions docs/Lama2/docs/reference/controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Package controller coordinates all the other components in the \`Lama2\` project


<a name="ExecuteProcessorBlock"></a>
## func [ExecuteProcessorBlock](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L30>)
## func [ExecuteProcessorBlock](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L32>)

```go
func ExecuteProcessorBlock(block *gabs.Container, vm *goja.Runtime)
Expand All @@ -27,7 +27,7 @@ func ExecuteProcessorBlock(block *gabs.Container, vm *goja.Runtime)


<a name="ExecuteRequestorBlock"></a>
## func [ExecuteRequestorBlock](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L37>)
## func [ExecuteRequestorBlock](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L39>)

```go
func ExecuteRequestorBlock(block *gabs.Container, vm *goja.Runtime, opts *lama2cmd.Opts, dir string) httpie.ExResponse
Expand All @@ -36,7 +36,7 @@ func ExecuteRequestorBlock(block *gabs.Container, vm *goja.Runtime, opts *lama2c


<a name="GetParsedAPIBlocks"></a>
## func [GetParsedAPIBlocks](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L26>)
## func [GetParsedAPIBlocks](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L28>)

```go
func GetParsedAPIBlocks(parsedAPI *gabs.Container) []*gabs.Container
Expand All @@ -45,7 +45,7 @@ func GetParsedAPIBlocks(parsedAPI *gabs.Container) []*gabs.Container


<a name="HandleParsedFile"></a>
## func [HandleParsedFile](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L54>)
## func [HandleParsedFile](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L56>)

```go
func HandleParsedFile(parsedAPI *gabs.Container, o *lama2cmd.Opts, dir string)
Expand All @@ -54,7 +54,7 @@ func HandleParsedFile(parsedAPI *gabs.Container, o *lama2cmd.Opts, dir string)


<a name="Process"></a>
## func [Process](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L81>)
## func [Process](<https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L83>)

```go
func Process(version string)
Expand Down
2 changes: 1 addition & 1 deletion docs/Lama2/docs/reference/lama2cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Opts struct {
PostmanFile string `short:"p" long:"postmanfile" description:"JSON export from Postman (Settings -> Data -> Export Data)"`
LamaDir string `short:"l" long:"lama2dir" description:"Output directory to put .l2 files after conversion from Postman format"`
Help bool `short:"h" long:"help" group:"AddHelp" description:"Usage help for Lama2"`
Env bool `short:"e" long:"env" description:"Get a JSON of environment variables"`
Env string `short:"e" long:"env" description:"Get a JSON of environment variables revelant to input arg"`
Version bool `long:"version" description:"Print Lama2 binary version"`

Positional struct {
Expand Down
8 changes: 4 additions & 4 deletions docs/Lama2/docs/reference/preprocess.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Package preprocess provides facilities to expand environment variables in \`.l2\
- [func ExpandHeaders\(block \*gabs.Container, vm \*goja.Runtime\)](<#ExpandHeaders>)
- [func ExpandJSON\(block \*gabs.Container, vm \*goja.Runtime\)](<#ExpandJSON>)
- [func ExpandURL\(block \*gabs.Container, vm \*goja.Runtime\)](<#ExpandURL>)
- [func GetL2EnvVariables\(dir string\) \(\[\]byte, error\)](<#GetL2EnvVariables>)
- [func GetL2EnvVariables\(dir string\) \(map\[string\]map\[string\]interface\{\}, error\)](<#GetL2EnvVariables>)
- [func GetLamaFileAsString\(path string\) string](<#GetLamaFileAsString>)
- [func LamaFile\(inputFile string\) \(string, string\)](<#LamaFile>)
- [func LoadEnvFile\(l2path string\)](<#LoadEnvFile>)
Expand Down Expand Up @@ -73,13 +73,13 @@ func ExpandURL(block *gabs.Container, vm *goja.Runtime)
## func [GetL2EnvVariables](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L169>)

```go
func GetL2EnvVariables(dir string) ([]byte, error)
func GetL2EnvVariables(dir string) (map[string]map[string]interface{}, error)
```



<a name="GetLamaFileAsString"></a>
## func [GetLamaFileAsString](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L200>)
## func [GetLamaFileAsString](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L196>)

```go
func GetLamaFileAsString(path string) string
Expand All @@ -88,7 +88,7 @@ func GetLamaFileAsString(path string) string


<a name="LamaFile"></a>
## func [LamaFile](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L214>)
## func [LamaFile](<https://github.com/HexmosTech/Lama2/blob/main/preprocess/preprocess.go#L210>)

```go
func LamaFile(inputFile string) (string, string)
Expand Down
23 changes: 14 additions & 9 deletions docs/Lama2/docs/tutorials/editor.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
## Useful Options
## Useful Options

The `l2` command provides some helpful options for
extension developers. The options are:

1. `--env` or `-e` outputs a JSON of environment variables (in CLI);
1. `--env=<string to get relevant env>` or `-e` outputs a JSON of environment variables (in CLI);
2. `--nocolor` or `-n` disables colored output in httpie-go (in CLI);
3. `--output=<target.json` or `-o` writes a structured JSON
output to the target file (without colors). The following is the content
structure:
output to the target file (without colors). The following is the content
structure:

```json
{
Expand All @@ -20,34 +21,37 @@ structure:
- `headers`: A string consisting of `HTTP header: Value` pairs separated by newline.
- `body`: A string containing the HTTP response. Usually a JSON or HTML response.


!!! note

Right now, all the three values in the JSON are strings. In the future, we may transform the values further to provide a more parse-friendly structure.

## The Commands

### Execute current file

Combining the options `-n` and `-o`, we get:

```bash
l2 -n -o /tmp/lama2.json my_api.l2
```

The command mentioned above disables HTTPie colors,
writes the whole transaction to a structured JSON,
writes the whole transaction to a structured JSON,
while also printing details into `stdout`.

The extension author can simply read the file, and
display the contents to users appropriately. For an
example, see [Lama2 for VSCode](https://github.com/HexmosTech/Lama2Code)
(also see [Marketplace page](https://marketplace.visualstudio.com/items?itemName=hexmos.Lama2)).

### Providing environment variable autocompletion
### Providing environment variable autocompletion

To obtain a combined JSON representation of environment variables from `l2.env` and `l2config.env`, use option `-e` or `--env`. This will output the result to `stdout`.

```bash
l2 -e /path/to/my_api.l2
l2 -e='' /path/to/my_api.l2
```

```json
{
"AHOST": {
Expand All @@ -60,6 +64,7 @@ l2 -e /path/to/my_api.l2
}
}
```

The extension author can simply read the `stdout` after executing the command, and display the variables to users appropriately.

![l2envvariable variable](l2envvariable.png)
Expand All @@ -70,4 +75,4 @@ Go to [Example](../tutorials/examples.md#case-3-override-root-variable-with-loca

## Syntax Highlighting

The VSCode plugin implements a rudimentary syntax highlighting for `.l2` files. We use [Iro](https://eeyo.io/iro/documentation/) for syntax grammar generation. Find [more details](https://github.com/HexmosTech/Lama2/tree/main/syntax/README.md) if interested.
The VSCode plugin implements a rudimentary syntax highlighting for `.l2` files. We use [Iro](https://eeyo.io/iro/documentation/) for syntax grammar generation. Find [more details](https://github.com/HexmosTech/Lama2/tree/main/syntax/README.md) if interested.
23 changes: 11 additions & 12 deletions docs/Lama2/docs/tutorials/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

### One-line install/update in Linux/MacOS

To install/update *Lama2* and its dependencies automatically, run the following:
To install/update _Lama2_ and its dependencies automatically, run the following:

```
curl -s https://hexmos.com/lama2/install.sh | bash -s
```

### One-line install/update in Windows

To install/update *Lama2* and its dependencies automatically, run the following as *Administrator*:
To install/update _Lama2_ and its dependencies automatically, run the following as _Administrator_:

```
choco install lama2 --version=1.0.0 --force -y
Expand All @@ -21,8 +21,8 @@ choco install lama2 --version=1.0.0 --force -y
#### (Optional) Import your collections from Postman

Follow [guide](./postman.md)
to import your existing Postman collections into a Plain-Text
Lama2 repository.
to import your existing Postman collections into a Plain-Text
Lama2 repository.

#### Self update

Expand All @@ -36,19 +36,18 @@ l2 -u

Install the [VSCode extension](#from-vs-code) to launch requests from within your editor


### Manual install

#### Step 1: Install HTTPie

*Lama2* depends on [HTTPie for Terminal](https://httpie.io/cli). Use
_Lama2_ depends on [HTTPie for Terminal](https://httpie.io/cli). Use
their official instructions to get the `http` command functional in
your local system.
your local system.

#### Step 2: Download & install *Lama2* binary packages
#### Step 2: Download & install _Lama2_ binary packages

Head over to Lama2 [releases](https://github.com/HexmosTech/Lama2/releases). Check under
the *Assets* head to find various packages. Download the relevant package for your
the _Assets_ head to find various packages. Download the relevant package for your
operating system and CPU architecture. Once you have the package, run the following:

```
Expand Down Expand Up @@ -90,7 +89,7 @@ Application Options:
-o, --output= Path to output JSON file to store logs, headers and result
-v, --verbose Show verbose debug information
-n, --nocolor Disable color in httpie output
-e --env Get a JSON of environment variables
-e --env= Get a JSON of environment variables revelant to input arg
-h, --help Usage help for Lama2
--version Print Lama2 binary version

Expand All @@ -100,6 +99,6 @@ Help Options:

### From VS Code

Find *Lama2 for VSCode* at the [VSCode Marketplace](https://marketplace.visualstudio.com/items?itemName=hexmos.Lama2). The extension requires the *l2* command available (usually at `/usr/local/bin/l2` for Linux/MacOS and `C:\ProgramData\chocolatey\bin` for Windows).
Find _Lama2 for VSCode_ at the [VSCode Marketplace](https://marketplace.visualstudio.com/items?itemName=hexmos.Lama2). The extension requires the _l2_ command available (usually at `/usr/local/bin/l2` for Linux/MacOS and `C:\ProgramData\chocolatey\bin` for Windows).

Once the extension is installed, open the command palette (ctrl + shift + p) and search for `Execute current file` to execute the file
Once the extension is installed, open the command palette (ctrl + shift + p) and search for `Execute current file` to execute the file
10 changes: 5 additions & 5 deletions docs/Lama2/site/reference/controller.html
Original file line number Diff line number Diff line change
Expand Up @@ -745,23 +745,23 @@ <h2 id="index">Index<a class="headerlink" href="#index" title="Permanent link">&
<li><a href="#Process">func Process(version string)</a></li>
</ul>
<p><a name="ExecuteProcessorBlock"></a></p>
<h2 id="func-executeprocessorblock">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L30">ExecuteProcessorBlock</a><a class="headerlink" href="#func-executeprocessorblock" title="Permanent link">&para;</a></h2>
<h2 id="func-executeprocessorblock">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L32">ExecuteProcessorBlock</a><a class="headerlink" href="#func-executeprocessorblock" title="Permanent link">&para;</a></h2>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">ExecuteProcessorBlock</span><span class="p">(</span><span class="nx">block</span><span class="w"> </span><span class="o">*</span><span class="nx">gabs</span><span class="p">.</span><span class="nx">Container</span><span class="p">,</span><span class="w"> </span><span class="nx">vm</span><span class="w"> </span><span class="o">*</span><span class="nx">goja</span><span class="p">.</span><span class="nx">Runtime</span><span class="p">)</span>
</code></pre></div>
<p><a name="ExecuteRequestorBlock"></a></p>
<h2 id="func-executerequestorblock">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L37">ExecuteRequestorBlock</a><a class="headerlink" href="#func-executerequestorblock" title="Permanent link">&para;</a></h2>
<h2 id="func-executerequestorblock">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L39">ExecuteRequestorBlock</a><a class="headerlink" href="#func-executerequestorblock" title="Permanent link">&para;</a></h2>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">ExecuteRequestorBlock</span><span class="p">(</span><span class="nx">block</span><span class="w"> </span><span class="o">*</span><span class="nx">gabs</span><span class="p">.</span><span class="nx">Container</span><span class="p">,</span><span class="w"> </span><span class="nx">vm</span><span class="w"> </span><span class="o">*</span><span class="nx">goja</span><span class="p">.</span><span class="nx">Runtime</span><span class="p">,</span><span class="w"> </span><span class="nx">opts</span><span class="w"> </span><span class="o">*</span><span class="nx">lama2cmd</span><span class="p">.</span><span class="nx">Opts</span><span class="p">,</span><span class="w"> </span><span class="nx">dir</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span><span class="w"> </span><span class="nx">httpie</span><span class="p">.</span><span class="nx">ExResponse</span>
</code></pre></div>
<p><a name="GetParsedAPIBlocks"></a></p>
<h2 id="func-getparsedapiblocks">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L26">GetParsedAPIBlocks</a><a class="headerlink" href="#func-getparsedapiblocks" title="Permanent link">&para;</a></h2>
<h2 id="func-getparsedapiblocks">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L28">GetParsedAPIBlocks</a><a class="headerlink" href="#func-getparsedapiblocks" title="Permanent link">&para;</a></h2>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">GetParsedAPIBlocks</span><span class="p">(</span><span class="nx">parsedAPI</span><span class="w"> </span><span class="o">*</span><span class="nx">gabs</span><span class="p">.</span><span class="nx">Container</span><span class="p">)</span><span class="w"> </span><span class="p">[]</span><span class="o">*</span><span class="nx">gabs</span><span class="p">.</span><span class="nx">Container</span>
</code></pre></div>
<p><a name="HandleParsedFile"></a></p>
<h2 id="func-handleparsedfile">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L54">HandleParsedFile</a><a class="headerlink" href="#func-handleparsedfile" title="Permanent link">&para;</a></h2>
<h2 id="func-handleparsedfile">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L56">HandleParsedFile</a><a class="headerlink" href="#func-handleparsedfile" title="Permanent link">&para;</a></h2>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">HandleParsedFile</span><span class="p">(</span><span class="nx">parsedAPI</span><span class="w"> </span><span class="o">*</span><span class="nx">gabs</span><span class="p">.</span><span class="nx">Container</span><span class="p">,</span><span class="w"> </span><span class="nx">o</span><span class="w"> </span><span class="o">*</span><span class="nx">lama2cmd</span><span class="p">.</span><span class="nx">Opts</span><span class="p">,</span><span class="w"> </span><span class="nx">dir</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span>
</code></pre></div>
<p><a name="Process"></a></p>
<h2 id="func-process">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L81">Process</a><a class="headerlink" href="#func-process" title="Permanent link">&para;</a></h2>
<h2 id="func-process">func <a href="https://github.com/HexmosTech/Lama2/blob/main/controller/controller.go#L83">Process</a><a class="headerlink" href="#func-process" title="Permanent link">&para;</a></h2>
<div class="highlight"><pre><span></span><code><span class="kd">func</span><span class="w"> </span><span class="nx">Process</span><span class="p">(</span><span class="nx">version</span><span class="w"> </span><span class="kt">string</span><span class="p">)</span>
</code></pre></div>
<p>Process initiates the following tasks in the given order: 1. Parse command line arguments 2. Read API file contents 3. Expand environment variables in API file 4. Parse the API contents 5. Generate API request command 6. Execute command &amp; retrieve results 7. Optionally, post-process and write results to a JSON file</p>
Expand Down
Loading