Skip to content

Commit

Permalink
Different files for wasm and cli build
Browse files Browse the repository at this point in the history
  • Loading branch information
thekingn committed May 18, 2024
1 parent 8d25a15 commit 2f68122
Show file tree
Hide file tree
Showing 24 changed files with 1,763 additions and 405 deletions.
34 changes: 12 additions & 22 deletions cmdexec/cmdexec.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build cli

// Package `cmdexec` provides a facility to execute
// l2 commands, stream output to stdout, while also
// providing ability to retrieve the command output as
Expand All @@ -6,15 +8,12 @@ package cmdexec

import (
"errors"
"fmt"

// "os"
"os"
"strings"

"github.com/HexmosTech/httpie-go"
// "github.com/HexmosTech/lama2/utils"
// "github.com/rs/zerolog/log"
// "github.com/HexmosTech/httpie-go"
"github.com/HexmosTech/lama2/utils"
"github.com/rs/zerolog/log"
)

// ExecCommand changes directory to the given `apiDir`
Expand All @@ -23,24 +22,15 @@ import (
// to stdout.
// Once execution finishes, previous CWD is restored,
// and the command output is returned as a string
func ExecCommand(cmdSlice []string, stdinBody string) (httpie.ExResponse, error) {
fmt.Println("Inside cmd exec code")
fmt.Println("Inside cmd exec code httpie Integer", httpie.Int(24601))
proxyURL := "https://proxyserver.hexmos.com/"
proxyUserName := "proxyServer"
proxyUserPassword := "proxy22523146server"
allowRedirects := true
resp, err := httpie.Lama2Entry(cmdSlice, strings.NewReader(stdinBody), proxyURL, proxyUserName, proxyUserPassword, allowRedirects)
// resp, err := httpie.Lama2Entry(cmdSlice, strings.NewReader(stdinBody), "", "", "", false)
fmt.Println("error:", err)
fmt.Println("Data Response:", resp)
func ExecCommand(cmdSlice []string, stdinBody string, apiDir string) (httpie.ExResponse, error) {
oldDir, _ := os.Getwd()
utils.ChangeWorkingDir(apiDir)
resp, err := httpie.Lama2Entry(cmdSlice, strings.NewReader(stdinBody))
if err != nil {
// log.Fatal().Str("Error from the API executor", err.Error()).Msg("")
fmt.Println("Got error while executing", err)
log.Fatal().Str("Error from the API executor", err.Error()).Msg("")
return httpie.ExResponse{}, errors.New("Error from API executor: " + err.Error())
}
// log.Debug().Str("Response body from API executor", resp.Body).Msg("")
// utils.ChangeWorkingDir(oldDir)
// fmt.Println("Reponse fro httpie", resp)
log.Debug().Str("Response body from API executor", resp.Body).Msg("")
utils.ChangeWorkingDir(oldDir)
return resp, nil
}
48 changes: 48 additions & 0 deletions cmdexec/cmdexec.wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//go:build wasm

// Package `cmdexec` provides a facility to execute
// l2 commands, stream output to stdout, while also
// providing ability to retrieve the command output as
// a string.
package cmdexec

import (
"errors"
"fmt"

// "os"
"strings"

"github.com/HexmosTech/httpie-go"
// "github.com/HexmosTech/lama2/utils"
// "github.com/rs/zerolog/log"
// "github.com/HexmosTech/httpie-go"
)

// ExecCommand changes directory to the given `apiDir`
// and then executes the command specified in `cmdStr`
// During command execution, ExecCommand streams output
// to stdout.
// Once execution finishes, previous CWD is restored,
// and the command output is returned as a string
func ExecCommand(cmdSlice []string, stdinBody string) (httpie.ExResponse, error) {
fmt.Println("Inside cmd exec code")
fmt.Println("Inside cmd exec code httpie Integer", httpie.Int(24601))
proxyURL := "https://proxyserver.hexmos.com/"
proxyUserName := "proxyServer"
proxyUserPassword := "proxy22523146server"
allowRedirects := true
resp, err := httpie.Lama2Entry(cmdSlice, strings.NewReader(stdinBody), proxyURL, proxyUserName, proxyUserPassword, allowRedirects)
// resp, err := httpie.Lama2Entry(cmdSlice, strings.NewReader(stdinBody), "", "", "", false)
fmt.Println("error:", err)
fmt.Println("Data Response:", resp)
if err != nil {
// log.Fatal().Str("Error from the API executor", err.Error()).Msg("")
fmt.Println("Got error while executing", err)
return httpie.ExResponse{}, errors.New("Error from API executor: " + err.Error())
}
// log.Debug().Str("Response body from API executor", resp.Body).Msg("")
// utils.ChangeWorkingDir(oldDir)
// fmt.Println("Reponse fro httpie", resp)
return resp, nil
}
37 changes: 18 additions & 19 deletions cmdexec/js.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
//go:build cli

package cmdexec

import (
// "github.com/rs/zerolog/log"
"fmt"
"syscall/js"
"github.com/dop251/goja"
"github.com/dop251/goja_nodejs/console"
"github.com/dop251/goja_nodejs/require"
"github.com/rs/zerolog/log"
)

// GetJSVm creates a new goja runtime instance
// with console.log enabled

// func GetJSVm() *goja.Runtime {
// vm := goja.New()
// new(require.Registry).Enable(vm)
// console.Enable(vm)
// return vm
// }
func GetJSVm() *goja.Runtime {
vm := goja.New()
new(require.Registry).Enable(vm)
console.Enable(vm)
return vm
}

// RunVMCode takes in a JS snippet as a string,
// executes the code in a JS VM, finally checks
Expand All @@ -23,13 +25,11 @@ import (
// Note: the vm runtime remains modified; so if
// you reuse the vm for other operations, the state
// from previous invocations carry over
func RunVMCode(jsCode string) {
// log.Info().Str("Evaluated through syscall js:", jsCode).Msg("")
js.Global().Call("eval", jsCode)
// _, err := vm.RunString(jsCode)
// if ex, ok := err.(*goja.Exception); ok {
// log.Debug().Str("Error executing JS processor block", ex.String()).Msg("")
// }
func RunVMCode(jsCode string, vm *goja.Runtime) {
_, err := vm.RunString(jsCode)
if ex, ok := err.(*goja.Exception); ok {
log.Fatal().Str("Error executing JS processor block", ex.String()).Msg("")
}
}

// GenerateChainCode takes in an HTTP response body
Expand All @@ -47,7 +47,6 @@ func GenerateChainCode(httpRespBody string) string {
console.log(e)
console.log("Stored as string")
}`
// log.Info().Str("Chain code generated", code).Msg("")
fmt.Println("code:", code)
log.Debug().Str("Chain code generated", code).Msg("")
return code
}
55 changes: 55 additions & 0 deletions cmdexec/js.wasm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//go:build wasm

package cmdexec

import (
// "github.com/rs/zerolog/log"
"fmt"
"syscall/js"
)

// GetJSVm creates a new goja runtime instance
// with console.log enabled

// func GetJSVm() *goja.Runtime {
// vm := goja.New()
// new(require.Registry).Enable(vm)
// console.Enable(vm)
// return vm
// }

// RunVMCode takes in a JS snippet as a string,
// executes the code in a JS VM, finally checks
// whether there are any errors, and if yes,
// logs the problem.
// Note: the vm runtime remains modified; so if
// you reuse the vm for other operations, the state
// from previous invocations carry over
func RunVMCode(jsCode string) {
// log.Info().Str("Evaluated through syscall js:", jsCode).Msg("")
js.Global().Call("eval", jsCode)
// _, err := vm.RunString(jsCode)
// if ex, ok := err.(*goja.Exception); ok {
// log.Debug().Str("Error executing JS processor block", ex.String()).Msg("")
// }
}

// GenerateChainCode takes in an HTTP response body
// and comes up with some JS code to define the
// "magic variable" result. What does the code do?
// The result is stored as a JS object, if the input
// value can be parsed as JSON. Otherwise the value is
// stored as a simple string.
func GenerateChainCode(httpRespBody string) string {
code := `try {
result = JSON.parse(String.raw` + "`" + httpRespBody + "`" + `)
console.log("Stored as JSON")
} catch (e) {
result = String.raw` + "`" + httpRespBody + "`" + `
console.log(e)
console.log("Stored as string")
}`
// log.Info().Str("Chain code generated", code).Msg("")
fmt.Println("code:", code)
return code
}
55 changes: 29 additions & 26 deletions cmdgen/cmdgen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//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 @@ -11,41 +13,41 @@ import (
"strings"

"github.com/HexmosTech/gabs/v2"
// "github.com/HexmosTech/lama2/lama2cmd"
// "github.com/rs/zerolog/log"
"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) ([]string, string) {
func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, headers *gabs.Container, multipart bool, form bool, o *lama2cmd.Opts) ([]string, string) {
command := make([]string, 0)
// log.Info().
// Str("Type", "Construct Command").
// Str("httpv", httpv).
// Str("url", url).
// Bool("multipart", multipart).
// Bool("form", form).
// Msg(fmt.Sprint("Construct parameters"))
log.Info().
Str("Type", "Construct Command").
Str("httpv", httpv).
Str("url", url).
Bool("multipart", multipart).
Bool("form", form).
Msg(fmt.Sprint("Construct parameters"))

// log.Debug().
// Str("JSONObj", jsonObj.String()).
// Str("Headers", headers.String()).Msg("")
log.Debug().
Str("JSONObj", jsonObj.String()).
Str("Headers", headers.String()).Msg("")

var files *gabs.Container
if multipart {
if jsonObj.ExistsP("@files") {
files = jsonObj.S("@files")
// log.Debug().Str("Files", files.String()).Msg("")
log.Debug().Str("Files", files.String()).Msg("")
jsonObj.Delete("@files")
// log.Trace().Str("Shortened JsonObj", jsonObj.String()).Msg("")
log.Trace().Str("Shortened JsonObj", jsonObj.String()).Msg("")
}
}

jsonStr := ""
if jsonObj != nil && !multipart && !form {
dst := &bytes.Buffer{}
if err := json.Compact(dst, []byte(jsonObj.String())); err != nil {
// log.Fatal().
// Str("Error", err.Error()).
// Msg("Couldn't minify JSON")
log.Fatal().
Str("Error", err.Error()).
Msg("Couldn't minify JSON")
}
jsonStr = dst.String()
}
Expand All @@ -58,9 +60,9 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header
}*/

command = append(command, "ht ")
// if o.Nocolor {
// command = append(command, "--pretty=none ")
// }
if o.Nocolor {
command = append(command, "--pretty=none ")
}
if multipart || form {
command = append(command, "--ignore-stdin", "--form")
}
Expand All @@ -70,7 +72,8 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header

if multipart {
for key, val := range jsonObj.Data().(*gabs.Container).ChildrenMap() {
command = append(command, "'"+key+"'='"+val.Data().(string)+"' ")
keyValuePair := fmt.Sprintf("%s=%s", key, val.Data().(string))
command = append(command, keyValuePair)
}
for key, val := range files.ChildrenMap() {
command = append(command, key+"@"+val.Data().(string))
Expand All @@ -79,7 +82,7 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header

if form {
for key, val := range jsonObj.Data().(*gabs.Container).ChildrenMap() {
keyValuePair := fmt.Sprintf("'%s'='%s' ", key, val.Data().(string))
keyValuePair := fmt.Sprintf("%s=%s", key, val.Data().(string))
command = append(command, keyValuePair)
}
}
Expand All @@ -104,8 +107,8 @@ 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) ([]string, string) {
// log.Info().Str("ParsedInput", parsedInput.String()).Msg("")
func ConstructCommand(parsedInput *gabs.Container, o *lama2cmd.Opts) ([]string, string) {
log.Info().Str("ParsedInput", parsedInput.String()).Msg("")
httpv := parsedInput.S("verb", "value")
url := parsedInput.S("url", "value")
jsonObj := parsedInput.S("details", "ip_data")
Expand All @@ -118,6 +121,6 @@ func ConstructCommand(parsedInput *gabs.Container) ([]string, string) {
form := parsedInput.S("form", "value")
formBool := form != nil

res, stdinBody := assembleCmdString(httpv.Data().(string), url.Data().(string), jsonObj, headers, multipartBool, formBool)
res, stdinBody := assembleCmdString(httpv.Data().(string), url.Data().(string), jsonObj, headers, multipartBool, formBool, o)
return res, stdinBody
}
Loading

0 comments on commit 2f68122

Please sign in to comment.