Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
dist
node_modules
docs/package-lock.json
.omc
.omc
cmd/jzero/jzero
49 changes: 43 additions & 6 deletions cmd/jzero/internal/command/add/add.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package add

import (
"fmt"

"github.com/spf13/cobra"

"github.com/jzero-io/jzero/cmd/jzero/internal/command/add/addapi"
"github.com/jzero-io/jzero/cmd/jzero/internal/command/add/addproto"
"github.com/jzero-io/jzero/cmd/jzero/internal/command/add/addsql"
"github.com/jzero-io/jzero/cmd/jzero/internal/config"
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/console"
)

// addCmd represents the add command
Expand All @@ -19,27 +23,30 @@ var addApiCmd = &cobra.Command{
Use: "api",
Short: `Add api`,
RunE: func(cmd *cobra.Command, args []string) error {
return addapi.Run(args)
return runAddStage("api", args, addapi.Run)
},
SilenceUsage: true,
SilenceUsage: true,
SilenceErrors: true,
}

var addProtoCmd = &cobra.Command{
Use: "proto",
Short: `Add proto`,
RunE: func(cmd *cobra.Command, args []string) error {
return addproto.Run(args)
return runAddStage("proto", args, addproto.Run)
},
SilenceUsage: true,
SilenceUsage: true,
SilenceErrors: true,
}

var addSqlCmd = &cobra.Command{
Use: "sql",
Short: `Add sql`,
RunE: func(cmd *cobra.Command, args []string) error {
return addsql.Run(args)
return runAddStage("sql", args, addsql.Run)
},
SilenceUsage: true,
SilenceUsage: true,
SilenceErrors: true,
}

func GetCommand() *cobra.Command {
Expand All @@ -50,3 +57,33 @@ func GetCommand() *cobra.Command {
addCmd.AddCommand(addSqlCmd)
return addCmd
}

func runAddStage(kind string, args []string, fn func([]string) (string, error)) error {
target, err := fn(args)
if config.C.Add.Output != "file" || config.C.Quiet {
return err
}

title := console.Green("Add") + " " + console.Yellow(kind)
fmt.Printf("%s\n", console.BoxHeader("", title))

if err != nil {
if target != "" {
fmt.Printf("%s\n", console.BoxErrorItem(target))
}
for _, line := range console.NormalizeErrorLines(err.Error()) {
fmt.Printf("%s\n", console.BoxDetailItem(line))
}
fmt.Printf("%s\n\n", console.BoxErrorFooter())
if config.C.Quiet {
return err
}
return console.MarkRenderedError(err)
}

if target != "" {
fmt.Printf("%s\n", console.BoxItem(target))
}
fmt.Printf("%s\n\n", console.BoxSuccessFooter())
return nil
}
18 changes: 10 additions & 8 deletions cmd/jzero/internal/command/add/addapi/addapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/templatex"
)

func Run(args []string) error {
func Run(args []string) (string, error) {
baseDir := filepath.Join("desc", "api")

apiName := args[0]
Expand All @@ -24,31 +24,33 @@ func Run(args []string) error {
apiName = strings.TrimSuffix(apiName, ".api")
}

target := filepath.Join(baseDir, apiName+".api")

// fix https://github.com/jzero-io/jzero/issues/405.
// For jzero, each api file, the server name can be different.
template, err := templatex.ParseTemplate(filepath.Join("api", "template.api.tpl"), map[string]any{
"Service": stringx.ToCamel(apiName),
"Group": apiName,
}, embeded.ReadTemplateFile(filepath.Join("api", "template.api.tpl")))
if err != nil {
return err
return target, err
}

if config.C.Add.Output == "file" {
if filex.FileExists(filepath.Join(baseDir, apiName+".api")) {
return fmt.Errorf("%s already exists", apiName)
if filex.FileExists(target) {
return target, fmt.Errorf("%s already exists", apiName)
}

_ = os.MkdirAll(filepath.Dir(filepath.Join(baseDir, apiName)), 0o755)

err = os.WriteFile(filepath.Join(baseDir, apiName+".api"), template, 0o644)
err = os.WriteFile(target, template, 0o644)
if err != nil {
return err
return target, err
}

// format
return format.ApiFormatByPath(filepath.Join(baseDir, apiName+".api"), false)
return target, format.ApiFormatByPath(target, false)
}
fmt.Println(string(template))
return nil
return target, nil
}
18 changes: 10 additions & 8 deletions cmd/jzero/internal/command/add/addproto/addproto.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/templatex"
)

func Run(args []string) error {
func Run(args []string) (string, error) {
baseDir := filepath.Join("desc", "proto")

protoName := args[0]
Expand All @@ -23,6 +23,8 @@ func Run(args []string) error {
protoName = strings.TrimSuffix(protoName, ".proto")
}

target := filepath.Join(baseDir, protoName+".proto")

frameType, _ := desc.GetFrameType()
if frameType == "" {
frameType = "rpc"
Expand All @@ -35,22 +37,22 @@ func Run(args []string) error {
"Service": stringx.ToCamel(protoName),
}, embeded.ReadTemplateFile(filepath.Join(frameType, "template.proto.tpl")))
if err != nil {
return err
return target, err
}

if config.C.Add.Output == "file" {
if filex.FileExists(filepath.Join(baseDir, protoName+".proto")) {
return fmt.Errorf("%s already exists", protoName)
if filex.FileExists(target) {
return target, fmt.Errorf("%s already exists", protoName)
}

_ = os.MkdirAll(filepath.Dir(filepath.Join(baseDir, protoName)), 0o755)

err = os.WriteFile(filepath.Join(baseDir, protoName+".proto"), template, 0o644)
err = os.WriteFile(target, template, 0o644)
if err != nil {
return err
return target, err
}
return nil
return target, nil
}
fmt.Println(string(template))
return nil
return target, nil
}
18 changes: 10 additions & 8 deletions cmd/jzero/internal/command/add/addsql/addsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/jzero-io/jzero/cmd/jzero/internal/pkg/templatex"
)

func Run(args []string) error {
func Run(args []string) (string, error) {
baseDir := filepath.Join("desc", "sql")

sqlName := args[0]
Expand All @@ -21,26 +21,28 @@ func Run(args []string) error {
sqlName = strings.TrimSuffix(sqlName, ".sql")
}

target := filepath.Join(baseDir, sqlName+".sql")

template, err := templatex.ParseTemplate(filepath.Join("model", "template.sql.tpl"), map[string]any{
"Name": sqlName,
}, embeded.ReadTemplateFile(filepath.Join("model", "template.sql.tpl")))
if err != nil {
return err
return target, err
}

if config.C.Add.Output == "file" {
if filex.FileExists(filepath.Join(baseDir, sqlName+".sql")) {
return fmt.Errorf("%s already exists", sqlName)
if filex.FileExists(target) {
return target, fmt.Errorf("%s already exists", sqlName)
}

_ = os.MkdirAll(filepath.Dir(filepath.Join(baseDir, sqlName)), 0o755)

err = os.WriteFile(filepath.Join(baseDir, sqlName+".sql"), template, 0o644)
err = os.WriteFile(target, template, 0o644)
if err != nil {
return err
return target, err
}
return nil
return target, nil
}
fmt.Println(string(template))
return nil
return target, nil
}
Loading
Loading