bootstrap/coco_update.go

55 lines
1.3 KiB
Go

package bootstrap
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func Update() *cobra.Command {
var config Config
var configPath string
var easyaml = NewEasYaml()
var rt = NewRuntime()
return &cobra.Command{
Use: "update",
Short: "coco update",
PreRun: func(cmd *cobra.Command, args []string) {
configPath = rt.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", rt.GetHomeDir()))
_ = easyaml.Read(configPath, &config)
},
Run: func(cmd *cobra.Command, args []string) {
client, err := NewGiteaClient("https://gitter.top")
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
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)
return
}
config.Version = commitID
if err := easyaml.Write(configPath, &config); err != nil {
logrus.Errorf("write config failed: %v", err)
return
}
logrus.Infof("update success!")
},
}
}