Skip to content

Commit

Permalink
Env suggesting with more revelance
Browse files Browse the repository at this point in the history
Adding go-autocomplete-trie
  • Loading branch information
lovestaco committed Aug 12, 2023
1 parent 87bbcc7 commit d94e88b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
30 changes: 26 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,34 @@ func Process(version string) {
oldDir, _ := os.Getwd()
utils.ChangeWorkingDir(dir)

if o.Env {
jsonEnvs, err := preprocess.GetL2EnvVariables(dir)
if len(o.Env) > 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))

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

filteredJSON, err := json.MarshalIndent(filteredEnvs, "", " ")
if err != nil {
log.Error().Str("Type", "Preprocess").Msg(fmt.Sprintf("Failed to marshal filtered env's to JSON: %v", err))
return
}

fmt.Println(string(filteredJSON))
return
}

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/HexmosTech/gabs/v2 v2.6.5
github.com/HexmosTech/godotenv v0.0.0-20230327154318-72e1f3d32ed3
github.com/HexmosTech/httpie-go v1.0.8
github.com/Vivino/go-autocomplete-trie v0.0.0-20230301121706-da951497d081
github.com/atotto/clipboard v0.1.4
github.com/dop251/goja v0.0.0-20230216180835-5937a312edda
github.com/dop251/goja_nodejs v0.0.0-20230207183254-2229640ea097
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ github.com/HexmosTech/httpie-go v1.0.8/go.mod h1:U2lyp7l8mHMJUBR3DAsa79A2eY1b/Ft
github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow=
github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4=
github.com/Vivino/go-autocomplete-trie v0.0.0-20230301121706-da951497d081 h1:CgfvELsx826xvyUI3te9pUu6K/9xc7INVm6QwdbbH6A=
github.com/Vivino/go-autocomplete-trie v0.0.0-20230301121706-da951497d081/go.mod h1:cknpiHPHiypnmvUq1EAV3M0SQQeVY2rPjGt32hNCEDs=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/coreos/go-systemd/v22 v22.3.3-0.20220203105225-a9a7ef127534/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.4.1-0.20201116162257-a2a8dda75c91/go.mod h1:2pZnwuY/m+8K6iRw6wQdMtk+rH5tNGR1i55kozfMjCc=
github.com/dlclark/regexp2 v1.7.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
Expand Down Expand Up @@ -87,6 +90,7 @@ github.com/pborman/getopt v1.1.0 h1:eJ3aFZroQqq0bWmraivjQNt6Dmm5M0h2JcDW38/Azb0=
github.com/pborman/getopt v1.1.0/go.mod h1:FxXoW1Re00sQG/+KIkuSqRL/LwQgSkv7uyac+STFsbk=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
Expand All @@ -97,6 +101,8 @@ github.com/rs/zerolog v1.29.0 h1:Zes4hju04hjbvkVkOhdl2HpZa+0PmVwigmo8XoORE5w=
github.com/rs/zerolog v1.29.0/go.mod h1:NILgTygv/Uej1ra5XxGf82ZFSLk58MFGAUS2o6usyD0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/vbauerster/mpb/v5 v5.4.0 h1:n8JPunifvQvh6P1D1HAl2Ur9YcmKT1tpoUuiea5mlmg=
github.com/vbauerster/mpb/v5 v5.4.0/go.mod h1:fi4wVo7BVQ22QcvFObm+VwliQXlV1eBT8JDaKXR4JGI=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down Expand Up @@ -154,6 +160,7 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
Expand Down Expand Up @@ -185,3 +192,5 @@ gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 2 additions & 2 deletions lama2cmd/lama2cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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 All @@ -51,7 +51,7 @@ func getParsedInput(argList []string) (Opts, []string) {
log.Fatal().
Str("Type", "Preprocess").
Strs("arglist", argList).
Msg(fmt.Sprint("Couldn't parse argument list"))
Msg("Couldn't parse argument list")
}

switch len(o.Verbose) {
Expand Down
8 changes: 2 additions & 6 deletions preprocess/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func combineEnvMaps(envMaps ...map[string]map[string]interface{}) map[string]map
return finalEnvMap
}

func GetL2EnvVariables(dir string) ([]byte, error) {
func GetL2EnvVariables(dir string) (map[string]map[string]interface{}, error) {
l2ConfigEnvMap := make(map[string]map[string]interface{})

l2ConfigPath, err := SearchL2ConfigEnv(dir)
Expand All @@ -191,11 +191,7 @@ func GetL2EnvVariables(dir string) ([]byte, error) {

finalEnvMap := combineEnvMaps(l2ConfigEnvMap, l2EnvMap)

jsonEnvs, err := json.MarshalIndent(finalEnvMap, "", " ")
if err != nil {
return nil, fmt.Errorf("Failed to marshal map env's to JSON: %v", err)
}
return jsonEnvs, nil
return finalEnvMap, nil
}

func GetLamaFileAsString(path string) string {
Expand Down

0 comments on commit d94e88b

Please sign in to comment.