bootstrap/coco_update.go

55 lines
1.3 KiB
Go
Raw Normal View History

2023-01-01 23:46:41 +08:00
package bootstrap
2023-01-01 23:31:51 +08:00
import (
"fmt"
2023-01-01 23:46:41 +08:00
2023-01-01 23:31:51 +08:00
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
2023-01-01 23:46:41 +08:00
func Update() *cobra.Command {
2023-01-01 23:31:51 +08:00
var config Config
var configPath string
2023-01-01 23:46:41 +08:00
var easyaml = NewEasYaml()
var rt = NewRuntime()
2023-01-01 23:31:51 +08:00
return &cobra.Command{
Use: "update",
2023-01-01 23:46:41 +08:00
Short: "coco update",
2023-01-01 23:31:51 +08:00
PreRun: func(cmd *cobra.Command, args []string) {
2023-01-01 23:46:41 +08:00
configPath = rt.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", rt.GetHomeDir()))
_ = easyaml.Read(configPath, &config)
2023-01-01 23:31:51 +08:00
},
Run: func(cmd *cobra.Command, args []string) {
2023-01-01 23:46:41 +08:00
client, err := NewGiteaClient("https://gitter.top")
2023-01-01 23:31:51 +08:00
if err != nil {
logrus.Errorf("new gitea api failed: %v", err)
return
}
commitID, err := client.GetLatestCommitID("coco", "bootstrap")
if err != nil {
logrus.Errorf("get coco bootstrap info failed: %v", err)
return
}
if commitID == config.Version {
logrus.Infof("version is latest!")
return
}
// exec update
2023-01-01 23:46:41 +08:00
repo := fmt.Sprintf("gitter.top/coco/bootstrap/coco/...@%s", commitID)
if _, err := rt.Exec("go", "install", repo); err != nil {
logrus.Errorf("update bootstrap failed: %v", err)
2023-01-01 23:31:51 +08:00
return
}
config.Version = commitID
2023-01-01 23:46:41 +08:00
if err := easyaml.Write(configPath, &config); err != nil {
2023-01-01 23:31:51 +08:00
logrus.Errorf("write config failed: %v", err)
return
}
2023-01-01 23:46:41 +08:00
logrus.Infof("update success!")
2023-01-01 23:31:51 +08:00
},
}
}