Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e8dc13992 | |||
|
323617361d
|
|||
|
c57b75721e
|
|||
|
c3800bcf99
|
|||
|
86ef6e86bf
|
|||
|
752041a69d
|
@@ -30,6 +30,7 @@ func init() {
|
|||||||
rootCmd.AddCommand(bootstrap.AddServiceCommand())
|
rootCmd.AddCommand(bootstrap.AddServiceCommand())
|
||||||
rootCmd.AddCommand(bootstrap.GenerateProtoFile())
|
rootCmd.AddCommand(bootstrap.GenerateProtoFile())
|
||||||
rootCmd.AddCommand(bootstrap.InjectProtoFile())
|
rootCmd.AddCommand(bootstrap.InjectProtoFile())
|
||||||
|
rootCmd.AddCommand(bootstrap.AddProtoFormat())
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
54
coco_proto_format.go
Normal file
54
coco_proto_format.go
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
package bootstrap
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"github.com/emicklei/proto"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"gitter.top/sync/proto-contrib/pkg/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
|
||||||
|
}
|
||||||
@@ -5,23 +5,22 @@ import (
|
|||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"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 {
|
func Update() *cobra.Command {
|
||||||
var config Config
|
var config Config
|
||||||
var configPath string
|
var configPath string
|
||||||
|
var easyaml = NewEasYaml()
|
||||||
|
var rt = NewRuntime()
|
||||||
return &cobra.Command{
|
return &cobra.Command{
|
||||||
Use: "update",
|
Use: "update",
|
||||||
Short: "coco update",
|
Short: "coco update",
|
||||||
PreRun: func(cmd *cobra.Command, args []string) {
|
PreRun: func(cmd *cobra.Command, args []string) {
|
||||||
configPath = runtime.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", runtime.GetHomeDir()))
|
configPath = rt.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", rt.GetHomeDir()))
|
||||||
_ = yaml.Read(configPath, &config)
|
_ = easyaml.Read(configPath, &config)
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
client, err := giteaApi.NewClient("https://gitter.top")
|
client, err := NewGiteaClient("https://gitter.top")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("new gitea api failed: %v", err)
|
logrus.Errorf("new gitea api failed: %v", err)
|
||||||
return
|
return
|
||||||
@@ -38,13 +37,13 @@ func Update() *cobra.Command {
|
|||||||
|
|
||||||
// exec update
|
// exec update
|
||||||
repo := fmt.Sprintf("gitter.top/coco/bootstrap/coco/...@%s", commitID)
|
repo := fmt.Sprintf("gitter.top/coco/bootstrap/coco/...@%s", commitID)
|
||||||
if _, err := runtime.Exec("go", "install", repo); err != nil {
|
if _, err := rt.Exec("go", "install", repo); err != nil {
|
||||||
logrus.Errorf("update bootstrap failed: %v", err)
|
logrus.Errorf("update bootstrap failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
config.Version = commitID
|
config.Version = commitID
|
||||||
if err := yaml.Write(configPath, &config); err != nil {
|
if err := easyaml.Write(configPath, &config); err != nil {
|
||||||
logrus.Errorf("write config failed: %v", err)
|
logrus.Errorf("write config failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,11 +55,18 @@ func AddServiceCommand() *cobra.Command {
|
|||||||
return
|
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)
|
buf, err := gobuf.NewParser(pbPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Errorf("read proto file failed: %v", err)
|
logrus.Errorf("read proto file failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if buf.ExistService(svcName) {
|
if buf.ExistService(svcName) {
|
||||||
logrus.Errorf("router group name exist")
|
logrus.Errorf("router group name exist")
|
||||||
return
|
return
|
||||||
|
|||||||
12
go.mod
12
go.mod
@@ -3,20 +3,22 @@ module gitter.top/coco/bootstrap
|
|||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
code.gitea.io/sdk/gitea v0.17.1
|
||||||
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/coco/components v0.0.0-20230414170908-eb14d021db12
|
gitter.top/common/gobuf v0.0.4
|
||||||
gitter.top/common/gobuf v0.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
code.gitea.io/sdk/gitea v0.15.1 // indirect
|
github.com/davidmz/go-pageant v1.0.2 // indirect
|
||||||
github.com/emicklei/proto v1.13.2 // indirect
|
github.com/emicklei/proto v1.13.2 // indirect
|
||||||
github.com/hashicorp/go-version v1.2.1 // 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/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/spf13/pflag v1.0.5 // indirect
|
github.com/spf13/pflag v1.0.5 // indirect
|
||||||
gitter.top/sync/proto-contrib v0.15.0 // 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/sys v0.18.0 // indirect
|
||||||
google.golang.org/protobuf v1.33.0 // indirect
|
google.golang.org/protobuf v1.33.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ const templateApiservicesProto = `syntax = "proto3";
|
|||||||
|
|
||||||
package proto.v1;
|
package proto.v1;
|
||||||
|
|
||||||
option go_package = "project/gen;genv1";
|
option go_package = "{{.Name}}/gen;genv1";
|
||||||
|
|
||||||
|
|
||||||
// @route_group: true
|
// @route_group: true
|
||||||
// @base_url: /v1/file
|
// @base_url: /v1/file
|
||||||
// @gen_to: ./services/controller/v1/file_controller.go
|
// @gen_to: ./services/controller/v1/file_controller.go
|
||||||
// @rpc_to: ./services/rpc/v1/file_impl.go
|
// @rpc_to: ./services/microservice/v1/file_service.go
|
||||||
service FileService {
|
service FileService {
|
||||||
// @desc: 列表
|
// @desc: 列表
|
||||||
// @author: Young Xu
|
// @author: Young Xu
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ const templateProto = `syntax = "proto3";
|
|||||||
|
|
||||||
package proto.v1;
|
package proto.v1;
|
||||||
|
|
||||||
option go_package = "project/gen;genv1";
|
option go_package = "{{.Name}}/gen;genv1";
|
||||||
|
|
||||||
|
|
||||||
// @table_name: t_file
|
// @table_name: t_file
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
wire:
|
wire:
|
||||||
|
go mod tidy
|
||||||
cd gen/wire && wire
|
cd gen/wire && wire
|
||||||
|
|
||||||
ui:
|
ui:
|
||||||
@@ -24,32 +25,36 @@ api:
|
|||||||
go mod tidy && go build .
|
go mod tidy && go build .
|
||||||
./{{.Name}} api
|
./{{.Name}} api
|
||||||
|
|
||||||
|
build: gen wire ui api
|
||||||
|
|
||||||
|
run: gen wire ui api
|
||||||
|
./{{.Name}} api
|
||||||
|
|
||||||
all: gen wire ui api
|
all: gen wire ui api
|
||||||
|
|
||||||
|
docker:
|
||||||
|
podman build -t images.internal/{{.Name}}:latest .
|
||||||
|
podman push images.internal/{{.Name}}:latest
|
||||||
`
|
`
|
||||||
|
|
||||||
const templateDockerfile = `FROM images.local/golang:latest
|
const templateDockerfile = `FROM images.internal/devel:latest
|
||||||
|
|
||||||
WORKDIR /app
|
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
|
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 .
|
|
||||||
|
|
||||||
EXPOSE 38080
|
RUN make build
|
||||||
ENTRYPOINT [ "./filesaver", "api" ]
|
|
||||||
|
EXPOSE 38001
|
||||||
|
|
||||||
|
ENTRYPOINT [ "./{{.Name}}", "api" ]
|
||||||
`
|
`
|
||||||
|
|
||||||
const templateDockerCompose = `version: "3"
|
const templateDockerCompose = `version: "3"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
server:
|
server:
|
||||||
image: xuthus5/{{.Name}}:latest
|
image: images.internal/{{.Name}}:latest
|
||||||
container_name: {{.Name}}
|
container_name: {{.Name}}
|
||||||
restart: always
|
restart: always
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
109
util.go
109
util.go
@@ -1,6 +1,15 @@
|
|||||||
package bootstrap
|
package bootstrap
|
||||||
|
|
||||||
import "strings"
|
import (
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"code.gitea.io/sdk/gitea"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
// FirstUpper 字符串首字母大写
|
// FirstUpper 字符串首字母大写
|
||||||
func FirstUpper(s string) string {
|
func FirstUpper(s string) string {
|
||||||
@@ -9,3 +18,101 @@ func FirstUpper(s string) string {
|
|||||||
}
|
}
|
||||||
return strings.ToUpper(s[:1]) + s[1:]
|
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) GetLatestCommitID(owner, repoName string) (string, error) {
|
||||||
|
commits, _, err := c.client.ListRepoCommits(owner, repoName, gitea.ListCommitOptions{
|
||||||
|
ListOptions: gitea.ListOptions{
|
||||||
|
Page: 1,
|
||||||
|
PageSize: 1,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return commits[0].SHA, 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)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user