Skip to content

Commit

Permalink
Merge branch 'Wasm' of github.com:HexmosTech/Lama2 into Wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
thekingn committed May 25, 2024
2 parents a48a9cf + 7eb34be commit 66236db
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 237 deletions.
111 changes: 111 additions & 0 deletions controller/controller.cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//go:build cli

// Package controller coordinates all the other
// components in the `Lama2` project. The high
// level overview of command execution is easily
// understood from this package
package contoller

import (
"os"

"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"
"github.com/HexmosTech/lama2/utils"
"github.com/dop251/goja"
"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)
}

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
}

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

// 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 & retrieve results
// 7. Optionally, post-process and write results to a JSON file
func Process(version string) {
o := lama2cmd.GetAndValidateCmd(os.Args)
lama2cmd.ArgParsing(o, version)

apiContent := preprocess.GetLamaFileAsString(o.Positional.LamaAPIFile)
_, dir, _ := utils.GetFilePathComponents(o.Positional.LamaAPIFile)
oldDir, _ := os.Getwd()
utils.ChangeWorkingDir(dir)

preprocess.LoadEnvironments(dir)
utils.ChangeWorkingDir(oldDir)
p := parser.NewLama2Parser()
parsedAPI, e := p.Parse(apiContent)
if o.Convert != "" {
codegen.GenerateTargetCode(o.Convert, parsedAPI)
return
}

if o.Prettify {
prettify.Prettify(parsedAPI, p.Context, p.MarkRange, apiContent, o.Positional.LamaAPIFile)
return
}

if e != nil {
log.Fatal().
Str("Type", "Controller").
Str("LamaFile", o.Positional.LamaAPIFile).
Str("Error", e.Error()).
Msg("Parse Error")
}
log.Debug().Str("Parsed API", parsedAPI.String()).Msg("")
HandleParsedFile(parsedAPI, o, dir)
}
100 changes: 0 additions & 100 deletions controller/controller.go
Original file line number Diff line number Diff line change
@@ -1,114 +1,14 @@
//go:build cli

// Package controller coordinates all the other
// components in the `Lama2` project. The high
// level overview of command execution is easily
// understood from this package
package contoller

import (
"os"

"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"
"github.com/HexmosTech/lama2/utils"
"github.com/dop251/goja"
"github.com/rs/zerolog/log"
)

func GetParsedAPIBlocks(parsedAPI *gabs.Container) []*gabs.Container {
return parsedAPI.S("value").Data().(*gabs.Container).Children()
}

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

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
}

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

// 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 & retrieve results
// 7. Optionally, post-process and write results to a JSON file
func Process(version string) {
o := lama2cmd.GetAndValidateCmd(os.Args)
lama2cmd.ArgParsing(o, version)

apiContent := preprocess.GetLamaFileAsString(o.Positional.LamaAPIFile)
_, dir, _ := utils.GetFilePathComponents(o.Positional.LamaAPIFile)
oldDir, _ := os.Getwd()
utils.ChangeWorkingDir(dir)

preprocess.LoadEnvironments(dir)
utils.ChangeWorkingDir(oldDir)
p := parser.NewLama2Parser()
parsedAPI, e := p.Parse(apiContent)
if o.Convert != "" {
codegen.GenerateTargetCode(o.Convert, parsedAPI)
return
}

if o.Prettify {
prettify.Prettify(parsedAPI, p.Context, p.MarkRange, apiContent, o.Positional.LamaAPIFile)
return
}

if e != nil {
log.Fatal().
Str("Type", "Controller").
Str("LamaFile", o.Positional.LamaAPIFile).
Str("Error", e.Error()).
Msg("Parse Error")
}
log.Debug().Str("Parsed API", parsedAPI.String()).Msg("")
HandleParsedFile(parsedAPI, o, dir)
}
4 changes: 0 additions & 4 deletions controller/controller.wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (
"syscall/js"
)

func GetParsedAPIBlocks(parsedAPI *gabs.Container) []*gabs.Container {
return parsedAPI.S("value").Data().(*gabs.Container).Children()
}

func ExecuteProcessorBlock(block *gabs.Container) {
b := block.S("value").Data().(*gabs.Container)
script := b.Data().(string)
Expand Down
1 change: 0 additions & 1 deletion lama2cmd/lama2cmd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//go:build cli

// Package `lama2cmd` provides CLI argument parsing facilities.
// It hosts the `Opts` structure to record user intentions
Expand Down
89 changes: 0 additions & 89 deletions lama2cmd/lama2cmd.wasm.go

This file was deleted.

2 changes: 1 addition & 1 deletion outputManager/output_manager.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build cli


// Package `outputmanager` provides facilities for controlling
// the logging library as well as capabilities to post-process
Expand Down
42 changes: 0 additions & 42 deletions outputManager/output_manager.wasm.go

This file was deleted.

0 comments on commit 66236db

Please sign in to comment.