diff --git a/cli/cli.go b/cli/cli.go index dab3ee284..a5092a166 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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" diff --git a/cli/run.go b/cli/run.go index f134806e7..6aeb6282f 100644 --- a/cli/run.go +++ b/cli/run.go @@ -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 } @@ -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") diff --git a/cli/serverless/golang/templates/main.tmpl b/cli/serverless/golang/templates/main.tmpl index f20ff6fbe..9e77d10de 100644 --- a/cli/serverless/golang/templates/main.tmpl +++ b/cli/serverless/golang/templates/main.tmpl @@ -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("-", "_")) diff --git a/cli/serverless/golang/templates/main_rx.tmpl b/cli/serverless/golang/templates/main_rx.tmpl index a8245391e..196f24bd1 100644 --- a/cli/serverless/golang/templates/main_rx.tmpl +++ b/cli/serverless/golang/templates/main_rx.tmpl @@ -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("-", "_")) diff --git a/cli/serverless/nodejs/runtime.go b/cli/serverless/nodejs/runtime.go index afe62c44e..7140d6b43 100644 --- a/cli/serverless/nodejs/runtime.go +++ b/cli/serverless/nodejs/runtime.go @@ -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) @@ -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 } @@ -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 } @@ -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() } diff --git a/cli/serverless/nodejs/serverless.go b/cli/serverless/nodejs/serverless.go index d8d0768f1..64149a64e 100644 --- a/cli/serverless/nodejs/serverless.go +++ b/cli/serverless/nodejs/serverless.go @@ -2,6 +2,8 @@ package nodejs import ( + "os" + "github.com/yomorun/yomo/cli/serverless" "github.com/yomorun/yomo/pkg/wrapper" ) @@ -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 diff --git a/pkg/wrapper/wrapper.go b/pkg/wrapper/wrapper.go index b3dfeac9f..20e9dd89c 100644 --- a/pkg/wrapper/wrapper.go +++ b/pkg/wrapper/wrapper.go @@ -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) @@ -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 } }()