2 Commits

Author SHA1 Message Date
4259c77974 chore: update module 2024-03-11 00:49:54 +08:00
1143e0624c release: new project template 2024-01-20 01:14:20 +08:00
11 changed files with 81 additions and 277 deletions

View File

@@ -24,13 +24,12 @@ func init() {
PadLevelText: false,
QuoteEmptyFields: false,
})
rootCmd.AddCommand(bootstrap.AddVersionCommand())
rootCmd.AddCommand(bootstrap.Update())
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,8 +1,5 @@
package bootstrap
import "time"
type Config struct {
Version string `yaml:"version"`
CommitAt time.Time `yaml:"commit_at"`
Version string `yaml:"version"`
}

View File

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

55
coco_update.go Normal file
View File

@@ -0,0 +1,55 @@
package bootstrap
import (
"fmt"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
giteaApi "gitter.top/coco/components/gitea-api"
"gitter.top/coco/components/runtime"
"gitter.top/coco/components/yaml"
)
func Update() *cobra.Command {
var config Config
var configPath string
return &cobra.Command{
Use: "update",
Short: "coco 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 := giteaApi.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/coco/...@%s", commitID)
if _, err := runtime.Exec("go", "install", repo); err != nil {
logrus.Errorf("update bootstrap failed: %v", err)
return
}
config.Version = commitID
if err := yaml.Write(configPath, &config); err != nil {
logrus.Errorf("write config failed: %v", err)
return
}
logrus.Infof("update success!")
},
}
}

View File

@@ -1,69 +0,0 @@
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

@@ -55,18 +55,11 @@ 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 {
logrus.Errorf("read proto file failed: %v", err)
return
}
if buf.ExistService(svcName) {
logrus.Errorf("router group name exist")
return

21
go.mod
View File

@@ -1,27 +1,22 @@
module gitter.top/coco/bootstrap
go 1.21
toolchain go1.21.8
go 1.20
require (
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.4
gitter.top/common/protofmt v0.0.1
gopkg.in/yaml.v3 v3.0.1
gitter.top/coco/components v0.0.0-20230414170908-eb14d021db12
gitter.top/common/gobuf v0.0.1
)
require (
github.com/davidmz/go-pageant v1.0.2 // indirect
github.com/go-fed/httpsig v1.1.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
code.gitea.io/sdk/gitea v0.15.1 // indirect
github.com/emicklei/proto v1.13.2 // indirect
github.com/hashicorp/go-version v1.2.1 // 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.22.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/sys v0.18.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

@@ -4,13 +4,13 @@ const templateApiservicesProto = `syntax = "proto3";
package proto.v1;
option go_package = "{{.Name}}/gen;genv1";
option go_package = "project/gen;genv1";
// @route_group: true
// @base_url: /v1/file
// @gen_to: ./services/controller/v1/file_controller.go
// @rpc_to: ./services/microservice/v1/file_service.go
// @rpc_to: ./services/rpc/v1/file_impl.go
service FileService {
// @desc: 列表
// @author: Young Xu

View File

@@ -4,7 +4,7 @@ const templateProto = `syntax = "proto3";
package proto.v1;
option go_package = "{{.Name}}/gen;genv1";
option go_package = "project/gen;genv1";
// @table_name: t_file

View File

@@ -15,7 +15,6 @@ else
endif
wire:
go mod tidy
cd gen/wire && wire
ui:
@@ -25,36 +24,32 @@ api:
go mod tidy && go build .
./{{.Name}} api
build: gen wire ui api
run: gen wire ui api
./{{.Name}} api
all: gen wire ui api
docker:
podman build -t images.internal/{{.Name}}:latest .
podman push images.internal/{{.Name}}:latest
`
const templateDockerfile = `FROM images.internal/devel:latest
const templateDockerfile = `FROM images.local/golang:latest
WORKDIR /app
RUN go install gitter.top/coco/protoc-gen-coco@latest &&\
go install github.com/golang/protobuf/protoc-gen-go@latest &&\
go install github.com/bufbuild/buf/cmd/...@latest
COPY . /app
RUN npm install ts-proto
RUN buf generate --exclude-path node_modules
RUN cd webui && npm install && npm run build &&\
cd .. && go mod tidy && go build .
RUN make build
EXPOSE 38001
ENTRYPOINT [ "./{{.Name}}", "api" ]
EXPOSE 38080
ENTRYPOINT [ "./filesaver", "api" ]
`
const templateDockerCompose = `version: "3"
services:
server:
image: images.internal/{{.Name}}:latest
image: xuthus5/{{.Name}}:latest
container_name: {{.Name}}
restart: always
volumes:

109
util.go
View File

@@ -1,15 +1,6 @@
package bootstrap
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"code.gitea.io/sdk/gitea"
"gopkg.in/yaml.v3"
)
import "strings"
// FirstUpper 字符串首字母大写
func FirstUpper(s string) string {
@@ -18,101 +9,3 @@ func FirstUpper(s string) string {
}
return strings.ToUpper(s[:1]) + s[1:]
}
type GiteaClient struct {
client *gitea.Client
}
func NewGiteaClient(addr string) (*GiteaClient, error) {
client, err := gitea.NewClient(addr)
if err != nil {
return nil, err
}
return &GiteaClient{client: client}, nil
}
func (c *GiteaClient) GetLatestCommit(owner, repoName string) (*gitea.Commit, error) {
commits, _, err := c.client.ListRepoCommits(owner, repoName, gitea.ListCommitOptions{
ListOptions: gitea.ListOptions{
Page: 1,
PageSize: 1,
},
})
if err != nil {
return nil, err
}
return commits[0], nil
}
type EasYaml struct {
}
func NewEasYaml() *EasYaml {
return &EasYaml{}
}
func (y *EasYaml) Read(filename string, ptr interface{}) error {
body, err := os.ReadFile(filename)
if err != nil {
return err
}
if err := yaml.Unmarshal(body, ptr); err != nil {
return err
}
return nil
}
func (y *EasYaml) Write(filename string, ptr interface{}) error {
body, err := yaml.Marshal(ptr)
if err != nil {
return err
}
return os.WriteFile(filename, body, os.ModePerm)
}
type Runtime struct {
}
func NewRuntime() *Runtime {
return &Runtime{}
}
func (r *Runtime) Exec(cmd string, args ...string) (output string, err error) {
command := exec.Command(cmd, args...)
body, err := command.Output()
if err != nil {
return "", err
}
return string(body), nil
}
type GOOS string
const (
Linux GOOS = "linux"
Windows GOOS = "windows"
Darwin GOOS = "darwin"
Freebsd GOOS = "freebsd"
)
// GetGOOS get GOOS value
func (r *Runtime) GetGOOS() GOOS {
return GOOS(runtime.GOOS)
}
// GetHomeDir get home directory
func (r *Runtime) GetHomeDir() string {
switch r.GetGOOS() {
case Linux:
return os.Getenv("HOME")
case Windows:
return os.Getenv("USERPROFILE")
default:
return os.Getenv("HOME")
}
}
// ReplaceEachSlash replacing each slash ('/') character
func (r *Runtime) ReplaceEachSlash(filename string) string {
return filepath.FromSlash(filename)
}