69 lines
1.8 KiB
Go
69 lines
1.8 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func AddVersionCommand() *cobra.Command {
|
|
var config Config
|
|
var configPath string
|
|
var update bool
|
|
var easyaml = NewEasYaml()
|
|
var rt = NewRuntime()
|
|
cmd := &cobra.Command{
|
|
Use: "version",
|
|
Short: "coco version",
|
|
PreRun: func(cmd *cobra.Command, args []string) {
|
|
if !update && len(args) != 0 {
|
|
u := args[0]
|
|
if u == "update" {
|
|
update = true
|
|
}
|
|
}
|
|
configPath = rt.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", rt.GetHomeDir()))
|
|
_ = easyaml.Read(configPath, &config)
|
|
logrus.Infof("current version: %s, release at: %s", config.Version, config.CommitAt)
|
|
},
|
|
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
|
|
}
|
|
commit, err := client.GetLatestCommit("coco", "bootstrap")
|
|
if err != nil {
|
|
logrus.Errorf("get coco vcs info failed: %v", err)
|
|
return
|
|
}
|
|
if commit.SHA == config.Version {
|
|
logrus.Infof("version is latest!")
|
|
return
|
|
}
|
|
|
|
var commitID = commit.SHA[:8]
|
|
logrus.Infof("latest version: %s, release at: %s", commitID, commit.CommitMeta.Created)
|
|
|
|
if update {
|
|
repo := fmt.Sprintf("gitter.top/coco/bootstrap/coco/...@%s", commitID)
|
|
if _, err := rt.Exec("go", "install", repo); err != nil {
|
|
logrus.Errorf("update coco failed: %v", err)
|
|
return
|
|
}
|
|
|
|
config.Version = commitID
|
|
config.CommitAt = commit.CommitMeta.Created
|
|
if err := easyaml.Write(configPath, &config); err != nil {
|
|
logrus.Errorf("write config failed: %v", err)
|
|
return
|
|
}
|
|
logrus.Infof("update success!")
|
|
}
|
|
},
|
|
}
|
|
cmd.Flags().BoolVar(&update, "update", true, "update coco version")
|
|
return cmd
|
|
}
|