实现更新功能

This commit is contained in:
许 洋 2024-08-09 14:57:41 +08:00
parent 576ef0d31e
commit d2bf98a710
5 changed files with 62 additions and 11 deletions

View File

@ -2,7 +2,7 @@
以golib.gaore.com/GaoreGo/hertz_demo为模板的hertz框架脚手架
#### 安装:
go install golib.gaore.com/GaoreGo/hertz_scaffold
go install golib.gaore.com/GaoreGo/hertz_scaffold@latest
#### 命令行参数:

View File

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

View File

@ -16,12 +16,11 @@ var createCmd = &cobra.Command{
},
}
func init() {
createCmd.Flags().StringVarP(&project, "project", "p", "", "项目名称")
createCmd.MarkFlagRequired("project")
}
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)
@ -30,3 +29,16 @@ 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,7 +1,9 @@
package cmd
import (
"bytes"
"fmt"
"github.com/spf13/cobra"
"io"
"io/fs"
"log"
@ -38,12 +40,13 @@ func handleRemoteTemplate(templateRepo, branch, projectPath string) (err error)
defer os.RemoveAll(tempDir)
// 克隆模板仓库
cloneCmd := exec.Command("git", "clone", "-b", branch, templateRepo, tempDir)
cloneCmd := exec.Command("git", "clone", "--branch", branch, templateRepo, tempDir)
cloneCmd.Stdout = io.Discard
cloneCmd.Stderr = os.Stderr
var stderr bytes.Buffer
cloneCmd.Stderr = &stderr
if err = cloneCmd.Run(); err != nil {
return fmt.Errorf("error cloning template repository: %s", err)
return fmt.Errorf("error cloning template repository: %s\n%s", err, extractError(stderr.String()))
}
return copyTemplate(tempDir, projectPath)
@ -117,3 +120,23 @@ 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,6 +3,8 @@ package cmd
import (
"fmt"
"github.com/spf13/cobra"
"log"
"os"
)
var updateCmd = &cobra.Command{
@ -10,6 +12,20 @@ var updateCmd = &cobra.Command{
Short: "更新项目",
Long: `Update the current project`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("还没做")
handleUpdateCommand(args)
},
}
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
}