Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: QoL Issues and Minor Bugs #45

Merged
merged 4 commits into from
Sep 27, 2023
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
8 changes: 4 additions & 4 deletions cmd/function/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ func BuildCmd(hidden bool) command.SetupCommand[*config.Config] {

sig, err := sts.Get(sf.Signature.Name, sf.Signature.Tag, sf.Signature.Organization, "")
if err != nil {
return fmt.Errorf("failed to get signature %s:%s: %w", sf.Signature.Name, sf.Signature.Tag, err)
return fmt.Errorf("failed to get signature local/%s:%s: %w", sf.Signature.Name, sf.Signature.Tag, err)
}
if sig == nil {
return fmt.Errorf("signature local/%s:%s not found", sf.Signature.Name, sf.Signature.Tag)
}

signatureSchema = sig.Schema
Expand All @@ -145,7 +148,6 @@ func BuildCmd(hidden bool) command.SetupCommand[*config.Config] {

out := ch.Printer.Out()

end := ch.Printer.PrintProgress(fmt.Sprintf("Building scale function local/%s:%s...", sf.Name, sf.Tag))
var scaleFunc *scalefunc.Schema
switch scalefunc.Language(sf.Language) {
case scalefunc.Go:
Expand Down Expand Up @@ -176,10 +178,8 @@ func BuildCmd(hidden bool) command.SetupCommand[*config.Config] {
}
scaleFunc, err = build.LocalRust(opts)
default:
end()
return fmt.Errorf("language %s is not supported for local builds", sf.Language)
}
end()
if err != nil {
return fmt.Errorf("failed to build scale function: %w", err)
}
Expand Down
28 changes: 15 additions & 13 deletions cmd/function/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,41 +88,43 @@ func ExportCmd() command.SetupCommand[*config.Config] {
outputPath = path.Join(wd, outputPath)
}

oInfo, err := os.Stat(output)
oInfo, err := os.Stat(outputPath)
if err != nil {
return fmt.Errorf("failed to stat output path %s: %w", output, err)
}

if !oInfo.IsDir() {
return fmt.Errorf("output path %s is not a directory", output)
ch.Printer.Printf("Creating output directory %s\n", printer.BoldBlue(outputPath))
err = os.MkdirAll(outputPath, 0755)
if err != nil {
return fmt.Errorf("failed to create output directory %s: %w", outputPath, err)
}
} else if !oInfo.IsDir() {
return fmt.Errorf("output path %s is not a directory", outputPath)
}

if outputName == "" {
suffix := "scale"
if raw {
suffix = "wasm"
}
output = path.Join(output, fmt.Sprintf("%s-%s-%s.%s", parsed.Organization, parsed.Name, parsed.Tag, suffix))
outputPath = path.Join(outputPath, fmt.Sprintf("%s-%s-%s.%s", parsed.Organization, parsed.Name, parsed.Tag, suffix))
} else {
output = path.Join(output, outputName)
outputPath = path.Join(outputPath, outputName)
}

if raw {
err = os.WriteFile(output, e.Schema.Function, 0644)
err = os.WriteFile(outputPath, e.Schema.Function, 0644)
} else {
err = os.WriteFile(output, e.Schema.Encode(), 0644)
err = os.WriteFile(outputPath, e.Schema.Encode(), 0644)
}
if err != nil {
return fmt.Errorf("failed to write function to %s: %w", output, err)
return fmt.Errorf("failed to write function to %s: %w", outputPath, err)
}

if ch.Printer.Format() == printer.Human {
ch.Printer.Printf("Exported scale function %s to %s\n", printer.BoldGreen(fmt.Sprintf("%s/%s:%s", parsed.Organization, parsed.Name, parsed.Tag)), printer.BoldBlue(output))
ch.Printer.Printf("Exported scale function %s to %s\n", printer.BoldGreen(fmt.Sprintf("%s/%s:%s", parsed.Organization, parsed.Name, parsed.Tag)), printer.BoldBlue(outputPath))
return nil
}

return ch.Printer.PrintResource(map[string]string{
"destination": output,
"destination": outputPath,
"org": parsed.Organization,
"name": parsed.Name,
"tag": parsed.Tag,
Expand Down
152 changes: 96 additions & 56 deletions cmd/function/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/loopholelabs/scale/scalefunc"
"github.com/loopholelabs/scale/signature/converter"
"github.com/loopholelabs/scale/storage"
"github.com/mattn/go-isatty"
"github.com/spf13/cobra"
"github.com/valyala/fasthttp"
"io"
Expand Down Expand Up @@ -144,72 +145,111 @@ func RunCmd(hidden bool) command.SetupCommand[*config.Config] {
return fmt.Errorf("failed to create type check signature: %w", err)
}

writer := ch.Printer.Out()
s, err := scale.New(scale.NewConfig(typecheckSignature.Signature).WithContext(ctx).WithFunctions(fns).WithStdout(writer).WithStderr(writer))
if err != nil {
return fmt.Errorf("failed to create scale: %w", err)
}
if isatty.IsTerminal(os.Stdin.Fd()) || isatty.IsCygwinTerminal(os.Stdin.Fd()) {
writer := ch.Printer.Out()
s, err := scale.New(scale.NewConfig(typecheckSignature.Signature).WithContext(ctx).WithFunctions(fns).WithStdout(writer).WithStderr(writer))
if err != nil {
return fmt.Errorf("failed to create scale: %w", err)
}

stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

server := fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
if !ctx.IsPost() {
ctx.Error("Unsupported method", fasthttp.StatusMethodNotAllowed)
return
}
instance, err := s.Instance()
if err != nil {
ctx.Error(fmt.Sprintf("Failed to create instance: %v", err), fasthttp.StatusInternalServerError)
return
}
sig, err := converter.NewSignature(fns[0].SignatureSchema)
if err != nil {
ctx.Error(fmt.Sprintf("Failed to create signature: %v", err), fasthttp.StatusInternalServerError)
return
}
err = sig.FromJSON(ctx.PostBody())
if err != nil {
ctx.Error(fmt.Sprintf("Failed to parse signature: %v", err), fasthttp.StatusInternalServerError)
return
}
err = instance.Run(ctx, sig)
if err != nil {
ctx.Error(fmt.Sprintf("Failed to run function: %v", err), fasthttp.StatusInternalServerError)
return
}
server := fasthttp.Server{
Handler: func(ctx *fasthttp.RequestCtx) {
if !ctx.IsPost() {
ctx.Error("Unsupported method", fasthttp.StatusMethodNotAllowed)
return
}
instance, err := s.Instance()
if err != nil {
ctx.Error(fmt.Sprintf("Failed to create instance: %v", err), fasthttp.StatusInternalServerError)
return
}
sig, err := converter.NewSignature(fns[0].SignatureSchema)
if err != nil {
ctx.Error(fmt.Sprintf("Failed to create signature: %v", err), fasthttp.StatusInternalServerError)
return
}
err = sig.FromJSON(ctx.PostBody())
if err != nil {
ctx.Error(fmt.Sprintf("Failed to parse signature: %v", err), fasthttp.StatusInternalServerError)
return
}
err = instance.Run(ctx, sig)
if err != nil {
ctx.Error(fmt.Sprintf("Failed to run function: %v", err), fasthttp.StatusInternalServerError)
return
}

body, err := sig.ToJSON()
if err != nil {
ctx.Error(fmt.Sprintf("Failed to encode signature: %v", err), fasthttp.StatusInternalServerError)
return
}

body, err := sig.ToJSON()
ctx.SetContentType("application/json")
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetBody(body)
},
CloseOnShutdown: true,
IdleTimeout: time.Second,
}

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
ch.Printer.Printf("Scale Functions %s listening at %s\n", printer.BoldGreen(args), printer.BoldGreen(listen))
err = server.ListenAndServe(listen)
if err != nil {
ctx.Error(fmt.Sprintf("Failed to encode signature: %v", err), fasthttp.StatusInternalServerError)
return
ch.Printer.Printf("error starting server: %v\n", printer.BoldRed(err))
}
}()
<-stop
err = server.Shutdown()
if err != nil {
return fmt.Errorf("failed to shutdown server: %w", err)
}
wg.Wait()
} else {
s, err := scale.New(scale.NewConfig(typecheckSignature.Signature).WithContext(ctx).WithFunctions(fns))
if err != nil {
return fmt.Errorf("failed to create scale: %w", err)
}

ctx.SetContentType("application/json")
ctx.SetStatusCode(fasthttp.StatusOK)
ctx.SetBody(body)
},
CloseOnShutdown: true,
IdleTimeout: time.Second,
}
inputData, err := io.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("failed to read from stdin: %w", err)
}
instance, err := s.Instance()
if err != nil {
return fmt.Errorf("failed to create instance: %w", err)
}
sig, err := converter.NewSignature(fns[0].SignatureSchema)
if err != nil {
return fmt.Errorf("failed to create signature: %w", err)
}
err = sig.FromJSON(inputData)
if err != nil {
return fmt.Errorf("failed to parse signature: %w", err)
}
err = instance.Run(ctx, sig)
if err != nil {
return fmt.Errorf("failed to run function: %w", err)
}

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
ch.Printer.Printf("Scale Functions %s listening at %s", printer.BoldGreen(args), printer.BoldGreen(listen))
err = server.ListenAndServe(listen)
outputData, err := sig.ToJSON()
if err != nil {
ch.Printer.Printf("error starting server: %v", printer.BoldRed(err))
return fmt.Errorf("failed to encode signature: %w", err)
}

_, err = os.Stdout.Write(outputData)
if err != nil {
return fmt.Errorf("failed to write to stdout: %w", err)
}
}()
<-stop
err = server.Shutdown()
if err != nil {
return fmt.Errorf("failed to shutdown server: %w", err)
}
wg.Wait()

return nil
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/signature/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func ExportCmd() command.SetupCommand[*config.Config] {
kind := args[2]
output := args[3]

kindString := "guest"
var kindString string
switch kind {
case "guest":
switch scalefunc.Language(language) {
Expand Down
4 changes: 2 additions & 2 deletions cmd/signature/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
}
sourceDir = path.Join(wd, sourceDir)
}
err := os.WriteFile(path.Join(sourceDir, fmt.Sprintf("scale.signature")), []byte(template.SignatureFile), 0644)
err := os.WriteFile(path.Join(sourceDir, "scale.signature"), []byte(template.SignatureFile), 0644)
if err != nil {
return fmt.Errorf("error writing signature: %w", err)
}
Expand All @@ -62,7 +62,7 @@ func NewCmd(hidden bool) command.SetupCommand[*config.Config] {
}

return ch.Printer.PrintResource(map[string]string{
"path": path.Join(directory, fmt.Sprintf("scale.signature")),
"path": path.Join(directory, "scale.signature"),
})
},
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ require (
github.com/loopholelabs/auth v0.2.47
github.com/loopholelabs/cmdutils v0.1.4
github.com/loopholelabs/releaser v0.1.1
github.com/loopholelabs/scale v0.4.0
github.com/loopholelabs/scale v0.4.2
github.com/mattn/go-isatty v0.0.19
github.com/mitchellh/go-homedir v1.1.0
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8
github.com/pkg/errors v0.9.1
github.com/posthog/posthog-go v0.0.0-20230801140217-d607812dee69
github.com/rs/zerolog v1.30.0
github.com/rs/zerolog v1.31.0
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
Expand Down Expand Up @@ -66,7 +67,6 @@ require (
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
Expand Down
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ github.com/loopholelabs/polyglot v1.1.3 h1:WUTcSZ2TQ1lv7CZ4I9nHFBUjf0hKJN+Yfz1rZ
github.com/loopholelabs/polyglot v1.1.3/go.mod h1:EA88BEkIluKHAWxhyOV88xXz68YkRdo9IzZ+1dj+7Ao=
github.com/loopholelabs/releaser v0.1.1 h1:o8Z4jIR4TcaSRiwFpAGIJSAkAUOxlXL1ML3EscGqxDU=
github.com/loopholelabs/releaser v0.1.1/go.mod h1:51hV+lDDYeJNzeVOZCQnGS5cJN8ODg/V5AxYtiL67UA=
github.com/loopholelabs/scale v0.4.0 h1:40x4q+Ch71YMMJBJFJo+2qB1XS95N8gbpjUg3U1Icpc=
github.com/loopholelabs/scale v0.4.0/go.mod h1:3dsQQCsIbJ5oOEpI/53jVBuDPYyi/Z85ZjroPDTKIzw=
github.com/loopholelabs/scale v0.4.2 h1:uIsUSeG0cPxTyt7GxJc+pY23QlBUAH5GsIobz6o/T5k=
github.com/loopholelabs/scale v0.4.2/go.mod h1:3dsQQCsIbJ5oOEpI/53jVBuDPYyi/Z85ZjroPDTKIzw=
github.com/loopholelabs/scale-signature-interfaces v0.1.7 h1:aOJJZpCKn/Q5Q0Gj+/Q6c7/iABEbojjbCzIqw7Mxyi0=
github.com/loopholelabs/scale-signature-interfaces v0.1.7/go.mod h1:3XLMjJjBf5lYxMtNKk+2XAWye4UyrkvUBJ9L6x2QCAk=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand All @@ -305,11 +305,9 @@ github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJ
github.com/markbates/oncer v0.0.0-20181203154359-bf2de49a0be2/go.mod h1:Ld9puTsIW75CHf65OeIOkyKbteujpZVXDpWK6YGZbxE=
github.com/markbates/safe v1.0.1/go.mod h1:nAqgmRi7cY2nqMc92/bSEeQA+R4OheNU2T1kNSCBdG0=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
Expand Down Expand Up @@ -355,8 +353,8 @@ github.com/rogpeppe/go-internal v1.2.2/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.30.0 h1:SymVODrcRsaRaSInD9yQtKbtWqwsfoPcRff/oRXLj4c=
github.com/rs/zerolog v1.30.0/go.mod h1:/tk+P47gFdPXq4QYjvCmT5/Gsug2nagsFWBWhAiSi1w=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
Expand Down Expand Up @@ -593,8 +591,6 @@ golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210616045830-e2b7044e8c71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down