1 Commits

Author SHA1 Message Date
752041a69d release: new version 2024-03-19 23:22:14 +08:00
6 changed files with 140 additions and 27 deletions

View File

@@ -5,23 +5,22 @@ import (
"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
var easyaml = NewEasYaml()
var rt = NewRuntime()
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)
configPath = rt.ReplaceEachSlash(fmt.Sprintf("%s/.coco.yaml", rt.GetHomeDir()))
_ = easyaml.Read(configPath, &config)
},
Run: func(cmd *cobra.Command, args []string) {
client, err := giteaApi.NewClient("https://gitter.top")
client, err := NewGiteaClient("https://gitter.top")
if err != nil {
logrus.Errorf("new gitea api failed: %v", err)
return
@@ -38,13 +37,13 @@ func Update() *cobra.Command {
// exec update
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)
return
}
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)
return
}

10
go.mod
View File

@@ -3,20 +3,22 @@ module gitter.top/coco/bootstrap
go 1.20
require (
code.gitea.io/sdk/gitea v0.17.1
github.com/sirupsen/logrus v1.9.3
github.com/spf13/cobra v1.8.0
gitter.top/coco/components v0.0.0-20230414170908-eb14d021db12
gitter.top/common/gobuf v0.0.1
gopkg.in/yaml.v3 v3.0.1
)
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/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/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
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

View File

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

View File

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

View File

@@ -15,6 +15,7 @@ else
endif
wire:
go mod tidy
cd gen/wire && wire
ui:
@@ -24,32 +25,36 @@ 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.local/golang:latest
const templateDockerfile = `FROM images.internal/devel: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 .
EXPOSE 38080
ENTRYPOINT [ "./filesaver", "api" ]
RUN make build
EXPOSE 38001
ENTRYPOINT [ "./{{.Name}}", "api" ]
`
const templateDockerCompose = `version: "3"
services:
server:
image: xuthus5/{{.Name}}:latest
image: images.internal/{{.Name}}:latest
container_name: {{.Name}}
restart: always
volumes:

109
util.go
View File

@@ -1,6 +1,15 @@
package bootstrap
import "strings"
import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"code.gitea.io/sdk/gitea"
"gopkg.in/yaml.v3"
)
// FirstUpper 字符串首字母大写
func FirstUpper(s string) string {
@@ -9,3 +18,101 @@ 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) 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)
}