Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 107 additions & 8 deletions cmdgen/cmdgen.wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,125 @@
package cmdgen

import (
"log"
"syscall/js"

"github.com/HexmosTech/gabs/v2"
"github.com/HexmosTech/lama2/lama2cmd"
"syscall/js"
)


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, nil)

LaBearerAuthToken := js.Global().Get("LaBearerAuthToken").String()
authHeaderExists := false
for _, header := range res {
if len(header) >= len("Authorization:") && header[:len("Authorization:")] == "Authorization:" {
authHeaderExists = true
manisfestData := js.Global().Get("liveapiManifest").String()

// Parse JSON
parsedJson, err := gabs.ParseJSON([]byte(manisfestData))
if err != nil {
log.Fatal("Error parsing JSON:", err)
}

// Get project count
projects, ok := parsedJson.Path("projects").Data().([]interface{})
if !ok {
log.Fatal("Failed to parse projects")
}


projectRoot := getProjectRoot()
if projectRoot == "" {
return res, stdinBody
}


var selectedAuthHeader string

// Find the matching project
for _, project := range projects {
projectMap, ok := project.(map[string]interface{})
if !ok {
continue
}

if projectMap["project_root"] == projectRoot {
authData, exists := projectMap["result"].(map[string]interface{})["auth"]
if !exists {
} else {
authArray, ok := authData.([]interface{})
if !ok {
return res, stdinBody
}

// Filter the auth methods where "selected" is true
for _, authEntry := range authArray {
authMap, ok := authEntry.(map[string]interface{})
if !ok {
continue
}

selected, exists := authMap["selected"].(bool)
if exists && selected {
// Construct Authorization header based on type
authType := authMap["type"].(string)
authValue := authMap["value"].(map[string]interface{})
switch authType {
case "bearer-token":
if token, ok := authValue["token"].(string); ok {
selectedAuthHeader = "Authorization: Bearer " + token
}
case "jwt":
if jwt, ok := authValue["jwt"].(string); ok {
selectedAuthHeader = "Authorization: Bearer " + jwt
}
case "api-key":
if key, ok := authValue["key"].(string); ok {
if value, ok := authValue["value"].(string); ok {
selectedAuthHeader = key + ": " + value
}
}
case "basic-auth":
if username, ok := authValue["username"].(string); ok {
if password, ok := authValue["password"].(string); ok {
credentials := username + ":" + password
encoded := js.Global().Get("btoa").Invoke(credentials).String()
selectedAuthHeader = "Authorization: Basic " + encoded
}
}
}
break // Stop after the first selected auth method
}
}
}
break
}
}
// Add the selected authentication method to headers if not already present
if selectedAuthHeader != "" {
authHeaderExists := false
for _, header := range res {
if len(header) >= len("Authorization:") && header[:len("Authorization:")] == "Authorization:" {
authHeaderExists = true
break
}
}

if !authHeaderExists {
res = append(res, "Authorization: Bearer "+LaBearerAuthToken)
if !authHeaderExists {
res = append(res, selectedAuthHeader)
}
}

return res, stdinBody
}



func getProjectRoot() string {
// Fetch project_root from the <meta> tag in the document
meta := js.Global().Get("document").Call("querySelector", `meta[name="project_root"]`)
if meta.IsNull() {
return ""
}
return meta.Call("getAttribute", "content").String()
}
4 changes: 2 additions & 2 deletions l2.wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func main() {

func wasmLamaPromise() js.Func {
return js.FuncOf(func(this js.Value, args []js.Value) interface{} {
laBearerAuthToken := args[2].String()
js.Global().Set("LaBearerAuthToken", laBearerAuthToken)
liveapiManifest := args[2].String()
js.Global().Set("liveapiManifest", liveapiManifest)
inputdata := args[0].String()
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
resolve := args[0]
Expand Down
5 changes: 4 additions & 1 deletion wasmbuild.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/bin/bash
export AWS_ACCESS_KEY_ID=""
export AWS_SECRET_ACCESS_KEY=""
GOOS=js GOARCH=wasm go build -a -gcflags=all="-l -B -wb=false" -ldflags="-w -s" -o static/main.wasm
cp /home/sreedeep/Downloads/Lama2/static/main.wasm /home/sreedeep/js-widget/dist/main.wasm
# cp static/main.wasm /home/i3nux-mint/repos/liveapi/src/views
aws s3 cp static/main.wasm s3://temp-2d/temp/main.wasm --region ap-south-1