diff --git a/api.go b/api.go index 61369a3..12362d8 100644 --- a/api.go +++ b/api.go @@ -10,12 +10,9 @@ import ( // ActionApi API应用列表 func ActionApi(c *cli.Context) error { - // 读取配置 - err := ReadConfig() - if err != nil { + if err := Init(c); err != nil { return err } - apiList := c.Args().Slice() color.Green("API应用列表:") for _, api := range config.Api.HasApp(apiList) { @@ -34,3 +31,8 @@ func ActionApi(c *cli.Context) error { } return nil } + +// ActionNewApi 创建API应用 +func ActionNewApi(c *cli.Context) error { + return nil +} diff --git a/configFile.go b/configFile.go index 1e2e06b..10396d5 100644 --- a/configFile.go +++ b/configFile.go @@ -1,12 +1,22 @@ package main import ( + "github.com/urfave/cli/v2" "io/ioutil" "gopkg.in/yaml.v3" ) -const DefaultConfigFile = ".drone.yml" +var DefaultConfigFile = ".drone.yml" + +func Init(c *cli.Context) error { + // 读取配置 + configFileName := c.String("config") + if configFileName != "" { + DefaultConfigFile = configFileName + } + return ReadConfig() +} type Config struct { Kind string `json:"kind" yaml:"kind"` diff --git a/go.mod b/go.mod index 34b5f93..480aac2 100644 --- a/go.mod +++ b/go.mod @@ -2,14 +2,17 @@ module git.icechen.cn/pkg/wdt go 1.17 +require ( + github.com/fatih/color v1.13.0 + github.com/urfave/cli/v2 v2.3.0 + gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b +) + require ( github.com/cpuguy83/go-md2man/v2 v2.0.1 // indirect - github.com/fatih/color v1.13.0 // indirect - github.com/mattn/go-colorable v0.1.9 // indirect + 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 github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect - github.com/urfave/cli/v2 v2.3.0 // indirect - golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect - gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect + golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect ) diff --git a/go.sum b/go.sum index 38eb177..ee28de4 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U= github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40= +github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= 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= @@ -21,6 +23,9 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM= +golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= diff --git a/main.go b/main.go index d56eedf..e9a4e1d 100644 --- a/main.go +++ b/main.go @@ -8,22 +8,41 @@ import ( ) func main() { + flags := []cli.Flag{ + &cli.BoolFlag{ + Name: "all", + Aliases: []string{"a"}, + Usage: "所有应用", + }, + &cli.StringSliceFlag{ + Name: "app", + Usage: "app列表", + }, + &cli.StringFlag{ + Name: "config", + Aliases: []string{"c"}, + Usage: "配置文件", + }, + } + app := cli.NewApp() app = &cli.App{ Name: "wujian develop tool", Usage: "无间开发者工具箱", Version: "v0.0.1", EnableBashCompletion: true, + Flags: flags, Commands: cli.Commands{ &cli.Command{ Name: "api", Usage: "api应用列表", Action: ActionApi, - Flags: []cli.Flag{ - &cli.BoolFlag{ - Name: "all", - Aliases: []string{"a"}, - Usage: "所有应用", + Flags: flags, + Subcommands: []*cli.Command{ + { + Name: "new", + Usage: "创建一个api应用", + Action: ActionNewApi, }, }, }, diff --git a/template/api/golang/Dockerfile b/template/api/golang/Dockerfile new file mode 100644 index 0000000..e0b09cf --- /dev/null +++ b/template/api/golang/Dockerfile @@ -0,0 +1,20 @@ +FROM golang:1.17 as builder +ENV GO111MODULE on +ENV GOPROXY https://goproxy.io,direct +WORKDIR /go/cache +ADD go.mod . +ADD go.sum . +RUN go mod download +WORKDIR /go/src +ADD . . +RUN go mod tidy +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o api_lark ./app/api/lark + +FROM reg.icechen.cn/alpine as lark +WORKDIR /go/src +COPY --from=builder /usr/share/zoneinfo /usr/share/zoneinfo +COPY --from=builder /go/src/api_lark ./ +ENV TZ=Asia/Shanghai +RUN chmod +x ./api_lark +EXPOSE 8080 +CMD ["./api_lark"] \ No newline at end of file diff --git a/template/api/golang/deploy/.helmignore b/template/api/golang/deploy/.helmignore new file mode 100644 index 0000000..0e8a0eb --- /dev/null +++ b/template/api/golang/deploy/.helmignore @@ -0,0 +1,23 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ diff --git a/template/api/golang/deploy/Chart.yaml b/template/api/golang/deploy/Chart.yaml new file mode 100644 index 0000000..c357b83 --- /dev/null +++ b/template/api/golang/deploy/Chart.yaml @@ -0,0 +1,24 @@ +apiVersion: v2 +name: lark +description: A Helm chart for Kubernetes + +# A chart can be either an 'application' or a 'library' chart. +# +# Application charts are a collection of templates that can be packaged into versioned archives +# to be deployed. +# +# Library charts provide useful utilities or functions for the chart developer. They're included as +# a dependency of application charts to inject those utilities and functions into the rendering +# pipeline. Library charts do not define any templates and therefore cannot be deployed. +type: application + +# This is the chart version. This version number should be incremented each time you make changes +# to the chart and its templates, including the app version. +# Versions are expected to follow Semantic Versioning (https://semver.org/) +version: 0.1.0 + +# This is the version number of the application being deployed. This version number should be +# incremented each time you make changes to the application. Versions are not expected to +# follow Semantic Versioning. They should reflect the version the application is using. +# It is recommended to use it with quotes. +appVersion: "0.1.0" diff --git a/template/api/golang/deploy/templates/NOTES.txt b/template/api/golang/deploy/templates/NOTES.txt new file mode 100644 index 0000000..3041ade --- /dev/null +++ b/template/api/golang/deploy/templates/NOTES.txt @@ -0,0 +1 @@ +notes \ No newline at end of file diff --git a/template/api/golang/deploy/templates/_helpers.tpl b/template/api/golang/deploy/templates/_helpers.tpl new file mode 100644 index 0000000..e69de29 diff --git a/template/api/golang/deploy/templates/deployment.yaml b/template/api/golang/deploy/templates/deployment.yaml new file mode 100644 index 0000000..e990c24 --- /dev/null +++ b/template/api/golang/deploy/templates/deployment.yaml @@ -0,0 +1,62 @@ +kind: Deployment +apiVersion: apps/v1 +metadata: + name: {{ .Release.Name }} + namespace: {{ .Values.nameSpace }} + labels: + app: {{ .Release.Name }} + annotations: + kubesphere.io/alias-name: {{ .Values.aliasName }} + kubesphere.io/creator: drone +spec: + replicas: 1 + selector: + matchLabels: + app: {{ .Release.Name }} + template: + metadata: + labels: + app: {{ .Release.Name }} + spec: + volumes: + - name: host-time + hostPath: + path: /etc/localtime + type: '' + containers: + - name: {{ .Release.Name }} + image: {{ .Values.image }}:{{ .Values.imageTag }} + ports: + - containerPort: {{ .Values.port }} + protocol: TCP + env: + - name: endpoints + value: 'etcd:2379' + resources: + limits: + cpu: 200m + memory: 1000Mi + requests: + cpu: 10m + memory: 200Mi + volumeMounts: + - name: host-time + readOnly: true + mountPath: /etc/localtime + terminationMessagePath: /dev/termination-log + terminationMessagePolicy: File + imagePullPolicy: IfNotPresent + restartPolicy: Always + terminationGracePeriodSeconds: 30 + dnsPolicy: ClusterFirst + securityContext: {} + imagePullSecrets: + - name: registry-secret + schedulerName: default-scheduler + strategy: + type: RollingUpdate + rollingUpdate: + maxUnavailable: 25% + maxSurge: 25% + revisionHistoryLimit: 10 + progressDeadlineSeconds: 600 \ No newline at end of file diff --git a/template/api/golang/deploy/templates/ingress.yaml b/template/api/golang/deploy/templates/ingress.yaml new file mode 100644 index 0000000..44bb7ee --- /dev/null +++ b/template/api/golang/deploy/templates/ingress.yaml @@ -0,0 +1,24 @@ +{{ if and .Values.host .Values.path }} +kind: Ingress +apiVersion: networking.k8s.io/v1 +metadata: + name: {{ .Release.Name }} + namespace: {{ .Values.nameSpace }} + annotations: + kubesphere.io/creator: drone + nginx.ingress.kubernetes.io/enable-cors: 'true' + nginx.ingress.kubernetes.io/rewrite-target: /$1 + nginx.ingress.kubernetes.io/use-regex: 'true' +spec: + rules: + - host: {{ .Values.host }} + http: + paths: + - path: {{ .Values.path }} + pathType: ImplementationSpecific + backend: + service: + name: {{ .Release.Name }} + port: + number: 80 + {{ end }} \ No newline at end of file diff --git a/template/api/golang/deploy/templates/service.yaml b/template/api/golang/deploy/templates/service.yaml new file mode 100644 index 0000000..884383e --- /dev/null +++ b/template/api/golang/deploy/templates/service.yaml @@ -0,0 +1,22 @@ +kind: Service +apiVersion: v1 +metadata: + name: {{ .Release.Name }} + namespace: {{ .Values.nameSpace }} + annotations: + kubesphere.io/creator: drone + labels: + app: {{ .Release.Name }} +spec: + ports: + - name: http-{{ .Release.Name }} + protocol: TCP + port: 80 + targetPort: {{ .Values.port }} + selector: + app: {{ .Release.Name }} + type: ClusterIP + sessionAffinity: None + ipFamilies: + - IPv4 + ipFamilyPolicy: SingleStack \ No newline at end of file diff --git a/template/api/golang/deploy/values.yaml b/template/api/golang/deploy/values.yaml new file mode 100644 index 0000000..8c6a3ff --- /dev/null +++ b/template/api/golang/deploy/values.yaml @@ -0,0 +1,7 @@ +nameSpace: {{ .NameSpace }} +aliasName: 请输入应用的简介 +image: reg.icechen.cn/{{ .NameSpace }}/{{ .AppName }} +imageTag: latest +port: 8080 +host: api.seamlesser.com +path: /{{ .NameSpace }}/{{ .AppName }}/?(.*) \ No newline at end of file diff --git a/util.go b/util.go index 5550e1b..7959d22 100644 --- a/util.go +++ b/util.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "github.com/fatih/color" "os" ) @@ -21,3 +22,8 @@ func ReadBool() bool { } return false } + +func ReadBoolWithMessage(message string) bool { + color.HiRed(message) + return ReadBool() +}