实现更新功能
This commit is contained in:
parent
576ef0d31e
commit
d2bf98a710
@ -2,7 +2,7 @@
|
|||||||
以golib.gaore.com/GaoreGo/hertz_demo为模板的hertz框架脚手架
|
以golib.gaore.com/GaoreGo/hertz_demo为模板的hertz框架脚手架
|
||||||
|
|
||||||
#### 安装:
|
#### 安装:
|
||||||
go install golib.gaore.com/GaoreGo/hertz_scaffold
|
go install golib.gaore.com/GaoreGo/hertz_scaffold@latest
|
||||||
|
|
||||||
|
|
||||||
#### 命令行参数:
|
#### 命令行参数:
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
var (
|
var (
|
||||||
template = "git@golib-ssh.gaore.com:GaoreGo/hertz_demo.git"
|
template = "git@golib-ssh.gaore.com:GaoreGo/hertz_demo.git"
|
||||||
project string
|
project string
|
||||||
branch = "master"
|
branch string
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
|
@ -16,12 +16,11 @@ var createCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
|
||||||
createCmd.Flags().StringVarP(&project, "project", "p", "", "项目名称")
|
|
||||||
createCmd.MarkFlagRequired("project")
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleCreateCommand(args []string) {
|
func handleCreateCommand(args []string) {
|
||||||
|
if projectExists(project) {
|
||||||
|
log.Printf("Project %s already exists, use update", project)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
err := handleRemoteTemplate(template, branch, project)
|
err := handleRemoteTemplate(template, branch, project)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error creating project: %s\n", err)
|
log.Printf("Error creating project: %s\n", err)
|
||||||
@ -30,3 +29,16 @@ func handleCreateCommand(args []string) {
|
|||||||
fmt.Printf("Project %s created successfully!\n", project)
|
fmt.Printf("Project %s created successfully!\n", project)
|
||||||
return
|
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,7 +1,9 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
@ -38,12 +40,13 @@ func handleRemoteTemplate(templateRepo, branch, projectPath string) (err error)
|
|||||||
defer os.RemoveAll(tempDir)
|
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.Stdout = io.Discard
|
||||||
cloneCmd.Stderr = os.Stderr
|
var stderr bytes.Buffer
|
||||||
|
cloneCmd.Stderr = &stderr
|
||||||
|
|
||||||
if err = cloneCmd.Run(); err != nil {
|
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)
|
return copyTemplate(tempDir, projectPath)
|
||||||
@ -117,3 +120,23 @@ func copyAndReplaceFile(src, dist string, mode os.FileMode, replacements map[str
|
|||||||
_, err = targetFile.WriteString(newContent)
|
_, err = targetFile.WriteString(newContent)
|
||||||
return
|
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,6 +3,8 @@ package cmd
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
var updateCmd = &cobra.Command{
|
var updateCmd = &cobra.Command{
|
||||||
@ -10,6 +12,20 @@ var updateCmd = &cobra.Command{
|
|||||||
Short: "更新项目",
|
Short: "更新项目",
|
||||||
Long: `Update the current project`,
|
Long: `Update the current project`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user