Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4259c77974 | |||
|
1143e0624c
|
@@ -5,22 +5,23 @@ 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 = rt.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", rt.GetHomeDir()))
|
configPath = runtime.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", runtime.GetHomeDir()))
|
||||||
_ = easyaml.Read(configPath, &config)
|
_ = yaml.Read(configPath, &config)
|
||||||
},
|
},
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
client, err := NewGiteaClient("https://gitter.top")
|
client, err := giteaApi.NewClient("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
|
||||||
@@ -37,13 +38,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 := rt.Exec("go", "install", repo); err != nil {
|
if _, err := runtime.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 := easyaml.Write(configPath, &config); err != nil {
|
if err := yaml.Write(configPath, &config); err != nil {
|
||||||
logrus.Errorf("write config failed: %v", err)
|
logrus.Errorf("write config failed: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
10
go.mod
10
go.mod
@@ -3,22 +3,20 @@ 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.1
|
gitter.top/common/gobuf v0.0.1
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/davidmz/go-pageant v1.0.2 // indirect
|
code.gitea.io/sdk/gitea v0.15.1 // indirect
|
||||||
github.com/emicklei/proto v1.13.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.2.1 // 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,7 +4,7 @@ const templateApiservicesProto = `syntax = "proto3";
|
|||||||
|
|
||||||
package proto.v1;
|
package proto.v1;
|
||||||
|
|
||||||
option go_package = "{{.Name}}/gen;genv1";
|
option go_package = "project/gen;genv1";
|
||||||
|
|
||||||
|
|
||||||
// @route_group: true
|
// @route_group: true
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ const templateProto = `syntax = "proto3";
|
|||||||
|
|
||||||
package proto.v1;
|
package proto.v1;
|
||||||
|
|
||||||
option go_package = "{{.Name}}/gen;genv1";
|
option go_package = "project/gen;genv1";
|
||||||
|
|
||||||
|
|
||||||
// @table_name: t_file
|
// @table_name: t_file
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ else
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
wire:
|
wire:
|
||||||
go mod tidy
|
|
||||||
cd gen/wire && wire
|
cd gen/wire && wire
|
||||||
|
|
||||||
ui:
|
ui:
|
||||||
@@ -25,36 +24,32 @@ 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.internal/devel:latest
|
const templateDockerfile = `FROM images.local/golang: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 .
|
||||||
|
|
||||||
RUN make build
|
EXPOSE 38080
|
||||||
|
ENTRYPOINT [ "./filesaver", "api" ]
|
||||||
EXPOSE 38001
|
|
||||||
|
|
||||||
ENTRYPOINT [ "./{{.Name}}", "api" ]
|
|
||||||
`
|
`
|
||||||
|
|
||||||
const templateDockerCompose = `version: "3"
|
const templateDockerCompose = `version: "3"
|
||||||
|
|
||||||
services:
|
services:
|
||||||
server:
|
server:
|
||||||
image: images.internal/{{.Name}}:latest
|
image: xuthus5/{{.Name}}:latest
|
||||||
container_name: {{.Name}}
|
container_name: {{.Name}}
|
||||||
restart: always
|
restart: always
|
||||||
volumes:
|
volumes:
|
||||||
|
|||||||
109
util.go
109
util.go
@@ -1,15 +1,6 @@
|
|||||||
package bootstrap
|
package bootstrap
|
||||||
|
|
||||||
import (
|
import "strings"
|
||||||
"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 {
|
||||||
@@ -18,101 +9,3 @@ 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