Skip to content

Commit

Permalink
refactor command template
Browse files Browse the repository at this point in the history
  • Loading branch information
venjiang committed Dec 26, 2024
1 parent 56548e8 commit 4cf32c0
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 33 deletions.
39 changes: 18 additions & 21 deletions cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ limitations under the License.
package cli

import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
"github.com/yomorun/yomo/cli/serverless/golang"
"github.com/yomorun/yomo/cli/template"
"github.com/yomorun/yomo/pkg/file"
"github.com/yomorun/yomo/pkg/log"
)
Expand All @@ -49,32 +50,28 @@ var initCmd = &cobra.Command{
name = strings.ReplaceAll(name, " ", "_")
// create app.go
fname := filepath.Join(name, defaultSFNSourceFile)
// sfn content template
var contentTmpl, testTmpl []byte
// sfn type
switch sfnType {
case "llm":
contentTmpl = golang.InitLLMTmpl
testTmpl = golang.InitLLMTestTmpl
case "normal":
contentTmpl = golang.InitTmpl
testTmpl = golang.InitTestTmpl
default:
log.WarningStatusEvent(os.Stdout, "The type of Stream Function is not supported, use the default type: llm")
contentTmpl = golang.InitLLMTmpl
testTmpl = golang.InitLLMTestTmpl

contentTmpl, err := template.GetContent("init", sfnType, "", false)
if err != nil {
log.FailureStatusEvent(os.Stdout, err.Error())
return
}

Check warning on line 57 in cli/init.go

View check run for this annotation

Codecov / codecov/patch

cli/init.go#L53-L57

Added lines #L53 - L57 were not covered by tests
if err := file.PutContents(fname, contentTmpl); err != nil {
log.FailureStatusEvent(os.Stdout, "Write stream function into app.go file failure with the error: %v", err)
log.FailureStatusEvent(os.Stdout, "Write stream function into %s file failure with the error: %v", fname, err)

Check warning on line 59 in cli/init.go

View check run for this annotation

Codecov / codecov/patch

cli/init.go#L59

Added line #L59 was not covered by tests
return
}

// create app_test.go
testName := filepath.Join(name, defaultSFNTestSourceFile)
if err := file.PutContents(testName, testTmpl); err != nil {
log.FailureStatusEvent(os.Stdout, "Write unittest tmpl into app_test.go file failure with the error: %v", err)
return
testTmpl, err := template.GetContent("init", sfnType, "", true)
if err != nil {
if !errors.Is(err, template.ErrUnsupportedTest) {
log.FailureStatusEvent(os.Stdout, err.Error())
return
}
} else {
if err := file.PutContents(testName, testTmpl); err != nil {
log.FailureStatusEvent(os.Stdout, "Write unittest tmpl into %s file failure with the error: %v", testName, err)
return
}

Check warning on line 74 in cli/init.go

View check run for this annotation

Codecov / codecov/patch

cli/init.go#L64-L74

Added lines #L64 - L74 were not covered by tests
}

// create .env
Expand Down
12 changes: 0 additions & 12 deletions cli/serverless/golang/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,6 @@ import (
//go:embed templates/main.tmpl
var MainFuncTmpl []byte

//go:embed templates/init.tmpl
var InitTmpl []byte

//go:embed templates/init_llm.tmpl
var InitLLMTmpl []byte

//go:embed templates/init_test.tmpl
var InitTestTmpl []byte

//go:embed templates/init_llm_test.tmpl
var InitLLMTestTmpl []byte

//go:embed templates/wasi_main.tmpl
var WasiMainFuncTmpl []byte

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
93 changes: 93 additions & 0 deletions cli/template/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package template

import (
"embed"
"errors"
"os"
"strings"
)

//go:embed go
var fs embed.FS

var (
ErrUnsupportedSfnType = errors.New("unsupported sfn type")
ErrorUnsupportedLang = errors.New("unsupported lang")
ErrUnsupportedTest = errors.New("unsupported test")
)

var (
SupportedSfnTypes = []string{"llm", "normal"}
SupportedLangs = []string{"go", "node"}
)

// get template content
func GetContent(command string, sfnType string, lang string, isTest bool) ([]byte, error) {
if command == "" {
command = "init"
}
sfnType, err := validateSfnType(sfnType)
if err != nil {
return nil, err
}
lang, err = validateLang(lang)
if err != nil {
return nil, err
}
sb := new(strings.Builder)
sb.WriteString(lang)
sb.WriteString("/")
sb.WriteString(command)
sb.WriteString("_")
sb.WriteString(sfnType)
if isTest {
sb.WriteString("_test")
}
sb.WriteString(".tmpl")

// valdiate the path exists
name := sb.String()
f, err := fs.Open(name)
if err != nil {
if os.IsNotExist(err) {
if isTest {
return nil, ErrUnsupportedTest
}
return nil, err

Check warning on line 56 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L25-L56

Added lines #L25 - L56 were not covered by tests
}
return nil, err

Check warning on line 58 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L58

Added line #L58 was not covered by tests
}
defer f.Close()
_, err = f.Stat()
if err != nil {
return nil, err
}

Check warning on line 64 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L60-L64

Added lines #L60 - L64 were not covered by tests

return fs.ReadFile(name)

Check warning on line 66 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L66

Added line #L66 was not covered by tests
}

func validateSfnType(sfnType string) (string, error) {
if sfnType == "" {
// default sfn type
return "llm", nil
}
for _, t := range SupportedSfnTypes {
if t == sfnType {
return sfnType, nil
}

Check warning on line 77 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L69-L77

Added lines #L69 - L77 were not covered by tests
}
return sfnType, ErrUnsupportedSfnType

Check warning on line 79 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L79

Added line #L79 was not covered by tests
}

func validateLang(lang string) (string, error) {
if lang == "" {
// default lang
return "go", nil
}
for _, l := range SupportedLangs {
if l == lang {
return lang, nil
}

Check warning on line 90 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L82-L90

Added lines #L82 - L90 were not covered by tests
}
return lang, ErrorUnsupportedLang

Check warning on line 92 in cli/template/template.go

View check run for this annotation

Codecov / codecov/patch

cli/template/template.go#L92

Added line #L92 was not covered by tests
}

0 comments on commit 4cf32c0

Please sign in to comment.