bootstrap/coco_update.go

55 lines
1.4 KiB
Go

package main
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
gitea_api "gitter.top/coco/components/gitea-api"
"gitter.top/coco/components/runtime"
"gitter.top/coco/components/yaml"
)
func cocoUpdate() *cobra.Command {
var config Config
var configPath string
return &cobra.Command{
Use: "update",
Short: "coco tool update",
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) {
client, err := gitea_api.NewClient("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@%s", commitID)
if output, err := runtime.Exec("go", "build", "-o", "coco", repo); err != nil {
logrus.Errorf("update coco failed: %v", err)
return
} else {
logrus.Infof("%s", output)
}
config.Version = commitID
if err := yaml.Write(configPath, &config); err != nil {
logrus.Errorf("write config failed: %v", err)
return
}
},
}
}