bootstrap/coco_update.go

56 lines
1.4 KiB
Go
Raw Permalink Normal View History

2023-01-01 15:46:41 +00:00
package bootstrap
2023-01-01 15:31:51 +00:00
import (
"fmt"
2023-01-01 15:46:41 +00:00
2023-01-01 15:31:51 +00:00
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
2023-01-01 15:46:41 +00:00
giteaApi "gitter.top/coco/components/gitea-api"
2023-01-01 15:31:51 +00:00
"gitter.top/coco/components/runtime"
"gitter.top/coco/components/yaml"
)
2023-01-01 15:46:41 +00:00
func Update() *cobra.Command {
2023-01-01 15:31:51 +00:00
var config Config
var configPath string
return &cobra.Command{
Use: "update",
2023-01-01 15:46:41 +00:00
Short: "coco update",
2023-01-01 15:31:51 +00:00
PreRun: func(cmd *cobra.Command, args []string) {
configPath = runtime.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", runtime.GetHomeDir()))
_ = yaml.Read(configPath, &config)
},
Run: func(cmd *cobra.Command, args []string) {
2023-01-01 15:46:41 +00:00
client, err := giteaApi.NewClient("https://gitter.top")
2023-01-01 15:31:51 +00: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 15:46:41 +00:00
repo := fmt.Sprintf("gitter.top/coco/bootstrap/coco/...@%s", commitID)
if _, err := runtime.Exec("go", "install", repo); err != nil {
logrus.Errorf("update bootstrap failed: %v", err)
2023-01-01 15:31:51 +00:00
return
}
config.Version = commitID
if err := yaml.Write(configPath, &config); err != nil {
logrus.Errorf("write config failed: %v", err)
return
}
2023-01-01 15:46:41 +00:00
logrus.Infof("update success!")
2023-01-01 15:31:51 +00:00
},
}
}