Skip to content

Commit

Permalink
feat: run with env & remove default sfn name
Browse files Browse the repository at this point in the history
  • Loading branch information
woorui committed Oct 31, 2024
1 parent 3fbeb92 commit 4f95c38
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
// defaultSFNFile is the default serverless file name
const (
defaultSFNSourceFile = "app.go"
defaultSFNSourceTSFile = "app.ts"
defaultSFNTestSourceFile = "app_test.go"
defaultSFNCompliedFile = "sfn.yomo"
defaultSFNWASIFile = "sfn.wasm"
Expand Down
4 changes: 2 additions & 2 deletions cli/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var runCmd = &cobra.Command{
Short: "Run a YoMo Stream Function",
Long: "Run a YoMo Stream Function",
Run: func(cmd *cobra.Command, args []string) {
if err := parseFileArg(args, &opts, defaultSFNCompliedFile, defaultSFNWASIFile, defaultSFNSourceFile); err != nil {
if err := parseFileArg(args, &opts, defaultSFNCompliedFile, defaultSFNWASIFile, defaultSFNSourceFile, defaultSFNSourceTSFile); err != nil {
log.FailureStatusEvent(os.Stdout, err.Error())
return
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func init() {
rootCmd.AddCommand(runCmd)

runCmd.Flags().StringVarP(&opts.ZipperAddr, "zipper", "z", "localhost:9000", "YoMo-Zipper endpoint addr")
runCmd.Flags().StringVarP(&opts.Name, "name", "n", "app", "yomo stream function name.")
runCmd.Flags().StringVarP(&opts.Name, "name", "n", "", "yomo stream function name.")
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")
Expand Down
2 changes: 1 addition & 1 deletion cli/serverless/golang/templates/main.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func run(_ *cobra.Command, _ []string) {

func init() {
rootCmd.Flags().StringVarP(&zipper, "zipper", "z", "localhost:9000", "YoMo-Zipper endpoint addr")
rootCmd.Flags().StringVarP(&name, "name", "n", "app", "yomo stream function name")
rootCmd.Flags().StringVarP(&name, "name", "n", "", "yomo stream function name")
rootCmd.Flags().StringVarP(&credential, "credential", "d", "", "client credential payload, eg: `token:dBbBiRE7`")
viper.SetEnvPrefix("YOMO_SFN")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
Expand Down
2 changes: 1 addition & 1 deletion cli/serverless/golang/templates/main_rx.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func run(_ *cobra.Command, _ []string) {

func init() {
rootCmd.Flags().StringVarP(&zipper, "zipper", "z", "localhost:9000", "YoMo-Zipper endpoint addr")
rootCmd.Flags().StringVarP(&name, "name", "n", "app", "yomo stream function name")
rootCmd.Flags().StringVarP(&name, "name", "n", "", "yomo stream function name")
rootCmd.Flags().StringVarP(&credential, "credential", "d", "", "client credential payload, eg: `token:dBbBiRE7`")
viper.SetEnvPrefix("YOMO_SFN")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
Expand Down
7 changes: 5 additions & 2 deletions cli/serverless/nodejs/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (w *NodejsWrapper) WorkDir() string {
}

// Build defines how to build the serverless function.
func (w *NodejsWrapper) Build() error {
func (w *NodejsWrapper) Build(env []string) error {
// 1. generate .wrapper.ts file
dstPath := filepath.Join(w.workDir, wrapperTS)
_ = os.Remove(dstPath)
Expand All @@ -87,6 +87,7 @@ func (w *NodejsWrapper) Build() error {
cmd.Dir = w.workDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = env
if err := cmd.Run(); err != nil {
return err
}
Expand All @@ -96,6 +97,7 @@ func (w *NodejsWrapper) Build() error {
cmd2.Dir = w.workDir
cmd2.Stdout = os.Stdout
cmd2.Stderr = os.Stderr
cmd2.Env = env
if err := cmd2.Run(); err != nil {
return err
}
Expand All @@ -104,11 +106,12 @@ func (w *NodejsWrapper) Build() error {
}

// Run runs the serverless function
func (w *NodejsWrapper) Run() error {
func (w *NodejsWrapper) Run(env []string) error {
cmd := exec.Command(w.nodePath, wrapperJS)
cmd.Dir = w.workDir
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = env

return cmd.Run()
}
Expand Down
14 changes: 12 additions & 2 deletions cli/serverless/nodejs/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
package nodejs

import (
"os"

"github.com/yomorun/yomo/cli/serverless"
"github.com/yomorun/yomo/pkg/wrapper"
)
Expand Down Expand Up @@ -31,12 +33,20 @@ func (s *nodejsServerless) Init(opts *serverless.Options) error {

// Build calls wrapper.Build
func (s *nodejsServerless) Build(_ bool) error {
return s.wrapper.Build()
return s.wrapper.Build(os.Environ())
}

// Run the wrapper.Run
func (s *nodejsServerless) Run(verbose bool) error {
return wrapper.Run(s.name, s.zipperAddr, s.credential, s.wrapper)
err := serverless.LoadEnvFile(s.wrapper.workDir)
if err != nil {
return err
}
env := os.Environ()
if verbose {
env = append(env, "YOMO_LOG_LEVEL=debug")
}
return wrapper.Run(s.name, s.zipperAddr, s.credential, s.wrapper, env)
}

// Executable shows whether the program needs to be built
Expand Down
14 changes: 7 additions & 7 deletions pkg/wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ type SFNWrapper interface {
// WorkDir returns the working directory of the serverless function to build and run.
WorkDir() string
// Build defines how to build the serverless function.
Build() error
Build(env []string) error
// Run defines how to run the serverless function.
Run() error
Run(env []string) error
}

// BuildAndRun builds and runs the serverless function.
func BuildAndRun(name, zipperAddr, credential string, wrapper SFNWrapper) error {
if err := wrapper.Build(); err != nil {
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)
return Run(name, zipperAddr, credential, wrapper, env)
}

// Run runs the serverless function.
func Run(name, zipperAddr, credential string, wrapper SFNWrapper) error {
func Run(name, zipperAddr, credential string, wrapper SFNWrapper, env []string) error {
sockPath := filepath.Join(wrapper.WorkDir(), "sfn.sock")
_ = os.Remove(sockPath)

Expand All @@ -53,7 +53,7 @@ func Run(name, zipperAddr, credential string, wrapper SFNWrapper) error {
errch := make(chan error)

go func() {
if err := wrapper.Run(); err != nil {
if err := wrapper.Run(env); err != nil {
errch <- err
}
}()
Expand Down

0 comments on commit 4f95c38

Please sign in to comment.