diff --git a/cli/cli.go b/cli/cli.go index a5092a166..d148f2e54 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -17,11 +17,12 @@ 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" + defaultSFNSourceFile = "app.go" + defaultSFNSourceTSFile = "app.ts" + defaultSFNTestSourceFile = "app_test.go" + defaultSFNTestSourceTSFile = "app_test.ts" + defaultSFNCompliedFile = "sfn.yomo" + defaultSFNWASIFile = "sfn.wasm" ) // GetRootPath get root path @@ -91,3 +92,23 @@ func checkOptions(opts *serverless.Options) error { opts.Filename = f return nil } + +// DefaultSFNSourceFile returns the default source file name for the given language +func DefaultSFNSourceFile(lang string) string { + switch lang { + case "node": + return defaultSFNSourceTSFile + default: + return defaultSFNSourceFile + } +} + +// DefaultSFNTestSourceFile returns the default test source file name +func DefaultSFNTestSourceFile(lang string) string { + switch lang { + case "node": + return defaultSFNTestSourceTSFile + default: + return defaultSFNTestSourceFile + } +} diff --git a/cli/init.go b/cli/init.go index b5a936933..bb4662ee7 100644 --- a/cli/init.go +++ b/cli/init.go @@ -28,8 +28,11 @@ import ( "github.com/yomorun/yomo/pkg/log" ) -var name string -var sfnType string +var ( + name string + sfnType string + lang string +) // initCmd represents the init command var initCmd = &cobra.Command{ @@ -48,9 +51,9 @@ var initCmd = &cobra.Command{ log.PendingStatusEvent(os.Stdout, "Initializing the Stream Function...") name = strings.ReplaceAll(name, " ", "_") - // create app.go - fname := filepath.Join(name, defaultSFNSourceFile) - contentTmpl, err := template.GetContent("init", sfnType, "", false) + // create app source file + fname := filepath.Join(name, DefaultSFNSourceFile(lang)) + contentTmpl, err := template.GetContent("init", sfnType, lang, false) if err != nil { log.FailureStatusEvent(os.Stdout, err.Error()) return @@ -59,9 +62,9 @@ var initCmd = &cobra.Command{ log.FailureStatusEvent(os.Stdout, "Write stream function into %s file failure with the error: %v", fname, err) return } - // create app_test.go - testName := filepath.Join(name, defaultSFNTestSourceFile) - testTmpl, err := template.GetContent("init", sfnType, "", true) + // create app test file + testName := filepath.Join(name, DefaultSFNTestSourceFile(lang)) + testTmpl, err := template.GetContent("init", sfnType, lang, true) if err != nil { if !errors.Is(err, template.ErrUnsupportedTest) { log.FailureStatusEvent(os.Stdout, err.Error()) @@ -84,7 +87,7 @@ var initCmd = &cobra.Command{ log.SuccessStatusEvent(os.Stdout, "Congratulations! You have initialized the stream function successfully.") log.InfoStatusEvent(os.Stdout, "You can enjoy the YoMo Stream Function via the command: ") log.InfoStatusEvent(os.Stdout, "\tStep 1: cd %s && yomo build", name) - log.InfoStatusEvent(os.Stdout, "\tStep 2: yomo run sfn.yomo") + log.InfoStatusEvent(os.Stdout, "\tStep 2: yomo run") }, } @@ -93,4 +96,5 @@ func init() { initCmd.Flags().StringVarP(&name, "name", "n", "", "The name of Stream Function") initCmd.Flags().StringVarP(&sfnType, "type", "t", "llm", "The type of Stream Function, support normal and llm") + initCmd.Flags().StringVarP(&lang, "lang", "l", "go", "The language of Stream Function, support go and node") } diff --git a/cli/template/node/init_llm.tmpl b/cli/template/node/init_llm.tmpl new file mode 100644 index 000000000..8c1f42bd9 --- /dev/null +++ b/cli/template/node/init_llm.tmpl @@ -0,0 +1,21 @@ +export const description = 'Get the current weather for `city_name`' + +export const tag = 0x33 + +// For jsonschema in TypeScript, see: https://github.com/YousefED/typescript-json-schema +export type Argument = { + /** + * The name of the city to be queried + */ + city_name: string; +} + +async function getWeather(city_name: string) { + let tempraure = Math.floor(Math.random() * 41) + console.log(`get weather for ${city_name} with temperature ${tempraure}°C`) + return { city_name: city_name, temperature: tempraure } +} +export async function handler(args: Argument) { + const result = await getWeather(args.city_name) + return result +} diff --git a/cli/template/template.go b/cli/template/template.go index 27745a283..724b7d96d 100644 --- a/cli/template/template.go +++ b/cli/template/template.go @@ -8,12 +8,14 @@ import ( ) //go:embed go +//go:embed node var fs embed.FS var ( ErrUnsupportedSfnType = errors.New("unsupported sfn type") ErrorUnsupportedLang = errors.New("unsupported lang") ErrUnsupportedTest = errors.New("unsupported test") + ErrUnsupportedFeature = errors.New("unsupported feature") ) var ( @@ -53,7 +55,7 @@ func GetContent(command string, sfnType string, lang string, isTest bool) ([]byt if isTest { return nil, ErrUnsupportedTest } - return nil, err + return nil, ErrUnsupportedFeature } return nil, err }