Compare commits

...

3 Commits

Author SHA1 Message Date
xuthus5 1342202230
chore: upgrade dependency 2024-04-20 01:49:24 +08:00
Young Xu 103fa7fe32
feat: new command pbf
fix: coco version command
2024-04-06 12:49:45 +08:00
xuthus5 323617361d
fix: panic when proto file not exist 2024-03-20 23:47:29 +08:00
8 changed files with 149 additions and 75 deletions

View File

@ -24,12 +24,13 @@ func init() {
PadLevelText: false,
QuoteEmptyFields: false,
})
rootCmd.AddCommand(bootstrap.Update())
rootCmd.AddCommand(bootstrap.AddVersionCommand())
rootCmd.AddCommand(bootstrap.CreateProject())
rootCmd.AddCommand(bootstrap.AddAPICommand())
rootCmd.AddCommand(bootstrap.AddServiceCommand())
rootCmd.AddCommand(bootstrap.GenerateProtoFile())
rootCmd.AddCommand(bootstrap.InjectProtoFile())
rootCmd.AddCommand(bootstrap.AddProtoFormat())
}
func main() {

View File

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

54
coco_proto_format.go Normal file
View File

@ -0,0 +1,54 @@
package bootstrap
import (
"bytes"
"github.com/emicklei/proto"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"gitter.top/common/protofmt"
"os"
)
func AddProtoFormat() *cobra.Command {
var protoFile string
cmd := &cobra.Command{
Use: "pbf",
Short: "protobuf format",
PreRun: func(_ *cobra.Command, args []string) {
if protoFile == "" && len(args) != 0 {
protoFile = args[0]
}
stat, err := os.Stat(protoFile)
if err != nil {
logrus.Fatalf("read proto file %s failed: %v", protoFile, err)
return
}
if stat.IsDir() {
logrus.Fatalf("proto file can not be directory")
return
}
},
Run: func(cmd *cobra.Command, args []string) {
file, err := os.Open(protoFile)
if err != nil {
logrus.Errorf("format proto file %s failed: %v", protoFile, err)
return
}
buf := new(bytes.Buffer)
parser := proto.NewParser(file)
parser.Filename(protoFile)
def, err := parser.Parse()
if err != nil {
logrus.Errorf("parse proto file %s failed: %v", protoFile, err)
return
}
protofmt.NewFormatter(buf, " ").Format(def)
if err := os.WriteFile(protoFile, buf.Bytes(), os.ModePerm); err != nil {
logrus.Errorf("write proto file %s failed: %v", protoFile, err)
return
}
},
}
cmd.Flags().StringVar(&protoFile, "path", "", "proto file path")
return cmd
}

View File

@ -1,54 +0,0 @@
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!")
},
}
}

69
coco_version.go Normal file
View File

@ -0,0 +1,69 @@
package bootstrap
import (
"fmt"
"time"
"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.Format(time.DateTime))
},
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.Format(time.DateTime))
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", false, "update coco version")
return cmd
}

View File

@ -1,7 +1,6 @@
package bootstrap
import (
"errors"
"os"
"github.com/sirupsen/logrus"
@ -56,17 +55,16 @@ func AddServiceCommand() *cobra.Command {
return
}
err := gobuf.CreateFile(pbPath)
if err != nil {
logrus.Errorf("read proto file failed (when create new file): %v", err)
return
}
buf, err := gobuf.NewParser(pbPath)
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
logrus.Errorf("read proto file failed: %v", err)
return
}
err := buf.CreateFile(pbPath)
if err != nil {
logrus.Errorf("read proto file failed (when create new file): %v", err)
return
}
logrus.Errorf("read proto file failed: %v", err)
return
}
if buf.ExistService(svcName) {

15
go.mod
View File

@ -1,24 +1,27 @@
module gitter.top/coco/bootstrap
go 1.20
go 1.21
toolchain go1.21.8
require (
code.gitea.io/sdk/gitea v0.17.1
code.gitea.io/sdk/gitea v0.18.0
github.com/emicklei/proto v1.13.2
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
gitter.top/common/gobuf v0.0.3
gitter.top/common/gobuf v0.0.4
gitter.top/common/protofmt v0.0.1
gopkg.in/yaml.v3 v3.0.1
)
require (
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/hashicorp/go-version v1.6.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gitter.top/sync/proto-contrib v0.15.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/sys v0.19.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

View File

@ -31,7 +31,7 @@ func NewGiteaClient(addr string) (*GiteaClient, error) {
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{
ListOptions: gitea.ListOptions{
Page: 1,
@ -39,9 +39,9 @@ func (c *GiteaClient) GetLatestCommitID(owner, repoName string) (string, error)
},
})
if err != nil {
return "", err
return nil, err
}
return commits[0].SHA, nil
return commits[0], nil
}
type EasYaml struct {