Compare commits
No commits in common. "master" and "v0.0.6" have entirely different histories.
@ -2,7 +2,7 @@
|
||||
以golib.gaore.com/GaoreGo/hertz_demo为模板的hertz框架脚手架
|
||||
|
||||
#### 安装:
|
||||
go install golib.gaore.com/GaoreGo/hertz_scaffold@latest
|
||||
go install golib.gaore.com/GaoreGo/hertz_scaffold
|
||||
|
||||
|
||||
#### 命令行参数:
|
||||
@ -17,13 +17,12 @@
|
||||
Flags:
|
||||
-h, --help help for create
|
||||
-p, --project string 项目名称
|
||||
-t, --tag string 指定tag (不指定默认拉取master)
|
||||
|
||||
|
||||
#### 创建项目:
|
||||
hertz_scaffold create -p hertz.gaore.com
|
||||
|
||||
#### 更新项目(注意,更新项目会覆盖本地项目,请谨慎操作):
|
||||
hertz_scaffold update -p hertz.gaore.com -t 1.0.0
|
||||
#### 更新项目:
|
||||
hertz_scaffold update -p hertz.gaore.com (还没实现)
|
||||
|
||||
|
@ -1,7 +1,6 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
"os"
|
||||
@ -10,7 +9,7 @@ import (
|
||||
var (
|
||||
template = "git@golib-ssh.gaore.com:GaoreGo/hertz_demo.git"
|
||||
project string
|
||||
branch string
|
||||
branch = "master"
|
||||
)
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
@ -18,11 +17,6 @@ var rootCmd = &cobra.Command{
|
||||
Short: "hertz框架脚手架",
|
||||
Long: `以golib.gaore.com/GaoreGo/hertz_demo为模板的脚手架`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
cmd.Help()
|
||||
} else {
|
||||
fmt.Println("Invalid command")
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -16,11 +16,11 @@ var createCmd = &cobra.Command{
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
createCmd.Flags().StringVarP(&project, "project", "p", "", "项目名称")
|
||||
}
|
||||
|
||||
func handleCreateCommand(args []string) {
|
||||
if projectExists(project) {
|
||||
log.Printf("Project %s already exists, use update", project)
|
||||
os.Exit(1)
|
||||
}
|
||||
err := handleRemoteTemplate(template, branch, project)
|
||||
if err != nil {
|
||||
log.Printf("Error creating project: %s\n", err)
|
||||
@ -29,16 +29,3 @@ func handleCreateCommand(args []string) {
|
||||
fmt.Printf("Project %s created successfully!\n", project)
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
initFlags(createCmd)
|
||||
}
|
||||
|
||||
// projectExists 检查项目路径是否已经存在
|
||||
func projectExists(projectPath string) bool {
|
||||
info, err := os.Stat(projectPath)
|
||||
if os.IsNotExist(err) {
|
||||
return false
|
||||
}
|
||||
return info.IsDir()
|
||||
}
|
||||
|
@ -1,9 +1,7 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
@ -40,13 +38,12 @@ func handleRemoteTemplate(templateRepo, branch, projectPath string) (err error)
|
||||
defer os.RemoveAll(tempDir)
|
||||
|
||||
// 克隆模板仓库
|
||||
cloneCmd := exec.Command("git", "clone", "--branch", branch, templateRepo, tempDir)
|
||||
cloneCmd := exec.Command("git", "clone", "-b", branch, templateRepo, tempDir)
|
||||
cloneCmd.Stdout = io.Discard
|
||||
var stderr bytes.Buffer
|
||||
cloneCmd.Stderr = &stderr
|
||||
cloneCmd.Stderr = os.Stderr
|
||||
|
||||
if err = cloneCmd.Run(); err != nil {
|
||||
return fmt.Errorf("error cloning template repository: %s\n%s", err, extractError(stderr.String()))
|
||||
return fmt.Errorf("error cloning template repository: %s", err)
|
||||
}
|
||||
|
||||
return copyTemplate(tempDir, projectPath)
|
||||
@ -120,23 +117,3 @@ func copyAndReplaceFile(src, dist string, mode os.FileMode, replacements map[str
|
||||
_, err = targetFile.WriteString(newContent)
|
||||
return
|
||||
}
|
||||
|
||||
// extractError 解析并提取实际的错误信息
|
||||
func extractError(stderr string) string {
|
||||
lines := strings.Split(stderr, "\n")
|
||||
var errorLines []string
|
||||
for _, line := range lines {
|
||||
// 过滤掉非错误信息
|
||||
if strings.HasPrefix(line, "fatal:") || strings.Contains(line, "error:") {
|
||||
errorLines = append(errorLines, line)
|
||||
}
|
||||
}
|
||||
return strings.Join(errorLines, "\n")
|
||||
}
|
||||
|
||||
// initFlags 初始化通用的命令行参数
|
||||
func initFlags(cmd *cobra.Command) {
|
||||
cmd.Flags().StringVarP(&project, "project", "p", "", "项目名称")
|
||||
cmd.Flags().StringVarP(&branch, "tag", "t", "master", "指定tag")
|
||||
cmd.MarkFlagRequired("project")
|
||||
}
|
||||
|
@ -3,8 +3,6 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
var updateCmd = &cobra.Command{
|
||||
@ -12,20 +10,6 @@ var updateCmd = &cobra.Command{
|
||||
Short: "更新项目",
|
||||
Long: `Update the current project`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
handleUpdateCommand(args)
|
||||
fmt.Println("还没做")
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
initFlags(updateCmd)
|
||||
}
|
||||
|
||||
func handleUpdateCommand(args []string) {
|
||||
err := handleRemoteTemplate(template, branch, project)
|
||||
if err != nil {
|
||||
log.Printf("Error update project: %s\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Printf("Project %s update successfully!\n", project)
|
||||
return
|
||||
}
|
||||
|
19
vendor/github.com/spf13/pflag/flag.go
generated
vendored
19
vendor/github.com/spf13/pflag/flag.go
generated
vendored
@ -27,23 +27,32 @@ unaffected.
|
||||
Define flags using flag.String(), Bool(), Int(), etc.
|
||||
|
||||
This declares an integer flag, -flagname, stored in the pointer ip, with type *int.
|
||||
|
||||
var ip = flag.Int("flagname", 1234, "help message for flagname")
|
||||
|
||||
If you like, you can bind the flag to a variable using the Var() functions.
|
||||
|
||||
var flagvar int
|
||||
func init() {
|
||||
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
|
||||
}
|
||||
|
||||
Or you can create custom flags that satisfy the Value interface (with
|
||||
pointer receivers) and couple them to flag parsing by
|
||||
|
||||
flag.Var(&flagVal, "name", "help message for flagname")
|
||||
|
||||
For such flags, the default value is just the initial value of the variable.
|
||||
|
||||
After all flags are defined, call
|
||||
|
||||
flag.Parse()
|
||||
|
||||
to parse the command line into the defined flags.
|
||||
|
||||
Flags may then be used directly. If you're using the flags themselves,
|
||||
they are all pointers; if you bind to variables, they're values.
|
||||
|
||||
fmt.Println("ip has value ", *ip)
|
||||
fmt.Println("flagvar has value ", flagvar)
|
||||
|
||||
@ -54,22 +63,26 @@ The arguments are indexed from 0 through flag.NArg()-1.
|
||||
The pflag package also defines some new functions that are not in flag,
|
||||
that give one-letter shorthands for flags. You can use these by appending
|
||||
'P' to the name of any function that defines a flag.
|
||||
|
||||
var ip = flag.IntP("flagname", "f", 1234, "help message")
|
||||
var flagvar bool
|
||||
func init() {
|
||||
flag.BoolVarP(&flagvar, "boolname", "b", true, "help message")
|
||||
}
|
||||
flag.VarP(&flagval, "varname", "v", "help message")
|
||||
|
||||
Shorthand letters can be used with single dashes on the command line.
|
||||
Boolean shorthand flags can be combined with other shorthand flags.
|
||||
|
||||
Command line flag syntax:
|
||||
|
||||
--flag // boolean flags only
|
||||
--flag=x
|
||||
|
||||
Unlike the flag package, a single dash before an option means something
|
||||
different than a double dash. Single dashes signify a series of shorthand
|
||||
letters for flags. All but the last shorthand letter must be boolean flags.
|
||||
|
||||
// boolean flags
|
||||
-f
|
||||
-abc
|
||||
@ -927,9 +940,9 @@ func (f *FlagSet) usage() {
|
||||
}
|
||||
}
|
||||
|
||||
//--unknown (args will be empty)
|
||||
//--unknown --next-flag ... (args will be --next-flag ...)
|
||||
//--unknown arg ... (args will be arg ...)
|
||||
// --unknown (args will be empty)
|
||||
// --unknown --next-flag ... (args will be --next-flag ...)
|
||||
// --unknown arg ... (args will be arg ...)
|
||||
func stripUnknownFlagValue(args []string) []string {
|
||||
if len(args) == 0 {
|
||||
//--unknown
|
||||
|
12
vendor/github.com/spf13/pflag/string_slice.go
generated
vendored
12
vendor/github.com/spf13/pflag/string_slice.go
generated
vendored
@ -98,8 +98,11 @@ func (f *FlagSet) GetStringSlice(name string) ([]string, error) {
|
||||
// The argument p points to a []string variable in which to store the value of the flag.
|
||||
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
||||
// For example:
|
||||
//
|
||||
// --ss="v1,v2" --ss="v3"
|
||||
//
|
||||
// will result in
|
||||
//
|
||||
// []string{"v1", "v2", "v3"}
|
||||
func (f *FlagSet) StringSliceVar(p *[]string, name string, value []string, usage string) {
|
||||
f.VarP(newStringSliceValue(value, p), name, "", usage)
|
||||
@ -114,8 +117,11 @@ func (f *FlagSet) StringSliceVarP(p *[]string, name, shorthand string, value []s
|
||||
// The argument p points to a []string variable in which to store the value of the flag.
|
||||
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
||||
// For example:
|
||||
//
|
||||
// --ss="v1,v2" --ss="v3"
|
||||
//
|
||||
// will result in
|
||||
//
|
||||
// []string{"v1", "v2", "v3"}
|
||||
func StringSliceVar(p *[]string, name string, value []string, usage string) {
|
||||
CommandLine.VarP(newStringSliceValue(value, p), name, "", usage)
|
||||
@ -130,8 +136,11 @@ func StringSliceVarP(p *[]string, name, shorthand string, value []string, usage
|
||||
// The return value is the address of a []string variable that stores the value of the flag.
|
||||
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
||||
// For example:
|
||||
//
|
||||
// --ss="v1,v2" --ss="v3"
|
||||
//
|
||||
// will result in
|
||||
//
|
||||
// []string{"v1", "v2", "v3"}
|
||||
func (f *FlagSet) StringSlice(name string, value []string, usage string) *[]string {
|
||||
p := []string{}
|
||||
@ -150,8 +159,11 @@ func (f *FlagSet) StringSliceP(name, shorthand string, value []string, usage str
|
||||
// The return value is the address of a []string variable that stores the value of the flag.
|
||||
// Compared to StringArray flags, StringSlice flags take comma-separated value as arguments and split them accordingly.
|
||||
// For example:
|
||||
//
|
||||
// --ss="v1,v2" --ss="v3"
|
||||
//
|
||||
// will result in
|
||||
//
|
||||
// []string{"v1", "v2", "v3"}
|
||||
func StringSlice(name string, value []string, usage string) *[]string {
|
||||
return CommandLine.StringSliceP(name, "", value, usage)
|
||||
|
Loading…
Reference in New Issue
Block a user