Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

interface/cli/starport/cmd: made type command newable #70

Merged
merged 3 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion interface/cli/starport/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func New() *cobra.Command {
Short: "A tool for scaffolding out Cosmos applications",
}
c.AddCommand(NewApp())
c.AddCommand(typedCmd)
c.AddCommand(NewTyped())
c.AddCommand(NewServe())
c.AddCommand(addCmd)
c.Flags().BoolP("toggle", "t", false, "Help message for toggle")
Expand Down
76 changes: 39 additions & 37 deletions interface/cli/starport/cmd/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,44 +10,46 @@ import (
"github.com/tendermint/starport/templates/typed"
)

func init() {
typedCmd.Flags().StringVarP(&appPath, "path", "p", "", "path of the app")
func NewTyped() *cobra.Command {
c := &cobra.Command{
Use: "type [typeName] [field1] [field2] ...",
Short: "Generates CRUD actions for type",
Args: cobra.MinimumNArgs(1),
Run: typedHandler,
}
c.Flags().StringVarP(&appPath, "path", "p", "", "path of the app")
return c
}

var typedCmd = &cobra.Command{
Use: "type [typeName] [field1] [field2] ...",
Short: "Generates CRUD actions for type",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
appName, modulePath := getAppAndModule(appPath)
var fields []typed.Field
for _, f := range args[1:] {
fs := strings.Split(f, ":")
name := fs[0]
var datatype string
acceptedTypes := map[string]bool{
"string": true,
"bool": true,
"int": true,
"float": true,
}
if len(fs) == 2 && acceptedTypes[fs[1]] {
datatype = fs[1]
} else {
datatype = "string"
}
field := typed.Field{Name: name, Datatype: datatype}
fields = append(fields, field)
func typedHandler(cmd *cobra.Command, args []string) {
appName, modulePath := getAppAndModule(appPath)
var fields []typed.Field
for _, f := range args[1:] {
fs := strings.Split(f, ":")
name := fs[0]
var datatype string
acceptedTypes := map[string]bool{
"string": true,
"bool": true,
"int": true,
"float": true,
}
g, _ := typed.New(&typed.Options{
ModulePath: modulePath,
AppName: appName,
TypeName: args[0],
Fields: fields,
})
run := genny.WetRunner(context.Background())
run.With(g)
run.Run()
fmt.Printf("\n🎉 Created a type `%[1]v`.\n\n", args[0])
},
if len(fs) == 2 && acceptedTypes[fs[1]] {
datatype = fs[1]
} else {
datatype = "string"
}
field := typed.Field{Name: name, Datatype: datatype}
fields = append(fields, field)
}
g, _ := typed.New(&typed.Options{
ModulePath: modulePath,
AppName: appName,
TypeName: args[0],
Fields: fields,
})
run := genny.WetRunner(context.Background())
run.With(g)
run.Run()
fmt.Printf("\n🎉 Created a type `%[1]v`.\n\n", args[0])
}