Compare commits

..

No commits in common. "master" and "v0.0.7" have entirely different histories.

5 changed files with 13 additions and 65 deletions

View File

@ -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 (还没实现)

View File

@ -10,7 +10,7 @@ import (
var (
template = "git@golib-ssh.gaore.com:GaoreGo/hertz_demo.git"
project string
branch string
branch = "master"
)
var rootCmd = &cobra.Command{

View File

@ -16,11 +16,12 @@ var createCmd = &cobra.Command{
},
}
func handleCreateCommand(args []string) {
if projectExists(project) {
log.Printf("Project %s already exists, use update", project)
os.Exit(1)
func init() {
createCmd.Flags().StringVarP(&project, "project", "p", "", "项目名称")
createCmd.MarkFlagRequired("project")
}
func handleCreateCommand(args []string) {
err := handleRemoteTemplate(template, branch, project)
if err != nil {
log.Printf("Error creating project: %s\n", err)
@ -29,16 +30,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()
}

View File

@ -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")
}

View File

@ -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
}