-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
linyyyang
committed
Sep 3, 2021
1 parent
1278e76
commit 911fde7
Showing
15 changed files
with
579 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.ginny | ||
.ginny.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package command | ||
|
||
import ( | ||
"github.com/gorillazer/ginny-cli/ginny/handle" | ||
"github.com/gorillazer/ginny-cli/ginny/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(handleCmd) | ||
} | ||
|
||
var handleCmd = &cobra.Command{ | ||
Use: "handle", | ||
Short: "Create handle file", | ||
Long: "Create handle file from template", | ||
PreRunE: func(cmd *cobra.Command, args []string) (err error) { | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
if err := CheckArgs(args); err != nil { | ||
return err | ||
} | ||
// 获取参数 | ||
handleName := args[0] | ||
|
||
if err := handle.CreateHandle(handleName); err != nil { | ||
return err | ||
} | ||
|
||
util.Info("Create new handle file success!") | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package command | ||
|
||
import ( | ||
"github.com/gorillazer/ginny-cli/ginny/handle" | ||
"github.com/gorillazer/ginny-cli/ginny/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
protoCmd.Flags().BoolP("validate", "v", false, "Added support for parameter verification") | ||
rootCmd.AddCommand(protoCmd) | ||
} | ||
|
||
var protoCmd = &cobra.Command{ | ||
Use: "proto", | ||
Short: "Create proto file", | ||
Long: "Create proto file from template", | ||
PreRunE: func(cmd *cobra.Command, args []string) (err error) { | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
if err := CheckArgs(args); err != nil { | ||
return err | ||
} | ||
// 获取参数 | ||
serviceName := args[0] | ||
flags := cmd.Flags() | ||
validate, err := flags.GetBool("validate") | ||
if err != nil { | ||
return err | ||
} | ||
if err := handle.CreateProto(serviceName, validate); err != nil { | ||
return err | ||
} | ||
|
||
util.Info("Create new proto file success!") | ||
util.Info("You can modify the proto file, and then execute `make proto` to generate pb code.") | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package command | ||
|
||
import ( | ||
"github.com/gorillazer/ginny-cli/ginny/handle" | ||
"github.com/gorillazer/ginny-cli/ginny/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
repoCmd.Flags().StringArrayP("database", "d", []string{}, "Define the database used by the project, support mysql、mongo、redis") | ||
rootCmd.AddCommand(repoCmd) | ||
} | ||
|
||
var repoCmd = &cobra.Command{ | ||
Use: "repo", | ||
Short: "Create repository file", | ||
Long: "Create repository file from template", | ||
PreRunE: func(cmd *cobra.Command, args []string) (err error) { | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
if err := CheckArgs(args); err != nil { | ||
return err | ||
} | ||
// 获取参数 | ||
repoName := args[0] | ||
flags := cmd.Flags() | ||
database, err := flags.GetStringArray("database") | ||
if err != nil { | ||
return err | ||
} | ||
if err := handle.CreateRepo(repoName, database); err != nil { | ||
return err | ||
} | ||
|
||
util.Info("Create new repository file success!") | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package command | ||
|
||
import ( | ||
"github.com/gorillazer/ginny-cli/ginny/handle" | ||
"github.com/gorillazer/ginny-cli/ginny/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(serviceCmd) | ||
} | ||
|
||
var serviceCmd = &cobra.Command{ | ||
Use: "service", | ||
Short: "Create service file", | ||
Long: "Create service file from template", | ||
PreRunE: func(cmd *cobra.Command, args []string) (err error) { | ||
return nil | ||
}, | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
if err := CheckArgs(args); err != nil { | ||
return err | ||
} | ||
// 获取参数 | ||
serviceName := args[0] | ||
|
||
if err := handle.CreateService(serviceName); err != nil { | ||
return err | ||
} | ||
|
||
util.Info("Create new service file success!") | ||
return nil | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package handle | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/fatih/structs" | ||
"github.com/gorillazer/ginny-cli/ginny/options" | ||
"github.com/gorillazer/ginny-cli/ginny/util" | ||
"github.com/iancoleman/strcase" | ||
) | ||
|
||
func CreateHandle(handleName string) error { | ||
conf, err := GetProjectInfo() | ||
if err != nil { | ||
util.Error("Failed to get project info, ", err.Error()) | ||
return err | ||
} | ||
|
||
tmpPath := fmt.Sprintf("%s/%s", conf.ProjectPath, options.TempPath) | ||
if err := PullTemplate(tmpPath, options.ComponentTemplateRepo); err != nil { | ||
return err | ||
} | ||
|
||
srcFile := fmt.Sprintf("%s/handlers/test.go", tmpPath) | ||
dstFile := fmt.Sprintf("%s/internal/handlers/%s.go", conf.ProjectPath, handleName) | ||
if util.Exists(dstFile) { | ||
return errors.New("File already exists and overwriting is not allowed") | ||
} | ||
if err := util.CopyFile(srcFile, dstFile); err != nil { | ||
return err | ||
} | ||
// replace handle | ||
caseName := strcase.ToCamel(handleName) | ||
r := &options.ReplaceKeywords{ | ||
APP_NAME: conf.ProjectName, | ||
MODULE_NAME: conf.ProjectModule, | ||
HANDLE_NAME: caseName, | ||
} | ||
|
||
// replace provider | ||
providerFile := fmt.Sprintf("%s/internal/handlers/provider.go", conf.ProjectPath) | ||
if !util.Exists(providerFile) { | ||
if err := util.CopyFile(fmt.Sprintf("%s/handlers/provider.go", tmpPath), providerFile); err != nil { | ||
return err | ||
} | ||
} | ||
m := structs.Map(r) | ||
m[options.HandleReplaceAnchor[1]] = options.HandleReplaceAnchorValue[1]([]string{handleName, caseName}) | ||
m[options.HandleReplaceAnchor[2]] = options.HandleReplaceAnchorValue[2]([]string{caseName}) | ||
|
||
if err := ReplaceFileKeyword([]string{dstFile, providerFile}, m); err != nil { | ||
return err | ||
} | ||
|
||
if err := ExecCommand(conf.ProjectPath, "go", "mod", "tidy"); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.