Compare commits
4 Commits
Author | SHA1 | Date |
---|---|---|
icechen | d984d78b03 | |
icechen | 0540fcd17f | |
icechen | 7f8bb65be3 | |
icechen | 5826eef7bf |
23
api.go
23
api.go
|
@ -2,9 +2,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||||
"os"
|
|
||||||
|
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/template/api/deploy"
|
"git.icechen.cn/pkg/wujian_develop_tool/template/api/deploy"
|
||||||
|
|
||||||
|
@ -23,7 +24,7 @@ func ActionListApi(c *cli.Context) error {
|
||||||
}
|
}
|
||||||
color.Green("API应用列表:")
|
color.Green("API应用列表:")
|
||||||
for _, api := range config.Config.HasApi {
|
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.Root)
|
||||||
color.Green("\t\t类型:\t%s", api.Type)
|
color.Green("\t\t类型:\t%s", api.Type)
|
||||||
color.Green("\t\t端口:\t%s", api.Port)
|
color.Green("\t\t端口:\t%s", api.Port)
|
||||||
|
@ -59,11 +60,21 @@ func ActionNewApi(c *cli.Context) error {
|
||||||
appList = append(appList, appName)
|
appList = append(appList, appName)
|
||||||
}
|
}
|
||||||
for _, newApp := range appList {
|
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)
|
color.Green("正在创建[%s]API应用...", newApp)
|
||||||
app := config.Config.Api.GetApp(newApp)
|
app := config.Config.Api.GetApp(namespace, newApp)
|
||||||
app = config.Api{
|
app = config.Api{
|
||||||
|
Namespace: namespace,
|
||||||
Name: newApp,
|
Name: newApp,
|
||||||
Root: "app/api/" + newApp,
|
Root: fmt.Sprintf("app/%s/api/%s", namespace, newApp),
|
||||||
}
|
}
|
||||||
|
|
||||||
if util.ExistDir(app.Root) && !util.ReadBoolWithMessage(app.Root+"目录已存在,是否要覆盖app文件目录(y/n): ") {
|
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.Host = util.ReadLineWithMessage("请输入API应用请求的域名(host,如:api.seamlesser.com,建议为空): ")
|
||||||
app.Path = util.ReadLineWithMessage("请输入API应用请求的路由(path,如:/uwe/core/?(.*),建议为空): ")
|
app.Path = util.ReadLineWithMessage("请输入API应用请求的路由(path,如:/uwe/core/?(.*),建议为空): ")
|
||||||
|
|
||||||
err = deploy.GenDeploy(config.Config.Name, app)
|
err = deploy.GenDeploy(app)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = golang.GenDockerfile(newApp)
|
err = golang.GenDockerfile(app)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
"github.com/urfave/cli/v2"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
|
||||||
"github.com/urfave/cli/v2"
|
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var DefaultConfigFile = ".drone.yml"
|
var DefaultConfigFile = ".drone.yml"
|
||||||
|
@ -42,8 +41,9 @@ type ConfigFile struct {
|
||||||
type (
|
type (
|
||||||
ApiList []Api
|
ApiList []Api
|
||||||
Api struct {
|
Api struct {
|
||||||
|
Namespace string `json:"namespace" yaml:"namespace"`
|
||||||
Name string `json:"name" yaml:"name"`
|
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"`
|
Root string `json:"root" yaml:"root"`
|
||||||
Type string `json:"type" yaml:"type"`
|
Type string `json:"type" yaml:"type"`
|
||||||
Port string `json:"port" yaml:"port"`
|
Port string `json:"port" yaml:"port"`
|
||||||
|
@ -55,8 +55,9 @@ type (
|
||||||
type (
|
type (
|
||||||
ServiceList []Service
|
ServiceList []Service
|
||||||
Service struct {
|
Service struct {
|
||||||
|
Namespace string `json:"namespace" yaml:"namespace"`
|
||||||
Name string `json:"name" yaml:"name"`
|
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"`
|
Root string `json:"root" yaml:"root"`
|
||||||
Type string `json:"type" yaml:"type"`
|
Type string `json:"type" yaml:"type"`
|
||||||
Port string `json:"port" yaml:"port"`
|
Port string `json:"port" yaml:"port"`
|
||||||
|
@ -80,7 +81,7 @@ func ReadConfig() error {
|
||||||
func (c *ConfigFile) AppendAPI(api Api) error {
|
func (c *ConfigFile) AppendAPI(api Api) error {
|
||||||
has := false
|
has := false
|
||||||
for i, a := range c.Api {
|
for i, a := range c.Api {
|
||||||
if a.Name == api.Name {
|
if a.Name == api.Name && a.Namespace == api.Namespace {
|
||||||
c.Api[i] = api
|
c.Api[i] = api
|
||||||
has = true
|
has = true
|
||||||
break
|
break
|
||||||
|
@ -102,7 +103,7 @@ func (c *ConfigFile) AppendAPI(api Api) error {
|
||||||
func (c *ConfigFile) AppendService(service Service) error {
|
func (c *ConfigFile) AppendService(service Service) error {
|
||||||
has := false
|
has := false
|
||||||
for i, s := range c.Service {
|
for i, s := range c.Service {
|
||||||
if s.Name == service.Name {
|
if s.Name == service.Name && s.Namespace == service.Namespace {
|
||||||
c.Service[i] = service
|
c.Service[i] = service
|
||||||
has = true
|
has = true
|
||||||
break
|
break
|
||||||
|
@ -140,9 +141,9 @@ al:
|
||||||
return hasApp
|
return hasApp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sl ApiList) GetApp(appName string) Api {
|
func (sl ApiList) GetApp(namespace, appName string) Api {
|
||||||
for _, api := range sl {
|
for _, api := range sl {
|
||||||
if api.Name == appName {
|
if api.Name == appName && api.Namespace == namespace {
|
||||||
return api
|
return api
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -168,9 +169,9 @@ sl:
|
||||||
return hasApp
|
return hasApp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sl ServiceList) GetApp(appName string) Service {
|
func (sl ServiceList) GetApp(namespace, appName string) Service {
|
||||||
for _, service := range sl {
|
for _, service := range sl {
|
||||||
if service.Name == appName {
|
if service.Name == appName && service.Namespace == namespace {
|
||||||
return service
|
return service
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
4
go.mod
4
go.mod
|
@ -5,6 +5,7 @@ go 1.17
|
||||||
require (
|
require (
|
||||||
github.com/drone/drone-go v1.7.1
|
github.com/drone/drone-go v1.7.1
|
||||||
github.com/fatih/color v1.13.0
|
github.com/fatih/color v1.13.0
|
||||||
|
github.com/sergi/go-diff v1.2.0
|
||||||
github.com/urfave/cli/v2 v2.3.0
|
github.com/urfave/cli/v2 v2.3.0
|
||||||
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
|
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
|
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-colorable v0.1.12 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.14 // indirect
|
github.com/mattn/go-isatty v0.0.14 // indirect
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 // 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
|
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
|
||||||
google.golang.org/appengine v1.6.7 // indirect
|
google.golang.org/appengine v1.6.7 // indirect
|
||||||
google.golang.org/protobuf v1.27.1 // indirect
|
google.golang.org/protobuf v1.27.1 // indirect
|
||||||
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
)
|
)
|
||||||
|
|
16
go.sum
16
go.sum
|
@ -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 h1:r/myEWzV9lfsM1tFLgDyu0atFtJ1fXn261LKYj/3DxU=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
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.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 h1:ZX+3Rs8YHUSUQ5mkuMLmm1zr1ttiiE2YGNxF3AnyDKw=
|
||||||
github.com/drone/drone-go v1.7.1/go.mod h1:fxCf9jAnXDZV1yDr0ckTuWd1intvcQwfJmTRpTZ1mXg=
|
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=
|
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.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 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
|
||||||
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
|
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/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/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/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.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 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
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/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/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/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 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
|
||||||
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
|
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-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-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-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-20220107192237-5cfca573fb4d h1:62NvYBuaanGXR2ZOfwDFkhhl6X1DUgf8qg3GuQvxZsE=
|
||||||
golang.org/x/net v0.0.0-20220105145211-5b0dc2dfae98/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
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-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-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||||
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/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 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
|
||||||
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
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 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-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/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.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.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 h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
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=
|
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
|
||||||
|
|
23
service.go
23
service.go
|
@ -2,9 +2,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||||
"os"
|
|
||||||
|
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/template/service/deploy"
|
"git.icechen.cn/pkg/wujian_develop_tool/template/service/deploy"
|
||||||
|
|
||||||
|
@ -23,7 +24,7 @@ func ActionListService(c *cli.Context) error {
|
||||||
}
|
}
|
||||||
color.Green("Service应用列表:")
|
color.Green("Service应用列表:")
|
||||||
for _, service := range config.Config.HasService {
|
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.Root)
|
||||||
color.Green("\t\t类型:\t%s", service.Type)
|
color.Green("\t\t类型:\t%s", service.Type)
|
||||||
color.Green("\t\t端口:\t%s", service.Port)
|
color.Green("\t\t端口:\t%s", service.Port)
|
||||||
|
@ -59,11 +60,21 @@ func ActionNewService(c *cli.Context) error {
|
||||||
appList = append(appList, appName)
|
appList = append(appList, appName)
|
||||||
}
|
}
|
||||||
for _, newApp := range appList {
|
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)
|
color.Green("正在创建[%s]Service应用...", newApp)
|
||||||
app := config.Config.Service.GetApp(newApp)
|
app := config.Config.Service.GetApp(namespace, newApp)
|
||||||
app = config.Service{
|
app = config.Service{
|
||||||
|
Namespace: namespace,
|
||||||
Name: newApp,
|
Name: newApp,
|
||||||
Root: "app/service/" + newApp,
|
Root: fmt.Sprintf("app/%s/service/%s", namespace, newApp),
|
||||||
}
|
}
|
||||||
|
|
||||||
if util.ExistDir(app.Root) && !util.ReadBoolWithMessage(app.Root+"目录已存在,是否要覆盖app文件目录(y/n): ") {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = golang.GenDockerfile(newApp)
|
err = golang.GenDockerfile(app)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,9 @@ import (
|
||||||
"embed"
|
"embed"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||||
|
@ -15,7 +15,7 @@ import (
|
||||||
//go:embed "*"
|
//go:embed "*"
|
||||||
var deployDir embed.FS
|
var deployDir embed.FS
|
||||||
|
|
||||||
func GenDeploy(namespace string, api config.Api) error {
|
func GenDeploy(api config.Api) error {
|
||||||
color.Green("正在生成deploy...")
|
color.Green("正在生成deploy...")
|
||||||
|
|
||||||
// deploy 文件夹
|
// deploy 文件夹
|
||||||
|
@ -32,16 +32,17 @@ func GenDeploy(namespace string, api config.Api) error {
|
||||||
|
|
||||||
path := api.Path
|
path := api.Path
|
||||||
if path == "" {
|
if path == "" {
|
||||||
path = fmt.Sprintf("/%s/%s/?(.*)", namespace, api.Name)
|
path = fmt.Sprintf("/%s/%s/?(.*)", api.Namespace, api.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := map[string]string{
|
data := map[string]string{
|
||||||
"NameSpace": namespace,
|
"NameSpace": api.Namespace,
|
||||||
"AppName": api.Name,
|
"AppName": api.Name,
|
||||||
"AliasName": api.AliasName,
|
"AliasName": api.AliasName,
|
||||||
"Port": api.Port,
|
"Port": api.Port,
|
||||||
"Host": host,
|
"Host": host,
|
||||||
"Path": path}
|
"Path": path,
|
||||||
|
}
|
||||||
|
|
||||||
// value.yaml
|
// value.yaml
|
||||||
valuesTemplate, err := deployDir.ReadFile("values.tpl")
|
valuesTemplate, err := deployDir.ReadFile("values.tpl")
|
||||||
|
@ -120,13 +121,5 @@ func copyTo(fileName string, toPath string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return util.WriteFile(toPath, fileContent)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,8 +44,14 @@ spec:
|
||||||
- containerPort: {{ .Values.port }}
|
- containerPort: {{ .Values.port }}
|
||||||
protocol: TCP
|
protocol: TCP
|
||||||
env:
|
env:
|
||||||
- name: endpoints
|
- name: ENDPOINTS
|
||||||
value: 'etcd:2379'
|
value: 'etcd:2379'
|
||||||
|
- name: APP_NAME
|
||||||
|
value: {{ .Values.appName }}
|
||||||
|
- name: NAMESPACE
|
||||||
|
value: {{ .Values.nameSpace }}
|
||||||
|
- name: APP_TYPE
|
||||||
|
value: api
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpu: 200m
|
cpu: 200m
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
nameSpace: {{ .NameSpace }}
|
nameSpace: {{ .NameSpace }}
|
||||||
|
appName: {{ .AppName }}
|
||||||
aliasName: {{ .AliasName }}
|
aliasName: {{ .AliasName }}
|
||||||
image: reg.icechen.cn/{{ .NameSpace }}/api-{{ .AppName }}
|
image: reg.icechen.cn/{{ .NameSpace }}/api-{{ .AppName }}
|
||||||
imageTag: latest
|
imageTag: latest
|
||||||
|
|
|
@ -8,7 +8,7 @@ RUN go mod download -x
|
||||||
WORKDIR /go/src
|
WORKDIR /go/src
|
||||||
ADD . .
|
ADD . .
|
||||||
RUN go mod tidy
|
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 }}
|
FROM reg.icechen.cn/alpine as {{ .Name }}
|
||||||
WORKDIR /go/src
|
WORKDIR /go/src
|
||||||
|
|
|
@ -3,6 +3,8 @@ package golang
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
|
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||||
|
@ -14,11 +16,11 @@ var dockerfileTemplate string
|
||||||
//go:embed "main.tpl"
|
//go:embed "main.tpl"
|
||||||
var mainGoTemplate string
|
var mainGoTemplate string
|
||||||
|
|
||||||
func GenDockerfile(name string) error {
|
func GenDockerfile(api config.Api) error {
|
||||||
color.Green("正在生成dockerfile...")
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return util.TemplateToFile("app/api/"+name+"/main.go", mainGoTemplate, nil)
|
return util.TemplateToFile(api.Root+"/main.go", mainGoTemplate, nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,10 @@ package deploy
|
||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||||
|
@ -14,7 +15,7 @@ import (
|
||||||
//go:embed "*"
|
//go:embed "*"
|
||||||
var deployDir embed.FS
|
var deployDir embed.FS
|
||||||
|
|
||||||
func GenDeploy(namespace string, service config.Service) error {
|
func GenDeploy(service config.Service) error {
|
||||||
color.Green("正在生成deploy...")
|
color.Green("正在生成deploy...")
|
||||||
|
|
||||||
// deploy 文件夹
|
// deploy 文件夹
|
||||||
|
@ -25,10 +26,11 @@ func GenDeploy(namespace string, service config.Service) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
data := map[string]string{
|
data := map[string]string{
|
||||||
"NameSpace": namespace,
|
"NameSpace": service.Namespace,
|
||||||
"AppName": service.Name,
|
"AppName": service.Name,
|
||||||
"AliasName": service.AliasName,
|
"AliasName": service.AliasName,
|
||||||
"Port": service.Port}
|
"Port": service.Port,
|
||||||
|
}
|
||||||
|
|
||||||
// value.yaml
|
// value.yaml
|
||||||
valuesTemplate, err := deployDir.ReadFile("values.tpl")
|
valuesTemplate, err := deployDir.ReadFile("values.tpl")
|
||||||
|
@ -101,13 +103,5 @@ func copyTo(fileName string, toPath string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return util.WriteFile(toPath, fileContent)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,8 +44,14 @@ spec:
|
||||||
- containerPort: {{ .Values.port }}
|
- containerPort: {{ .Values.port }}
|
||||||
protocol: TCP
|
protocol: TCP
|
||||||
env:
|
env:
|
||||||
- name: endpoints
|
- name: ENDPOINTS
|
||||||
value: 'etcd:2379'
|
value: 'etcd:2379'
|
||||||
|
- name: APP_NAME
|
||||||
|
value: {{ .Values.appName }}
|
||||||
|
- name: NAMESPACE
|
||||||
|
value: {{ .Values.nameSpace }}
|
||||||
|
- name: APP_TYPE
|
||||||
|
value: service
|
||||||
resources:
|
resources:
|
||||||
limits:
|
limits:
|
||||||
cpu: 200m
|
cpu: 200m
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
nameSpace: {{ .NameSpace }}
|
nameSpace: {{ .NameSpace }}
|
||||||
|
appName: {{ .AppName }}
|
||||||
aliasName: {{ .AliasName }}
|
aliasName: {{ .AliasName }}
|
||||||
image: reg.icechen.cn/{{ .NameSpace }}/api-{{ .AppName }}
|
image: reg.icechen.cn/{{ .NameSpace }}/service-{{ .AppName }}
|
||||||
imageTag: latest
|
imageTag: latest
|
||||||
port: {{ .Port }}
|
port: {{ .Port }}
|
|
@ -8,7 +8,7 @@ RUN go mod download -x
|
||||||
WORKDIR /go/src
|
WORKDIR /go/src
|
||||||
ADD . .
|
ADD . .
|
||||||
RUN go mod tidy
|
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 }}
|
FROM reg.icechen.cn/alpine as {{ .Name }}
|
||||||
WORKDIR /go/src
|
WORKDIR /go/src
|
||||||
|
|
|
@ -3,6 +3,8 @@ package golang
|
||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
|
|
||||||
|
"git.icechen.cn/pkg/wujian_develop_tool/config"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
|
||||||
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
"git.icechen.cn/pkg/wujian_develop_tool/util"
|
||||||
|
@ -14,11 +16,11 @@ var dockerfileTemplate string
|
||||||
//go:embed "main.tpl"
|
//go:embed "main.tpl"
|
||||||
var mainGoTemplate string
|
var mainGoTemplate string
|
||||||
|
|
||||||
func GenDockerfile(name string) error {
|
func GenDockerfile(service config.Service) error {
|
||||||
color.Green("正在生成dockerfile...")
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return util.TemplateToFile("app/service/"+name+"/main.go", mainGoTemplate, nil)
|
return util.TemplateToFile(service.Root+"/main.go", mainGoTemplate, nil)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,12 +3,14 @@ package util
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fatih/color"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func ReadBool() bool {
|
func ReadBool() bool {
|
||||||
|
retry:
|
||||||
read := bufio.NewReader(os.Stdin)
|
read := bufio.NewReader(os.Stdin)
|
||||||
ret, _ := read.ReadByte()
|
ret, _ := read.ReadByte()
|
||||||
switch ret {
|
switch ret {
|
||||||
|
@ -20,8 +22,10 @@ func ReadBool() bool {
|
||||||
fallthrough
|
fallthrough
|
||||||
case 'N':
|
case 'N':
|
||||||
return false
|
return false
|
||||||
|
default:
|
||||||
|
NoNewLinePrint(color.HiRedString, "请输入(y,Y,n,N)其中之一: ")
|
||||||
|
goto retry
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadBoolWithMessage(message string) bool {
|
func ReadBoolWithMessage(message string) bool {
|
||||||
|
|
68
util/file.go
68
util/file.go
|
@ -2,19 +2,18 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/fatih/color"
|
||||||
|
"github.com/sergi/go-diff/diffmatchpatch"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TemplateToFile(desFile string, templateContent string, data interface{}) error {
|
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 := template.New("template")
|
||||||
tpl, err = tpl.Parse(templateContent)
|
tpl, err := tpl.Parse(templateContent)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -25,14 +24,66 @@ func TemplateToFile(desFile string, templateContent string, data interface{}) er
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = file.Write(b.Bytes())
|
err = WriteFile(desFile, b.Bytes())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
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 {
|
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)
|
file, err := os.OpenFile(filePath, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -40,6 +91,7 @@ func WriteFile(filePath string, data []byte) error {
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
_, err = file.Write(data)
|
_, err = file.Write(data)
|
||||||
|
color.Red("写入文件完成: %s\n\n", filePath)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue