fix: coco version command

This commit is contained in:
Young Xu 2024-04-06 12:39:35 +08:00
parent 7e8dc13992
commit f656fdfce8
6 changed files with 47 additions and 27 deletions

View File

@ -24,7 +24,7 @@ func init() {
PadLevelText: false, PadLevelText: false,
QuoteEmptyFields: false, QuoteEmptyFields: false,
}) })
rootCmd.AddCommand(bootstrap.Update()) rootCmd.AddCommand(bootstrap.AddVersionCommand())
rootCmd.AddCommand(bootstrap.CreateProject()) rootCmd.AddCommand(bootstrap.CreateProject())
rootCmd.AddCommand(bootstrap.AddAPICommand()) rootCmd.AddCommand(bootstrap.AddAPICommand())
rootCmd.AddCommand(bootstrap.AddServiceCommand()) rootCmd.AddCommand(bootstrap.AddServiceCommand())

View File

@ -1,5 +1,8 @@
package bootstrap package bootstrap
import "time"
type Config struct { type Config struct {
Version string `yaml:"version"` Version string `yaml:"version"`
CommitAt time.Time `yaml:"commit_at"`
} }

View File

@ -5,7 +5,7 @@ import (
"github.com/emicklei/proto" "github.com/emicklei/proto"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"gitter.top/sync/proto-contrib/pkg/protofmt" "gitter.top/common/protofmt"
"os" "os"
) )

View File

@ -7,17 +7,25 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func Update() *cobra.Command { func AddVersionCommand() *cobra.Command {
var config Config var config Config
var configPath string var configPath string
var update bool
var easyaml = NewEasYaml() var easyaml = NewEasYaml()
var rt = NewRuntime() var rt = NewRuntime()
return &cobra.Command{ cmd := &cobra.Command{
Use: "update", Use: "version",
Short: "coco update", Short: "coco version",
PreRun: func(cmd *cobra.Command, args []string) { 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())) configPath = rt.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", rt.GetHomeDir()))
_ = easyaml.Read(configPath, &config) _ = easyaml.Read(configPath, &config)
logrus.Infof("current version: %s, release at: %s", config.Version, config.CommitAt)
}, },
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
client, err := NewGiteaClient("https://gitter.top") client, err := NewGiteaClient("https://gitter.top")
@ -25,30 +33,36 @@ func Update() *cobra.Command {
logrus.Errorf("new gitea api failed: %v", err) logrus.Errorf("new gitea api failed: %v", err)
return return
} }
commitID, err := client.GetLatestCommitID("coco", "bootstrap") commit, err := client.GetLatestCommit("coco", "bootstrap")
if err != nil { if err != nil {
logrus.Errorf("get coco bootstrap info failed: %v", err) logrus.Errorf("get coco vcs info failed: %v", err)
return return
} }
if commitID == config.Version { if commit.SHA == config.Version {
logrus.Infof("version is latest!") logrus.Infof("version is latest!")
return return
} }
// exec update var commitID = commit.SHA[:8]
repo := fmt.Sprintf("gitter.top/coco/bootstrap/coco/...@%s", commitID) logrus.Infof("latest version: %s, release at: %s", commitID, commit.CommitMeta.Created)
if _, err := rt.Exec("go", "install", repo); err != nil {
logrus.Errorf("update bootstrap failed: %v", err)
return
}
config.Version = commitID if update {
if err := easyaml.Write(configPath, &config); err != nil { repo := fmt.Sprintf("gitter.top/coco/bootstrap/coco/...@%s", commitID)
logrus.Errorf("write config failed: %v", err) if _, err := rt.Exec("go", "install", repo); err != nil {
return logrus.Errorf("update coco failed: %v", err)
} return
}
logrus.Infof("update success!") 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
} }

7
go.mod
View File

@ -1,18 +1,21 @@
module gitter.top/coco/bootstrap module gitter.top/coco/bootstrap
go 1.20 go 1.21
toolchain go1.21.8
require ( require (
code.gitea.io/sdk/gitea v0.17.1 code.gitea.io/sdk/gitea v0.17.1
github.com/emicklei/proto v1.13.2
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0 github.com/spf13/cobra v1.8.0
gitter.top/common/gobuf v0.0.4 gitter.top/common/gobuf v0.0.4
gitter.top/common/protofmt v0.0.1
gopkg.in/yaml.v3 v3.0.1 gopkg.in/yaml.v3 v3.0.1
) )
require ( require (
github.com/davidmz/go-pageant v1.0.2 // indirect github.com/davidmz/go-pageant v1.0.2 // indirect
github.com/emicklei/proto v1.13.2 // indirect
github.com/go-fed/httpsig v1.1.0 // indirect github.com/go-fed/httpsig v1.1.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect github.com/hashicorp/go-version v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect

View File

@ -31,7 +31,7 @@ func NewGiteaClient(addr string) (*GiteaClient, error) {
return &GiteaClient{client: client}, nil return &GiteaClient{client: client}, nil
} }
func (c *GiteaClient) GetLatestCommitID(owner, repoName string) (string, error) { func (c *GiteaClient) GetLatestCommit(owner, repoName string) (*gitea.Commit, error) {
commits, _, err := c.client.ListRepoCommits(owner, repoName, gitea.ListCommitOptions{ commits, _, err := c.client.ListRepoCommits(owner, repoName, gitea.ListCommitOptions{
ListOptions: gitea.ListOptions{ ListOptions: gitea.ListOptions{
Page: 1, Page: 1,
@ -39,9 +39,9 @@ func (c *GiteaClient) GetLatestCommitID(owner, repoName string) (string, error)
}, },
}) })
if err != nil { if err != nil {
return "", err return nil, err
} }
return commits[0].SHA, nil return commits[0], nil
} }
type EasYaml struct { type EasYaml struct {