Skip to content

Commit

Permalink
adressing the code repetetion issues
Browse files Browse the repository at this point in the history
  • Loading branch information
thekingn committed May 27, 2024
1 parent 66236db commit f77c1c5
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 328 deletions.
14 changes: 14 additions & 0 deletions cmdgen/cmdgen.cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build cli

package cmdgen

import (
"github.com/HexmosTech/gabs/v2"
"github.com/HexmosTech/lama2/lama2cmd"
)

func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, string) {
httpv, url, jsonObj, headers, multipartBool, formBool := ConstructCommandHelper(parsedInput)
res, stdinBody := assembleCmdString(httpv, url, jsonObj, headers, multipartBool, formBool, o)
return res, stdinBody
}
12 changes: 3 additions & 9 deletions cmdgen/cmdgen.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cli

// Package `cmdgen` provides an API to generate
// API request commands (by default based on HTTPie)
// based on the parsed API file contents and the `l2`
Expand All @@ -14,7 +12,6 @@ import (

"github.com/HexmosTech/gabs/v2"
"github.com/HexmosTech/lama2/lama2cmd"
"github.com/rs/zerolog/log"
)

func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, headers *gabs.Container, multipart bool, form bool, o *lama2cmd.Opts) ([]string, string) {
Expand Down Expand Up @@ -90,20 +87,17 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header
// API file inputs, figures out the type of target command
// and finally generates a string representing the generated
// command
func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, string) {
log.Info().Str("ParsedInput", parsedInput.String()).Msg("")
func ConstructCommandHelper(parsedInput *gabs.Container) (string, string, *gabs.Container, *gabs.Container, bool, bool) {
httpv := parsedInput.S("verb", "value")
url := parsedInput.S("url", "value")
jsonObj := parsedInput.S("details", "ip_data")
headers := parsedInput.S("details", "headers")
multipart := parsedInput.S("multipart", "value")
form := parsedInput.S("form", "value")
multipartBool := false
if multipart != nil {
multipartBool = true
}
form := parsedInput.S("form", "value")
formBool := form != nil

res, stdinBody := assembleCmdString(httpv.Data().(string), url.Data().(string), jsonObj, headers, multipartBool, formBool, o)
return res, stdinBody
return httpv.Data().(string), url.Data().(string), jsonObj, headers, multipartBool, formBool
}
82 changes: 2 additions & 80 deletions cmdgen/cmdgen.wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,11 @@
package cmdgen

import (
"bytes"
"encoding/json"
"fmt"
"strings"

"github.com/HexmosTech/gabs/v2"
)

func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, headers *gabs.Container, multipart bool, form bool) ([]string, string) {
command := make([]string, 0)
var files *gabs.Container
if multipart {
if jsonObj.ExistsP("@files") {
files = jsonObj.S("@files")
jsonObj.Delete("@files")
}
}

jsonStr := ""
if jsonObj != nil && !multipart && !form {
dst := &bytes.Buffer{}
if err := json.Compact(dst, []byte(jsonObj.String())); err != nil {
}
jsonStr = dst.String()
}

command = append(command, "ht ")
if multipart || form {
command = append(command, "--ignore-stdin", "--form")
}

command = append(command, httpv+" ")
command = append(command, url+" ")

if multipart {
for key, val := range jsonObj.Data().(*gabs.Container).ChildrenMap() {
command = append(command, "'"+key+"'='"+val.Data().(string)+"' ")
}
for key, val := range files.ChildrenMap() {
command = append(command, key+"@"+val.Data().(string))
}
}

if form {
for key, val := range jsonObj.Data().(*gabs.Container).ChildrenMap() {
keyValuePair := fmt.Sprintf("'%s'='%s' ", key, val.Data().(string))
command = append(command, keyValuePair)
}
}

if headers != nil {
for key, val := range headers.Data().(*gabs.Container).ChildrenMap() {
command = append(command, key+":"+val.Data().(*gabs.Container).Data().(string))
}
}
cleanCommand := make([]string, 0)
for _, c := range command {
cleanC := strings.TrimSpace(c)
cleanCommand = append(cleanCommand, cleanC)
}
if multipart || form {
return cleanCommand, ""
}
return cleanCommand, jsonStr
}

// func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, headers *gabs.Container, multipart bool, form bool, o *lama2cmd.Opts) ([]string, string) {
// return assembleCmdStringHelper(httpv, url, jsonObj, headers, multipart, form, o.Nocolor)
// }

func ConstructCommand(parsedInput *gabs.Container) ([]string, string) {
httpv := parsedInput.S("verb", "value")
url := parsedInput.S("url", "value")
jsonObj := parsedInput.S("details", "ip_data")
headers := parsedInput.S("details", "headers")
multipart := parsedInput.S("multipart", "value")
multipartBool := false
if multipart != nil {
multipartBool = true
}
form := parsedInput.S("form", "value")
formBool := form != nil

res, stdinBody := assembleCmdString(httpv.Data().(string), url.Data().(string), jsonObj, headers, multipartBool, formBool)
httpv, url, jsonObj, headers, multipartBool, formBool := ConstructCommandHelper(parsedInput)
res, stdinBody := assembleCmdString(httpv, url, jsonObj, headers, multipartBool, formBool, nil)
return res, stdinBody
}
13 changes: 0 additions & 13 deletions codegen/codegen.cli.go

This file was deleted.

140 changes: 0 additions & 140 deletions codegen/codegen.wasm.go

This file was deleted.

75 changes: 37 additions & 38 deletions controller/controller.cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ import (

"github.com/HexmosTech/gabs/v2"
"github.com/HexmosTech/httpie-go"
"github.com/HexmosTech/lama2/cmdexec"
"github.com/HexmosTech/lama2/cmdgen"
"github.com/HexmosTech/lama2/codegen"
"github.com/HexmosTech/lama2/lama2cmd"
outputmanager "github.com/HexmosTech/lama2/outputManager"
"github.com/HexmosTech/lama2/parser"
"github.com/HexmosTech/lama2/preprocess"
"github.com/HexmosTech/lama2/prettify"
Expand All @@ -24,48 +21,50 @@ import (
"github.com/rs/zerolog/log"
)


func ExecuteProcessorBlock(block *gabs.Container, vm *goja.Runtime) {
b := block.S("value").Data().(*gabs.Container)
log.Debug().Str("Processor block incoming block", block.String()).Msg("")
script := b.Data().(string)
cmdexec.RunVMCode(script, vm)
// b := block.S("value").Data().(*gabs.Container)
// log.Debug().Str("Processor block incoming block", block.String()).Msg("")
// script := b.Data().(string)
// cmdexec.RunVMCode(script, vm)
ExecuteProcessorBlockHelper(block, vm...)
}

func ExecuteRequestorBlock(block *gabs.Container, vm *goja.Runtime, opts *lama2cmd.Opts, dir string) httpie.ExResponse {
preprocess.ProcessVarsInBlock(block, vm)
// TODO - replace stuff in headers, and varjson and json as well
cmd, stdinBody := cmdgen.ConstructCommand(block, opts)
log.Debug().Str("Stdin Body to be passed into httpie", stdinBody).Msg("")
resp, e1 := cmdexec.ExecCommand(cmd, stdinBody, dir)
log.Debug().Str("Response from ExecCommand", resp.Body).Msg("")
if e1 == nil {
chainCode := cmdexec.GenerateChainCode(resp.Body)
cmdexec.RunVMCode(chainCode, vm)
} else {
log.Fatal().Str("Error from ExecCommand", e1.Error())
os.Exit(1)
}
return resp
// preprocess.ProcessVarsInBlock(block, vm)
// // TODO - replace stuff in headers, and varjson and json as well
// cmd, stdinBody := cmdgen.ConstructCommand(block, opts)
// log.Debug().Str("Stdin Body to be passed into httpie", stdinBody).Msg("")
// resp, e1 := cmdexec.ExecCommand(cmd, stdinBody, dir)
// log.Debug().Str("Response from ExecCommand", resp.Body).Msg("")
// if e1 == nil {
// chainCode := cmdexec.GenerateChainCode(resp.Body)
// cmdexec.RunVMCode(chainCode, vm)
// } else {
// log.Fatal().Str("Error from ExecCommand", e1.Error())
// os.Exit(1)
// }
// return resp
return ExecuteRequestorBlockHelper(block, vm, opts, dir)
}

func HandleParsedFile(parsedAPI *gabs.Container, o *lama2cmd.Opts, dir string) {
parsedAPIblocks := GetParsedAPIBlocks(parsedAPI)
vm := cmdexec.GetJSVm()
var resp httpie.ExResponse
for i, block := range parsedAPIblocks {
log.Debug().Int("Block num", i).Msg("")
log.Debug().Str("Block getting processed", block.String()).Msg("")
blockType := block.S("type").Data().(string)
if blockType == "processor" {
ExecuteProcessorBlock(block, vm)
} else if blockType == "Lama2File" {
resp = ExecuteRequestorBlock(block, vm, o, dir)
}
}
if o.Output != "" {
outputmanager.WriteJSONOutput(resp, o.Output)
}
// parsedAPIblocks := GetParsedAPIBlocks(parsedAPI)
// vm := cmdexec.GetJSVm()
// var resp httpie.ExResponse
// for i, block := range parsedAPIblocks {
// log.Debug().Int("Block num", i).Msg("")
// log.Debug().Str("Block getting processed", block.String()).Msg("")
// blockType := block.S("type").Data().(string)
// if blockType == "processor" {
// ExecuteProcessorBlock(block, vm)
// } else if blockType == "Lama2File" {
// resp = ExecuteRequestorBlock(block, vm, o, dir)
// }
// }
// if o.Output != "" {
// outputmanager.WriteJSONOutput(resp, o.Output)
// }
return HandleParsedFileHelper(parsedAPI, O, dir)
}

// Process initiates the following tasks in the given order:
Expand Down
Loading

0 comments on commit f77c1c5

Please sign in to comment.