Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
5 changes: 5 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,16 @@
opts.Runtime = v.GetString("runtime")
}

// parseFileArg parses the filename from command line arguments or uses default files if none provided.
// It updates the given serverless.Options with the filename and validates it via checkOptions.
// Returns nil if a valid filename is found, otherwise continues trying default files.
func parseFileArg(args []string, opts *serverless.Options, defaultFiles ...string) error {
// parse filename from args, like `yomo run app.go`

Check warning on line 71 in cli/cli.go

View check run for this annotation

Codecov / codecov/patch

cli/cli.go#L71

Added line #L71 was not covered by tests
if len(args) >= 1 && args[0] != "" {
opts.Filename = args[0]
return checkOptions(opts)
}
// if no filename is provided, use the default files
for _, f := range defaultFiles {
opts.Filename = f
err := checkOptions(opts)
Expand Down
12 changes: 9 additions & 3 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@
return
}

if err := s.Build(true); err != nil {
log.FailureStatusEvent(os.Stdout, "%s", err.Error())
os.Exit(127)
// if has `--production` flag, skip s.Build() process
isProduction := opts.Production
log.InfoStatusEvent(os.Stdout, "prodution mode is %v", opts.Production)
if !isProduction {
if err := s.Build(true); err != nil {
log.FailureStatusEvent(os.Stdout, "%s", err.Error())
os.Exit(127)
}

Check warning on line 77 in cli/run.go

View check run for this annotation

Codecov / codecov/patch

cli/run.go#L71-L77

Added lines #L71 - L77 were not covered by tests
}

log.InfoStatusEvent(
Expand All @@ -93,6 +98,7 @@
runCmd.Flags().StringVarP(&opts.ModFile, "modfile", "m", "", "custom go.mod")
runCmd.Flags().StringVarP(&opts.Credential, "credential", "d", "", "client credential payload, eg: `token:dBbBiRE7`")
runCmd.Flags().StringVarP(&opts.Runtime, "runtime", "r", "", "serverless runtime type")
runCmd.Flags().BoolVarP(&opts.Production, "production", "p", false, "run in production mode, skip the build process")

Check warning on line 101 in cli/run.go

View check run for this annotation

Codecov / codecov/patch

cli/run.go#L101

Added line #L101 was not covered by tests

viper.BindPFlags(viper.RunViper, runCmd.Flags())
}
92 changes: 33 additions & 59 deletions cli/serverless/nodejs/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@

// Build defines how to build the serverless llm function.
func (w *NodejsWrapper) Build(env []string) error {
// 1. generate .wrapper.ts file
dstPath := filepath.Join(w.workDir, wrapperTS)
// 1. generate ./src/.wrapper.ts file
dstPath := filepath.Join(w.workDir, "src/", wrapperTS)
// remove the old one

Check warning on line 94 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L92-L94

Added lines #L92 - L94 were not covered by tests
_ = os.Remove(dstPath)

// create new one

Check warning on line 96 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L96

Added line #L96 was not covered by tests
if err := w.genWrapperTS(w.functionName, dstPath); err != nil {
return err
}
Expand Down Expand Up @@ -137,80 +138,44 @@
return err
}

// 4. copy files other than .ts file from src/ to dist/src/ because tsc do not do that
srcDir := filepath.Join(w.workDir, "src")
dstDir := filepath.Join(w.workDir, "dist/src")
if err := os.MkdirAll(dstDir, 0755); err != nil {
return err
}
// copy all files from src/ to dist/
err = filepath.Walk(srcDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == srcDir {
return nil
}

// Get relative path to maintain directory structure
relPath, err := filepath.Rel(srcDir, path)
if err != nil {
return fmt.Errorf("failed to get relative path: %v", err)
}

if filepath.Ext(path) == ".ts" {
return nil
}
// 4. copy src/app.ts to dist/app.ts
baseFileName := filepath.Base(w.entryTSFile)
srcEntryPath := w.entryTSFile
dstEntryPath := filepath.Join(w.outputDir, baseFileName)

Check warning on line 144 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L142-L144

Added lines #L142 - L144 were not covered by tests

// Create destination path with same structure
dstPath := filepath.Join(dstDir, relPath)
data, err := os.ReadFile(srcEntryPath)
if err != nil {
return fmt.Errorf("failed to read file %s: %v", srcEntryPath, err)
}

Check warning on line 149 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L146-L149

Added lines #L146 - L149 were not covered by tests

// Check if the destination directory exists
dstDir := filepath.Dir(dstPath)
if _, err := os.Stat(dstDir); os.IsNotExist(err) {
return fmt.Errorf("destination directory %s does not exist", dstDir)
}
if err := os.WriteFile(dstEntryPath, data, 0644); err != nil {
return fmt.Errorf("failed to write file %s: %v", dstEntryPath, err)
}

Check warning on line 153 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L151-L153

Added lines #L151 - L153 were not covered by tests

// Copy the file to ./dist/src/
if info.IsDir() {
return os.MkdirAll(dstPath, info.Mode())
} else {
// Read the source file
data, err := os.ReadFile(path)
if err != nil {
return fmt.Errorf("failed to read file %s: %v", path, err)
}

// Write to the destination file
if err := os.WriteFile(dstPath, data, info.Mode()); err != nil {
return fmt.Errorf("failed to write file %s: %v", dstPath, err)
}

log.InfoStatusEvent(os.Stdout, "copied %s to %s\n", path, dstPath)
return nil
}
})
// log.InfoStatusEvent(os.Stdout, "Copied %s to %s", srcEntryPath, dstEntryPath)

return err
}

// Run runs the serverless function
func (w *NodejsWrapper) Run(env []string) error {
// ./dist/.wrapper.js
entryJSFile := filepath.Join(w.outputDir, wrapperJS)

Check warning on line 163 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L162-L163

Added lines #L162 - L163 were not covered by tests
// try to run with bunjs
// first, check if bun is installed
bunPath, err := exec.LookPath("bun")
if err == nil {
// bun is installed, run the wrapper with bun
// get the version of tsgo/tsc
// get the version of bun

Check warning on line 169 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L169

Added line #L169 was not covered by tests
var bunVersion string
if v, err := checkVersion("bun"); err != nil {
return err
} else {
bunVersion = v
}
log.InfoStatusEvent(os.Stdout, "Runtime is Bun (Version %s)", bunVersion)
log.InfoStatusEvent(os.Stdout, "Runtime is Bun (Version %s), %s", bunVersion, entryJSFile)

Check warning on line 176 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L176

Added line #L176 was not covered by tests

cmd := exec.Command(bunPath, wrapperTS)
cmd := exec.Command(bunPath, "run", entryJSFile)

Check warning on line 178 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L178

Added line #L178 was not covered by tests
cmd.Dir = w.workDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand All @@ -220,18 +185,27 @@
}

// if bun is not found, fallback to nodejs
cmd := exec.Command(w.nodePath, filepath.Join(w.outputDir, wrapperJS))
cmd := exec.Command(w.nodePath, entryJSFile)

Check warning on line 188 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L188

Added line #L188 was not covered by tests
cmd.Dir = w.workDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = env

var nodeVersion string
if v, err := checkVersion("node"); err != nil {
return err
} else {
nodeVersion = v
}

Check warning on line 199 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L194-L199

Added lines #L194 - L199 were not covered by tests

log.InfoStatusEvent(os.Stdout, "Runtime is Node.js (Version %s), %s, %s", nodeVersion, w.nodePath, entryJSFile)

Check warning on line 202 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L201-L202

Added lines #L201 - L202 were not covered by tests
return cmd.Run()
}

func (w *NodejsWrapper) genWrapperTS(functionName, dstPath string) error {
baseFilename := "./src/" + filepath.Base(w.fileName)
entryTS := baseFilename + ".ts"
baseFilename := "./" + filepath.Base(w.fileName)
entryTS := "./dist/" + baseFilename + ".ts"

Check warning on line 208 in cli/serverless/nodejs/runtime.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/runtime.go#L207-L208

Added lines #L207 - L208 were not covered by tests

data := struct {
WorkDir string
Expand Down
5 changes: 3 additions & 2 deletions cli/serverless/nodejs/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"outDir": "dist",
"outDir": "./dist",
"rootDir": "./src",

Check warning on line 47 in cli/serverless/nodejs/serverless.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/serverless.go#L46-L47

Added lines #L46 - L47 were not covered by tests
"skipLibCheck": true
},
"include": ["src/**/*", ".wrapper.ts"],
"include": ["src/**/*", "src/.wrapper.ts"],

Check warning on line 50 in cli/serverless/nodejs/serverless.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/nodejs/serverless.go#L50

Added line #L50 was not covered by tests
"exclude": ["node_modules"]
}`
if err := os.WriteFile(tsconfigPath, []byte(tsconfigContent), 0644); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions cli/serverless/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ type Options struct {
Credential string
// Runtime specifies the serverless runtime environment type
Runtime string
// Production indicates whether to run in production mode
Production bool
}
4 changes: 3 additions & 1 deletion cli/serverless/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@
return nil
}

// Create returns a new serverless instance with options.
// Create returns a new serverless instance with options
func Create(opts *Options) (Serverless, error) {
// TODO: need refactor to infer serverless runtime from `--runtime` flag or `RUNTIME` env,
// instead of inferring from file extension here.

Check warning on line 62 in cli/serverless/serverless.go

View check run for this annotation

Codecov / codecov/patch

cli/serverless/serverless.go#L61-L62

Added lines #L61 - L62 were not covered by tests
ext := filepath.Ext(opts.Filename)

driversMu.RLock()
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.23.0

require (
cloud.google.com/go/vertexai v0.13.4
github.com/anthropics/anthropic-sdk-go v1.2.0
github.com/anthropics/anthropic-sdk-go v1.2.1
github.com/briandowns/spinner v1.23.2
github.com/caarlos0/env/v6 v6.10.1
github.com/fatih/color v1.18.0
Expand All @@ -17,7 +17,7 @@ require (
github.com/matoous/go-nanoid/v2 v2.1.0
github.com/quic-go/quic-go v0.52.0
github.com/robfig/cron/v3 v3.0.1
github.com/sashabaranov/go-openai v1.40.0
github.com/sashabaranov/go-openai v1.40.1
github.com/shirou/gopsutil/v3 v3.24.5
github.com/spf13/cobra v1.9.1
github.com/spf13/pflag v1.0.6
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/anthropics/anthropic-sdk-go v1.2.0 h1:RQzJUqaROewrPTl7Rl4hId/TqmjFvfnkmhHJ6pP1yJ8=
github.com/anthropics/anthropic-sdk-go v1.2.0/go.mod h1:AapDW22irxK2PSumZiQXYUFvsdQgkwIWlpESweWZI/c=
github.com/anthropics/anthropic-sdk-go v1.2.1 h1:zwRsDe3+KEJNDwKdbtum4P3UsQ9Uc8y/WmBE+V2WElk=
github.com/anthropics/anthropic-sdk-go v1.2.1/go.mod h1:AapDW22irxK2PSumZiQXYUFvsdQgkwIWlpESweWZI/c=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -184,8 +184,8 @@ github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.9.0 h1:GbgQGNtTrEmddYDSAH9QLRyfAHY12md+8YFTqyMTC9k=
github.com/sagikazarmark/locafero v0.9.0/go.mod h1:UBUyz37V+EdMS3hDF3QWIiVr/2dPrx49OMO0Bn0hJqk=
github.com/sashabaranov/go-openai v1.40.0 h1:Peg9Iag5mUJtPW00aYatlsn97YML0iNULiLNe74iPrU=
github.com/sashabaranov/go-openai v1.40.0/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sashabaranov/go-openai v1.40.1 h1:bJ08Iwct5mHBVkuvG6FEcb9MDTfsXdTYPGjYLRdeTEU=
github.com/sashabaranov/go-openai v1.40.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
Expand Down
14 changes: 7 additions & 7 deletions pkg/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ type SFNWrapper interface {
Run(env []string) error
}

// BuildAndRun builds and runs the serverless function.
func BuildAndRun(name, zipperAddr, credential string, wrapper SFNWrapper, env []string) error {
if err := wrapper.Build(env); err != nil {
return err
}
return Run(name, zipperAddr, credential, wrapper, env)
}
// // BuildAndRun builds and runs the serverless function.
// func BuildAndRun(name, zipperAddr, credential string, wrapper SFNWrapper, env []string) error {
// if err := wrapper.Build(env); err != nil {
// return err
// }
// return Run(name, zipperAddr, credential, wrapper, env)
// }

// Run runs the serverless function.
func Run(name, zipperAddr, credential string, wrapper SFNWrapper, env []string) error {
Expand Down