Skip to content

Commit

Permalink
lsp implementation. fixes and disabling log for lsp
Browse files Browse the repository at this point in the history
  • Loading branch information
LinceMathew committed Sep 6, 2024
1 parent 489ecb5 commit db279aa
Show file tree
Hide file tree
Showing 14 changed files with 57 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cmdexec/js.wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func RunVMCode(jsCode string, vm interface{}) {

// GenerateChainCode takes in an HTTP response body and comes up with some JS code to define the "magic variable" result.
func GenerateChainCode(httpRespBody string) string {
fmt.Println("WW: Value of httpRespBody is",httpRespBody)
// fmt.Println("WW: Value of httpRespBody is",httpRespBody)
code := `try {
result = JSON.parse(String.raw` + "`" + httpRespBody + "`" + `)
console.log("Stored as JSON")
Expand Down
9 changes: 4 additions & 5 deletions cmdgen/cmdgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/HexmosTech/lama2/lama2cmd"
)

// The assembleCmdString function constructs a httpie
// command string for an HTTP request based on the provided
// The assembleCmdString function constructs a httpie
// command string for an HTTP request based on the provided
// HTTP method, URL, JSON object, headers, and options. It also
// handle multipart and form data.
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 @@ -93,7 +93,6 @@ func assembleCmdString(httpv string, url string, jsonObj *gabs.Container, header
// and finally generates a string representing the generated
// command
func ConstructCommandHelper(parsedInput *gabs.Container) (string, string, *gabs.Container, *gabs.Container, bool, bool) {
fmt.Println("WW parsedInput:", parsedInput.StringIndent("", " "))
httpv := parsedInput.S("verb", "value")
url := parsedInput.S("url", "value")
jsonObj := parsedInput.S("details", "ip_data")
Expand All @@ -105,7 +104,7 @@ func ConstructCommandHelper(parsedInput *gabs.Container) (string, string, *gabs.
multipartBool = true
}
formBool := form != nil
fmt.Println("WW httpv:", httpv.Data().(string))
fmt.Println("WW url:", url.Data().(string))
// fmt.Println("WW httpv:", httpv.Data().(string))
// fmt.Println("WW url:", url.Data().(string))
return httpv.Data().(string), url.Data().(string), jsonObj, headers, multipartBool, formBool
}
8 changes: 4 additions & 4 deletions controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ func extractArgs(args []interface{}) (*lama2cmd.Opts, string) {
}

func processLama2FileBlock(block *gabs.Container, vm interface{}, o *lama2cmd.Opts, dir string) (httpie.ExResponse, int64) {
fmt.Println("WW: block value before:", block.String())
// fmt.Println("WW: block value before:", block.String())
preprocess.ProcessVarsInBlock(block, vm)
fmt.Println("WW: block value after:", block.String())
// fmt.Println("WW: block value after:", block.String())

cmd, stdinBody := cmdgen.ConstructCommand(block, o)
fmt.Println("WW: cmd:", cmd)
fmt.Println("WW: stdinBody:", stdinBody)
// fmt.Println("WW: cmd:", cmd)
// fmt.Println("WW: stdinBody:", stdinBody)
var resp httpie.ExResponse
var e1 error
resp, responseTime, e1 := cmdexec.ExecCommand(cmd, stdinBody, dir)
Expand Down
7 changes: 3 additions & 4 deletions execute/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ func NewExecutor() *Executor {
}

func (e *Executor) ExecuteCommand(req request.JSONRPCRequest) response.JSONRPCResponse {
filePath := "/home/lince/hexmos/apihub/apihub/fb_backend_v3/search.l2"

filePath := req.Params.FilePath
jsonResponse, err := ProcessForLSP(filePath)
if err != nil {
log.Error().Err(err).Msg("")
Expand All @@ -34,7 +33,7 @@ func (e *Executor) ExecuteCommand(req request.JSONRPCRequest) response.JSONRPCRe
}

func ProcessForLSP(filePath string) (*gabs.Container, error) {
log.Debug().Str("filePath", filePath).Msg("")
// log.Debug().Str("filePath", filePath).Msg("")

apiContent, err := preprocess.GetLamaFileAsString(filePath)
if err != nil {
Expand All @@ -52,7 +51,7 @@ func ProcessForLSP(filePath string) (*gabs.Container, error) {
return nil, e
}

log.Debug().Str("Parsed API", parsedAPI.String()).Msg("")
// log.Debug().Str("Parsed API", parsedAPI.String()).Msg("")
resp, _, responseTime, statusCodes, contentSizes, err := controller.HandleParsedFileHelper(parsedAPI)
if err != nil {
return nil, err
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/HexmosTech/gabs/v2 v2.6.5 h1:08ZCTfpH/ZxGwrjOfipdZ7x75t0svRDIw2qjlGyA
github.com/HexmosTech/gabs/v2 v2.6.5/go.mod h1:172vE+burNJgaTsFgfbzzqT4wF+FELkL3t2BCmxa2lo=
github.com/HexmosTech/godotenv v0.0.0-20230327154318-72e1f3d32ed3 h1:erCSX5e8CH5f9HjQfpp9SMWuTJXD1ikmW9E9ns1DcwA=
github.com/HexmosTech/godotenv v0.0.0-20230327154318-72e1f3d32ed3/go.mod h1:sln7CopgjyZgBSuIw7IkdpwIQf72vgZiy1kgYterloo=
github.com/HexmosTech/httpie-go v1.0.10 h1:UBw2nsCs2mPmpGTR7vYJ/LuVSyt7ckB9PpBcyL6NT1U=
github.com/HexmosTech/httpie-go v1.0.10/go.mod h1:U2lyp7l8mHMJUBR3DAsa79A2eY1b/FtuMOkUw1vKY8s=
github.com/HexmosTech/httpie-go v1.0.11 h1:Nxv85Em3O2Cs0qsQn6+8PXfek/qWiK6W01XMYX+vr84=
github.com/HexmosTech/httpie-go v1.0.11/go.mod h1:U2lyp7l8mHMJUBR3DAsa79A2eY1b/FtuMOkUw1vKY8s=
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
Expand Down
16 changes: 6 additions & 10 deletions l2.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build cli

package main

import (
Expand All @@ -11,17 +9,15 @@ import (
var version string
var lamawasm string

// func init() {

// }

func main() {
if len(version) == 0 {
version = "vUnset"
}
// log.Logger = log.Output(io.Discard)

// os.Stdout.Write([]byte{})
// os.Stderr.Write([]byte{})

lama2cmd.SetLSPServer(&l2lsp.DefaultLSPServer{})
controller.Process(version)


}

}
11 changes: 6 additions & 5 deletions l2lsp/methods/lsp_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import (

func ExecuteCommand(req request.JSONRPCRequest) response.JSONRPCResponse {

filePath := "/home/lince/hexmos/apihub/apihub/fb_backend_v3/search.l2"
// filePath := "/home/lince/hexmos/apihub/apihub/fb_backend_v3/search.l2"

jsonResponse, err := execute.ProcessForLSP(filePath)
jsonResponse, err := execute.ProcessForLSP(req.Params.FilePath)
// log.Info().Interface("jsonRespons", jsonResponse).Msg("")
if err != nil {
log.Error().Err(err).Msg("")
}
resp := response.CreateExecuteCommandResponse(req, jsonResponse)
log.Info().Interface("resp", resp).Msg("")

return response.JSONRPCResponse{
Result: jsonResponse,
}
return resp
}
2 changes: 1 addition & 1 deletion l2lsp/request/general_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Params struct {
WorkspaceFolders *[]WorkspaceFolder `json:"workspaceFolders,omitempty"`
SearchQuery *string `json:"searchQuery,omitempty"`
TextDocument *Uri `json:"textDocument,omitempty"`
File *string `json:"file,omitempty"`
FilePath string `json:"filePath,omitempty"`
}

type ClientInfo struct {
Expand Down
13 changes: 13 additions & 0 deletions l2lsp/response/executecomand_request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package response

import "github.com/HexmosTech/lama2/l2lsp/request"

func CreateExecuteCommandResponse(req request.JSONRPCRequest, response interface{}) JSONRPCResponse {
return JSONRPCResponse{
JSONRPC: "2.0",
ID: req.ID,
Result: response,
}
}

// {"jsonrpc":"2.0","id":12,"method":"executeCommand","params":{"filePath":"/home/lince/hexmos/apihub/apihub/fb_backend_v3/search.l2"}}
4 changes: 3 additions & 1 deletion lama2cmd/lama2cmd.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// Package `lama2cmd` provides CLI argument parsing facilities.
// It hosts the `Opts` structure to record user intentions
package lama2cmd
Expand Down Expand Up @@ -47,6 +46,8 @@ type Opts struct {
} `positional-args:"yes"`
}



func getParsedInput(argList []string) (Opts, []string) {
argList = argList[1:] // remove command name
o := Opts{}
Expand Down Expand Up @@ -100,6 +101,7 @@ func ArgParsing(o *Opts, version string) {
os.Exit(0)
}
if o.Lsp {
outputmanager.DisableLogBuff()
log.Info().Msg("Starting LSP server")
lspServer.StartLspServer()
os.Exit(0)
Expand Down
14 changes: 12 additions & 2 deletions outputManager/output_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package outputmanager
import (
"bytes"
"fmt"
"io"
"os"

"github.com/HexmosTech/gabs/v2"
Expand Down Expand Up @@ -33,14 +34,23 @@ type ContentSize struct {
SizeInBytes int `json:"sizeInBytes"`
}

func init() {
consoleWriter := zerolog.ConsoleWriter{Out: os.Stderr}

func setupLogger(consoleOut io.Writer) {
consoleWriter := zerolog.ConsoleWriter{Out: consoleOut}
consoleWriter2 := zerolog.ConsoleWriter{Out: &LogBuff}
multi := zerolog.MultiLevelWriter(consoleWriter, consoleWriter2)
logger := zerolog.New(multi).With().Timestamp().Logger()
log.Logger = logger
}

func init() {
setupLogger(os.Stderr)
}

func DisableLogBuff() {
setupLogger(io.Discard)
}

// ConfigureZeroLog provides global log level setting. By default,
// ZeroLog uses the DEBUG level; however, the function makes the
// desired level more explicit
Expand Down
5 changes: 2 additions & 3 deletions parser/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package parser

import (
"errors"
"fmt"
"reflect"
"strings"

Expand Down Expand Up @@ -68,8 +67,8 @@ func (p *Parser) LookAhead(rules []string) bool {
for _, rule := range rules {
initialPos := p.Pos
res := p.ruleMethodMap[rule].Call([]reflect.Value{})
op := res[0].Interface().(*gabs.Container)
fmt.Println(op.String())
// op := res[0].Interface().(*gabs.Container)
// fmt.Println(op.String())
e := res[1]
p.Pos = initialPos
if e.IsNil() {
Expand Down
2 changes: 1 addition & 1 deletion preprocess/expandvar.wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func Expand(s string, vm interface{}, mapping map[string]string) string {
} else {
// buf = getJsValue(vm, name, mapping, buf)
buf = getJsValue(name, mapping, buf)
fmt.Println("WW: Buffer value:",name, string(buf))
// fmt.Println("WW: Buffer value:",name, string(buf))
}
j += w
i = j + 1
Expand Down
3 changes: 1 addition & 2 deletions preprocess/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func LoadEnvFile(l2path string) {
envFileName := filepath.Base(l2path)
err := godotenv.Load(l2path)
if err != nil {
fmt.Println("Didn't find " + envFileName + " in the API directory")
log.Warn().Msgf("Didn't find %s in the API directory", envFileName)
}
}

Expand Down Expand Up @@ -195,7 +195,6 @@ func ExpandURL(block *gabs.Container, vm interface{}) {
b := block.S("url", "value").Data().(string)
log.Debug().Str("Url block", b).Msg("")
url := ExpandEnv(b, vm)
fmt.Println("WW: Expanded URL:", url)
block.Delete("url", "value")
block.Set(url, "url", "value")
}
Expand Down

0 comments on commit db279aa

Please sign in to comment.