Compare commits

..

6 Commits

Author SHA1 Message Date
icechen d984d78b03 feat: 新增appName 2022-01-11 04:06:18 +08:00
icechen 0540fcd17f fix: yaml tag 2022-01-11 00:03:14 +08:00
icechen 7f8bb65be3 feat:
1. diff
2. namespace
2022-01-10 20:26:14 +08:00
icechen 5826eef7bf feat: read bool 更新 2022-01-10 03:37:21 +08:00
icechen 9137abdabf feat: 增加app 2022-01-09 22:23:28 +08:00
icechen 540e84ef09 feat: 增加app 2022-01-09 22:13:56 +08:00
22 changed files with 273 additions and 83 deletions

25
api.go
View File

@ -2,9 +2,10 @@ package main
import (
"fmt"
"os"
"git.icechen.cn/pkg/wujian_develop_tool/config"
"git.icechen.cn/pkg/wujian_develop_tool/util"
"os"
"git.icechen.cn/pkg/wujian_develop_tool/template/api/deploy"
@ -23,7 +24,7 @@ func ActionListApi(c *cli.Context) error {
}
color.Green("API应用列表:")
for _, api := range config.Config.HasApi {
color.Green("\t%s", api.Name)
color.Green("\t%s.%s", api.Name, api.Namespace)
color.Green("\t\t路径:\t%s", api.Root)
color.Green("\t\t类型:\t%s", api.Type)
color.Green("\t\t端口:\t%s", api.Port)
@ -59,11 +60,21 @@ func ActionNewApi(c *cli.Context) error {
appList = append(appList, appName)
}
for _, newApp := range appList {
namespace := ""
for {
namespace = util.ReadLineWithMessage(fmt.Sprintf("请输入%s所属的项目(namespace): ", newApp))
if namespace != "" {
break
} else {
color.Red("项目名称不能为空!")
}
}
color.Green("正在创建[%s]API应用...", newApp)
app := config.Config.Api.GetApp(newApp)
app := config.Config.Api.GetApp(namespace, newApp)
app = config.Api{
Name: newApp,
Root: "app/api/" + newApp,
Namespace: namespace,
Name: newApp,
Root: fmt.Sprintf("app/%s/api/%s", namespace, newApp),
}
if util.ExistDir(app.Root) && !util.ReadBoolWithMessage(app.Root+"目录已存在,是否要覆盖app文件目录(y/n): ") {
@ -96,12 +107,12 @@ func ActionNewApi(c *cli.Context) error {
app.Host = util.ReadLineWithMessage("请输入API应用请求的域名(host,如:api.seamlesser.com,建议为空): ")
app.Path = util.ReadLineWithMessage("请输入API应用请求的路由(path,如:/uwe/core/?(.*),建议为空): ")
err = deploy.GenDeploy(config.Config.Name, app)
err = deploy.GenDeploy(app)
if err != nil {
return err
}
err = golang.GenDockerfile(newApp)
err = golang.GenDockerfile(app)
if err != nil {
return err
}

View File

@ -1,12 +1,11 @@
package config
import (
"git.icechen.cn/pkg/wujian_develop_tool/util"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"io/ioutil"
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
"git.icechen.cn/pkg/wujian_develop_tool/util"
)
var DefaultConfigFile = ".drone.yml"
@ -42,8 +41,9 @@ type ConfigFile struct {
type (
ApiList []Api
Api struct {
Namespace string `json:"namespace" yaml:"namespace"`
Name string `json:"name" yaml:"name"`
AliasName string `json:"alias_name" yaml:"aliasName"`
AliasName string `json:"alias_name" yaml:"alias_name"`
Root string `json:"root" yaml:"root"`
Type string `json:"type" yaml:"type"`
Port string `json:"port" yaml:"port"`
@ -55,8 +55,9 @@ type (
type (
ServiceList []Service
Service struct {
Namespace string `json:"namespace" yaml:"namespace"`
Name string `json:"name" yaml:"name"`
AliasName string `json:"alias_name" yaml:"aliasName"`
AliasName string `json:"alias_name" yaml:"alias_name"`
Root string `json:"root" yaml:"root"`
Type string `json:"type" yaml:"type"`
Port string `json:"port" yaml:"port"`
@ -80,7 +81,7 @@ func ReadConfig() error {
func (c *ConfigFile) AppendAPI(api Api) error {
has := false
for i, a := range c.Api {
if a.Name == api.Name {
if a.Name == api.Name && a.Namespace == api.Namespace {
c.Api[i] = api
has = true
break
@ -102,7 +103,7 @@ func (c *ConfigFile) AppendAPI(api Api) error {
func (c *ConfigFile) AppendService(service Service) error {
has := false
for i, s := range c.Service {
if s.Name == service.Name {
if s.Name == service.Name && s.Namespace == service.Namespace {
c.Service[i] = service
has = true
break
@ -140,9 +141,9 @@ al:
return hasApp
}
func (sl ApiList) GetApp(appName string) Api {
func (sl ApiList) GetApp(namespace, appName string) Api {
for _, api := range sl {
if api.Name == appName {
if api.Name == appName && api.Namespace == namespace {
return api
}
}
@ -168,9 +169,9 @@ sl:
return hasApp
}
func (sl ServiceList) GetApp(appName string) Service {
func (sl ServiceList) GetApp(namespace, appName string) Service {
for _, service := range sl {
if service.Name == appName {
if service.Name == appName && service.Namespace == namespace {
return service
}
}

4
go.mod
View File

@ -5,6 +5,7 @@ go 1.17
require (
github.com/drone/drone-go v1.7.1
github.com/fatih/color v1.13.0
github.com/sergi/go-diff v1.2.0
github.com/urfave/cli/v2 v2.3.0
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
@ -16,8 +17,9 @@ require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98 // indirect
golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

16
go.sum
View File

@ -44,6 +44,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
github.com/cpuguy83/go-md2man/v2 v2.0.1 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/drone/drone-go v1.7.1 h1:ZX+3Rs8YHUSUQ5mkuMLmm1zr1ttiiE2YGNxF3AnyDKw=
github.com/drone/drone-go v1.7.1/go.mod h1:fxCf9jAnXDZV1yDr0ckTuWd1intvcQwfJmTRpTZ1mXg=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
@ -122,14 +124,18 @@ github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
github.com/sergi/go-diff v1.2.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
@ -202,8 +208,8 @@ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98 h1:+6WJMRLHlD7X7frgp7TUZ36RnQzSf9wVVTNakEp+nqY=
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d h1:62NvYBuaanGXR2ZOfwDFkhhl6X1DUgf8qg3GuQvxZsE=
golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
@ -386,11 +392,15 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

View File

@ -2,9 +2,10 @@ package main
import (
"fmt"
"os"
"git.icechen.cn/pkg/wujian_develop_tool/config"
"git.icechen.cn/pkg/wujian_develop_tool/util"
"os"
"git.icechen.cn/pkg/wujian_develop_tool/template/service/deploy"
@ -23,7 +24,7 @@ func ActionListService(c *cli.Context) error {
}
color.Green("Service应用列表:")
for _, service := range config.Config.HasService {
color.Green("\t%s", service.Name)
color.Green("\t%s.%s", service.Name, service.Namespace)
color.Green("\t\t路径:\t%s", service.Root)
color.Green("\t\t类型:\t%s", service.Type)
color.Green("\t\t端口:\t%s", service.Port)
@ -59,11 +60,21 @@ func ActionNewService(c *cli.Context) error {
appList = append(appList, appName)
}
for _, newApp := range appList {
namespace := ""
for {
namespace = util.ReadLineWithMessage(fmt.Sprintf("请输入%s所属的项目(namespace): ", newApp))
if namespace != "" {
break
} else {
color.Red("项目名称不能为空!")
}
}
color.Green("正在创建[%s]Service应用...", newApp)
app := config.Config.Service.GetApp(newApp)
app := config.Config.Service.GetApp(namespace, newApp)
app = config.Service{
Name: newApp,
Root: "app/service/" + newApp,
Namespace: namespace,
Name: newApp,
Root: fmt.Sprintf("app/%s/service/%s", namespace, newApp),
}
if util.ExistDir(app.Root) && !util.ReadBoolWithMessage(app.Root+"目录已存在,是否要覆盖app文件目录(y/n): ") {
@ -94,12 +105,12 @@ func ActionNewService(c *cli.Context) error {
}
}
err = deploy.GenDeploy(config.Config.Name, app)
err = deploy.GenDeploy(app)
if err != nil {
return err
}
err = golang.GenDockerfile(newApp)
err = golang.GenDockerfile(app)
if err != nil {
return err
}

View File

@ -4,9 +4,9 @@ import (
"embed"
_ "embed"
"fmt"
"git.icechen.cn/pkg/wujian_develop_tool/config"
"os"
"git.icechen.cn/pkg/wujian_develop_tool/config"
"github.com/fatih/color"
"git.icechen.cn/pkg/wujian_develop_tool/util"
@ -15,7 +15,7 @@ import (
//go:embed "*"
var deployDir embed.FS
func GenDeploy(namespace string, api config.Api) error {
func GenDeploy(api config.Api) error {
color.Green("正在生成deploy...")
// deploy 文件夹
@ -32,16 +32,17 @@ func GenDeploy(namespace string, api config.Api) error {
path := api.Path
if path == "" {
path = fmt.Sprintf("/%s/%s/?(.*)", namespace, api.Name)
path = fmt.Sprintf("/%s/%s/?(.*)", api.Namespace, api.Name)
}
data := map[string]string{
"NameSpace": namespace,
"NameSpace": api.Namespace,
"AppName": api.Name,
"AliasName": api.AliasName,
"Port": api.Port,
"Host": host,
"Path": path}
"Path": path,
}
// value.yaml
valuesTemplate, err := deployDir.ReadFile("values.tpl")
@ -82,6 +83,12 @@ func GenDeploy(namespace string, api config.Api) error {
return err
}
// application.yaml 文件
err = copyTo("templates/application.yaml", templatesDirPath+"/application.yaml")
if err != nil {
return err
}
// deployment.yaml 文件
err = copyTo("templates/deployment.yaml", templatesDirPath+"/deployment.yaml")
if err != nil {
@ -114,13 +121,5 @@ func copyTo(fileName string, toPath string) error {
if err != nil {
return err
}
toFile, err := os.OpenFile(toPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer toFile.Close()
_, err = toFile.Write(fileContent)
return err
return util.WriteFile(toPath, fileContent)
}

View File

@ -0,0 +1,20 @@
apiVersion: app.k8s.io/v1beta1
kind: Application
metadata:
name: {{ .Release.Name }}
namespace: {{ .Values.nameSpace }}
labels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
annotations:
servicemesh.kubesphere.io/enabled: 'true'
spec:
selector:
matchLabels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
addOwnerRef: true

View File

@ -5,18 +5,32 @@ metadata:
namespace: {{ .Values.nameSpace }}
labels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
app.kubernetes.io/instance: {{ .Release.Name }}
annotations:
kubesphere.io/alias-name: {{ .Values.aliasName }}
kubesphere.io/creator: drone
meta.helm.sh/release-name: {{ .Release.Name }}
meta.helm.sh/release-namespace: {{ .Values.nameSpace }}
spec:
replicas: 1
selector:
matchLabels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
app.kubernetes.io/instance: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
volumes:
- name: host-time
@ -27,11 +41,17 @@ spec:
- name: {{ .Release.Name }}
image: {{ .Values.image }}:{{ .Values.imageTag }}
ports:
- containerPort: {{ .Values.port }}
protocol: TCP
- containerPort: {{ .Values.port }}
protocol: TCP
env:
- name: endpoints
- name: ENDPOINTS
value: 'etcd:2379'
- name: APP_NAME
value: {{ .Values.appName }}
- name: NAMESPACE
value: {{ .Values.nameSpace }}
- name: APP_TYPE
value: api
resources:
limits:
cpu: 200m

View File

@ -4,6 +4,12 @@ apiVersion: networking.k8s.io/v1
metadata:
name: {{ .Release.Name }}
namespace: {{ .Values.nameSpace }}
labels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
app.kubernetes.io/instance: {{ .Release.Name }}
annotations:
kubesphere.io/creator: drone
nginx.ingress.kubernetes.io/enable-cors: 'true'

View File

@ -7,6 +7,10 @@ metadata:
kubesphere.io/creator: drone
labels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
ports:
- name: http-{{ .Release.Name }}

View File

@ -1,4 +1,5 @@
nameSpace: {{ .NameSpace }}
appName: {{ .AppName }}
aliasName: {{ .AliasName }}
image: reg.icechen.cn/{{ .NameSpace }}/api-{{ .AppName }}
imageTag: latest

View File

@ -8,7 +8,7 @@ RUN go mod download -x
WORKDIR /go/src
ADD . .
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o api_{{ .Name }} ./app/api/{{ .Name }}
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o api_{{ .Name }} ./{{ .Root }}
FROM reg.icechen.cn/alpine as {{ .Name }}
WORKDIR /go/src

View File

@ -3,6 +3,8 @@ package golang
import (
_ "embed"
"git.icechen.cn/pkg/wujian_develop_tool/config"
"github.com/fatih/color"
"git.icechen.cn/pkg/wujian_develop_tool/util"
@ -14,11 +16,11 @@ var dockerfileTemplate string
//go:embed "main.tpl"
var mainGoTemplate string
func GenDockerfile(name string) error {
func GenDockerfile(api config.Api) error {
color.Green("正在生成dockerfile...")
err := util.TemplateToFile("app/api/"+name+"/Dockerfile", dockerfileTemplate, map[string]string{"Name": name})
err := util.TemplateToFile(api.Root+"/Dockerfile", dockerfileTemplate, api)
if err != nil {
return err
}
return util.TemplateToFile("app/api/"+name+"/main.go", mainGoTemplate, nil)
return util.TemplateToFile(api.Root+"/main.go", mainGoTemplate, nil)
}

View File

@ -3,9 +3,10 @@ package deploy
import (
"embed"
_ "embed"
"git.icechen.cn/pkg/wujian_develop_tool/config"
"os"
"git.icechen.cn/pkg/wujian_develop_tool/config"
"github.com/fatih/color"
"git.icechen.cn/pkg/wujian_develop_tool/util"
@ -14,7 +15,7 @@ import (
//go:embed "*"
var deployDir embed.FS
func GenDeploy(namespace string, service config.Service) error {
func GenDeploy(service config.Service) error {
color.Green("正在生成deploy...")
// deploy 文件夹
@ -25,10 +26,11 @@ func GenDeploy(namespace string, service config.Service) error {
}
data := map[string]string{
"NameSpace": namespace,
"NameSpace": service.Namespace,
"AppName": service.Name,
"AliasName": service.AliasName,
"Port": service.Port}
"Port": service.Port,
}
// value.yaml
valuesTemplate, err := deployDir.ReadFile("values.tpl")
@ -69,6 +71,12 @@ func GenDeploy(namespace string, service config.Service) error {
return err
}
// application.yaml 文件
err = copyTo("templates/application.yaml", templatesDirPath+"/application.yaml")
if err != nil {
return err
}
// deployment.yaml 文件
err = copyTo("templates/deployment.yaml", templatesDirPath+"/deployment.yaml")
if err != nil {
@ -95,13 +103,5 @@ func copyTo(fileName string, toPath string) error {
if err != nil {
return err
}
toFile, err := os.OpenFile(toPath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer toFile.Close()
_, err = toFile.Write(fileContent)
return err
return util.WriteFile(toPath, fileContent)
}

View File

@ -0,0 +1,20 @@
apiVersion: app.k8s.io/v1beta1
kind: Application
metadata:
name: {{ .Release.Name }}
namespace: {{ .Values.nameSpace }}
labels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
annotations:
servicemesh.kubesphere.io/enabled: 'true'
spec:
selector:
matchLabels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
addOwnerRef: true

View File

@ -5,18 +5,32 @@ metadata:
namespace: {{ .Values.nameSpace }}
labels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
app.kubernetes.io/instance: {{ .Release.Name }}
annotations:
kubesphere.io/alias-name: {{ .Values.aliasName }}
kubesphere.io/creator: drone
meta.helm.sh/release-name: {{ .Release.Name }}
meta.helm.sh/release-namespace: {{ .Values.nameSpace }}
spec:
replicas: 1
selector:
matchLabels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
app.kubernetes.io/instance: {{ .Release.Name }}
template:
metadata:
labels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
volumes:
- name: host-time
@ -27,11 +41,17 @@ spec:
- name: {{ .Release.Name }}
image: {{ .Values.image }}:{{ .Values.imageTag }}
ports:
- containerPort: {{ .Values.port }}
protocol: TCP
- containerPort: {{ .Values.port }}
protocol: TCP
env:
- name: endpoints
- name: ENDPOINTS
value: 'etcd:2379'
- name: APP_NAME
value: {{ .Values.appName }}
- name: NAMESPACE
value: {{ .Values.nameSpace }}
- name: APP_TYPE
value: service
resources:
limits:
cpu: 200m

View File

@ -7,9 +7,13 @@ metadata:
kubesphere.io/creator: drone
labels:
app: {{ .Release.Name }}
app.kubernetes.io/version: v1
app.kubernetes.io/name: {{ .Release.Name }}
version: v1
app.kubernetes.io/instance: {{ .Release.Name }}
spec:
ports:
- name: grpc-{{ .Release.Name }}
- name: http-{{ .Release.Name }}
protocol: TCP
port: 80
targetPort: {{ .Values.port }}

View File

@ -1,5 +1,6 @@
nameSpace: {{ .NameSpace }}
appName: {{ .AppName }}
aliasName: {{ .AliasName }}
image: reg.icechen.cn/{{ .NameSpace }}/api-{{ .AppName }}
image: reg.icechen.cn/{{ .NameSpace }}/service-{{ .AppName }}
imageTag: latest
port: {{ .Port }}

View File

@ -8,7 +8,7 @@ RUN go mod download -x
WORKDIR /go/src
ADD . .
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o service_{{ .Name }} ./app/service/{{ .Name }}
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o service_{{ .Name }} ./{{ .Root }}
FROM reg.icechen.cn/alpine as {{ .Name }}
WORKDIR /go/src

View File

@ -3,6 +3,8 @@ package golang
import (
_ "embed"
"git.icechen.cn/pkg/wujian_develop_tool/config"
"github.com/fatih/color"
"git.icechen.cn/pkg/wujian_develop_tool/util"
@ -14,11 +16,11 @@ var dockerfileTemplate string
//go:embed "main.tpl"
var mainGoTemplate string
func GenDockerfile(name string) error {
func GenDockerfile(service config.Service) error {
color.Green("正在生成dockerfile...")
err := util.TemplateToFile("app/service/"+name+"/Dockerfile", dockerfileTemplate, map[string]string{"Name": name})
err := util.TemplateToFile(service.Root+"/Dockerfile", dockerfileTemplate, service)
if err != nil {
return err
}
return util.TemplateToFile("app/service/"+name+"/main.go", mainGoTemplate, nil)
return util.TemplateToFile(service.Root+"/main.go", mainGoTemplate, nil)
}

View File

@ -3,12 +3,14 @@ package util
import (
"bufio"
"fmt"
"github.com/fatih/color"
"os"
"strings"
"github.com/fatih/color"
)
func ReadBool() bool {
retry:
read := bufio.NewReader(os.Stdin)
ret, _ := read.ReadByte()
switch ret {
@ -20,8 +22,10 @@ func ReadBool() bool {
fallthrough
case 'N':
return false
default:
NoNewLinePrint(color.HiRedString, "请输入(y,Y,n,N)其中之一: ")
goto retry
}
return false
}
func ReadBoolWithMessage(message string) bool {

View File

@ -2,19 +2,18 @@ package util
import (
"bytes"
"fmt"
"os"
"strings"
"text/template"
"github.com/fatih/color"
"github.com/sergi/go-diff/diffmatchpatch"
)
func TemplateToFile(desFile string, templateContent string, data interface{}) error {
file, err := os.OpenFile(desFile, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
if err != nil {
return err
}
defer file.Close()
tpl := template.New("template")
tpl, err = tpl.Parse(templateContent)
tpl, err := tpl.Parse(templateContent)
if err != nil {
return err
}
@ -25,14 +24,66 @@ func TemplateToFile(desFile string, templateContent string, data interface{}) er
return err
}
_, err = file.Write(b.Bytes())
err = WriteFile(desFile, b.Bytes())
if err != nil {
return err
}
return nil
}
var defaultPrintDiff = color.WhiteString
func IsChange(diff []diffmatchpatch.Diff) bool {
for _, d := range diff {
if d.Type != diffmatchpatch.DiffEqual {
return true
}
}
return false
}
// AddLine 增加行号 TODO: 颜色不完美
func AddLine(s string) string {
lines := strings.Split(s, "\n")
for i, line := range lines {
// lines[i] = fmt.Sprintf("%s %s", defaultPrintDiff("%4d.", i+1), line)
lines[i] = fmt.Sprintf("%4d. %s", i+1, line)
}
return strings.Join(lines, "\n")
}
func WriteFile(filePath string, data []byte) error {
color.Green("写入[%s]文件...", filePath)
toFileContent, err := os.ReadFile(filePath)
if err == nil {
dmp := diffmatchpatch.New()
diffContent := dmp.DiffMain(string(toFileContent), string(data), true)
if IsChange(diffContent) {
color.Red("文件[%s]已存在:", filePath)
b := bytes.Buffer{}
diffContent = dmp.DiffCleanupSemanticLossless(diffContent)
for _, diff := range diffContent {
switch diff.Type {
case diffmatchpatch.DiffDelete:
b.WriteString(color.RedString(diff.Text))
case diffmatchpatch.DiffInsert:
b.WriteString(color.GreenString(diff.Text))
case diffmatchpatch.DiffEqual:
b.WriteString(diff.Text)
}
}
fmt.Println(AddLine(b.String()))
if !ReadBoolWithMessage("是否确认此次修改(y修改/n跳过): ") {
color.Red("跳过文件: %s\n\n", filePath)
return nil
}
} else {
color.Red("文件已存在且无变动,跳过文件: %s\n\n", filePath)
return nil
}
}
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
if err != nil {
return err
@ -40,6 +91,7 @@ func WriteFile(filePath string, data []byte) error {
defer file.Close()
_, err = file.Write(data)
color.Red("写入文件完成: %s\n\n", filePath)
return err
}