Skip to content

Commit

Permalink
fixed some code duplccation remaining
Browse files Browse the repository at this point in the history
  • Loading branch information
thekingn committed Jun 15, 2024
1 parent 8cd876f commit 8619733
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 76 deletions.
36 changes: 4 additions & 32 deletions controller/controller.cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ 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"
Expand All @@ -29,9 +28,9 @@ func ExecuteProcessorBlock(block *gabs.Container, vm *goja.Runtime) httpie.ExRes
return ExecuteProcessorBlockHelper(block, vm)
}

func ExecuteRequestorBlock(block *gabs.Container, vm *goja.Runtime, opts *lama2cmd.Opts, dir string) httpie.ExResponse {
return ExecuteRequestorBlockHelper(block, vm, opts, dir)
}
// func ExecuteRequestorBlock(block *gabs.Container, vm *goja.Runtime, opts *lama2cmd.Opts, dir string) httpie.ExResponse {
// return ExecuteRequestorBlockHelper(block, vm, opts, dir)
// }

func HandleParsedFile(parsedAPI *gabs.Container, o *lama2cmd.Opts, dir string) (httpie.ExResponse, *lama2cmd.Opts) {
vm := cmdexec.GetJSVm()
Expand Down Expand Up @@ -83,34 +82,7 @@ func Process(version string) {
}
}

func ExecuteRequestorBlockHelper(block *gabs.Container, args ...interface{}) httpie.ExResponse {
var vm *goja.Runtime
var opts *lama2cmd.Opts
var dir string
var cmd []string
var stdinBody string
if len(args) > 0 {
for _, arg := range args {
switch v := arg.(type) {
case *goja.Runtime:
vm = v
case *lama2cmd.Opts:
opts = v
case string:
dir = v
}
}
}
preprocess.ProcessVarsInBlock(block, vm)
cmd, stdinBody = cmdgen.ConstructCommand(block, opts)
var resp httpie.ExResponse
var e1 error
resp, e1 = cmdexec.ExecCommand(cmd, stdinBody, dir)
headers := resp.Headers
var headersString string
for key, value := range headers {
headersString += fmt.Sprintf("%s: %s\n", key, value)
}
func ExecuteRequestorBlockHelper(resp httpie.ExResponse, headersString string, e1 error, vm *goja.Runtime) httpie.ExResponse {
if e1 == nil {
chainCode := cmdexec.GenerateChainCode(resp.Body)
cmdexec.RunVMCode(chainCode, vm)
Expand Down
15 changes: 14 additions & 1 deletion controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
package contoller

import (
"fmt"
"log"

"github.com/HexmosTech/gabs/v2"
"github.com/HexmosTech/httpie-go"
"github.com/HexmosTech/lama2/cmdexec"
"github.com/HexmosTech/lama2/cmdgen"
"github.com/HexmosTech/lama2/lama2cmd"
"github.com/HexmosTech/lama2/preprocess"
"github.com/dop251/goja"
)

Expand All @@ -37,8 +40,18 @@ func extractArgs(args []interface{}) (*goja.Runtime, *lama2cmd.Opts, string) {
}

func processLama2FileBlock(block *gabs.Container, vm *goja.Runtime, o *lama2cmd.Opts, dir string) httpie.ExResponse {
preprocess.ProcessVarsInBlock(block, vm)
cmd, stdinBody := cmdgen.ConstructCommand(block, o)
var resp httpie.ExResponse
resp = ExecuteRequestorBlock(block, vm, o, dir)
var e1 error
resp, e1 = cmdexec.ExecCommand(cmd, stdinBody, dir)
headers := resp.Headers
var headersString string
for key, value := range headers {
headersString += fmt.Sprintf("%s: %s\n", key, value)
}

resp = ExecuteRequestorBlockHelper(resp, headersString, e1, vm)
return resp
}

Expand Down
45 changes: 2 additions & 43 deletions controller/controller.wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import (
"github.com/dop251/goja"

"github.com/HexmosTech/lama2/cmdexec"
"github.com/HexmosTech/lama2/cmdgen"
"github.com/HexmosTech/lama2/lama2cmd"
"github.com/HexmosTech/lama2/parser"
"github.com/HexmosTech/lama2/preprocess"
)

func ExecuteProcessorBlock(block *gabs.Container, vm *goja.Runtime) httpie.ExResponse {
Expand All @@ -27,10 +25,6 @@ func ExecuteJsCodeWasm(script string) {
js.Global().Call("eval", script)
}

func ExecuteRequestorBlock(block *gabs.Container, vm *goja.Runtime, opts *lama2cmd.Opts, dir string) httpie.ExResponse {
return ExecuteRequestorBlockHelper(block)
}

func HandleParsedFile(parsedAPI *gabs.Container) (httpie.ExResponse, *lama2cmd.Opts) {
return HandleParsedFileHelper(parsedAPI)
}
Expand All @@ -48,56 +42,21 @@ func ProcessWasmInput(data string) (httpie.ExResponse, *lama2cmd.Opts) {
return HandleParsedFile(parsedAPI)
}

func ExecuteRequestorBlockHelper(block *gabs.Container, args ...interface{}) httpie.ExResponse {
var vm *goja.Runtime
var opts *lama2cmd.Opts
var dir string

if len(args) > 0 {
for _, arg := range args {
switch v := arg.(type) {
case *goja.Runtime:
vm = v
case *lama2cmd.Opts:
opts = v
case string:
dir = v
}
}
}
preprocess.ProcessVarsInBlock(block, vm)
var cmd []string
var stdinBody string
cmd, stdinBody = cmdgen.ConstructCommand(block, opts)
var resp httpie.ExResponse
var e1 error
resp, e1 = cmdexec.ExecCommand(cmd, stdinBody, dir)
headers := resp.Headers
var headersString string
for key, value := range headers {
headersString += fmt.Sprintf("%s: %s\n", key, value)
}

func ExecuteRequestorBlockHelper(resp httpie.ExResponse, headersString string, e1 error, vm *goja.Runtime) httpie.ExResponse {
targetHeader := "text/html"
isTextHTMLPresent := strings.Contains(headersString, targetHeader)

if isTextHTMLPresent {
fmt.Printf("'%s' is present in the headers.\n", targetHeader)
return resp
} else {
fmt.Printf("'%s' is not present in the headers.\n", targetHeader)
if e1 == nil {
chainCode := cmdexec.GenerateChainCode(resp.Body)
if vm != nil {
cmdexec.RunVMCode(chainCode, vm)
} else {
ExecuteJsCodeWasm(chainCode)
}
ExecuteJsCodeWasm(chainCode)
} else {
fmt.Println("Error from ExecCommand", e1)
os.Exit(1)
}
}

return resp
}

0 comments on commit 8619733

Please sign in to comment.