From 723372fe4d82cccc9d9dd5ffcee5789af1cc1346 Mon Sep 17 00:00:00 2001 From: icechen Date: Wed, 5 Jan 2022 20:25:29 +0800 Subject: [PATCH] init --- README.md | 8 ++++++ api.go | 36 ++++++++++++++++++++++++++ app.go | 1 + configFile.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 15 +++++++++++ go.sum | 27 ++++++++++++++++++++ health.go | 62 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 37 +++++++++++++++++++++++++++ util.go | 23 +++++++++++++++++ 9 files changed, 279 insertions(+) create mode 100644 api.go create mode 100644 app.go create mode 100644 configFile.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 health.go create mode 100644 main.go create mode 100644 util.go diff --git a/README.md b/README.md index 1572d4c..f466f1d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,10 @@ # wujian_develop_tool +## 使用 + +### 初始化 + +``` +wdt init +``` + diff --git a/api.go b/api.go new file mode 100644 index 0000000..61369a3 --- /dev/null +++ b/api.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + + "github.com/fatih/color" + + "github.com/urfave/cli/v2" +) + +// ActionApi API应用列表 +func ActionApi(c *cli.Context) error { + // 读取配置 + err := ReadConfig() + if err != nil { + return err + } + + apiList := c.Args().Slice() + color.Green("API应用列表:") + for _, api := range config.Api.HasApp(apiList) { + color.Green("\t%s", api.Name) + color.Green("\t\t路径:\t%s", api.Root) + color.Green("\t\t类型:\t%s", api.Type) + color.Green("\t\t端口:\t%s", api.Port) + c := color.New(color.Bold) + c.EnableColor() + _, err := c.Println(healthApi(api)) + if err != nil { + return err + } + + fmt.Print("\n") + } + return nil +} diff --git a/app.go b/app.go new file mode 100644 index 0000000..06ab7d0 --- /dev/null +++ b/app.go @@ -0,0 +1 @@ +package main diff --git a/configFile.go b/configFile.go new file mode 100644 index 0000000..1e2e06b --- /dev/null +++ b/configFile.go @@ -0,0 +1,70 @@ +package main + +import ( + "io/ioutil" + + "gopkg.in/yaml.v3" +) + +const DefaultConfigFile = ".drone.yml" + +type Config struct { + Kind string `json:"kind" yaml:"kind"` + Type string `json:"type" yaml:"type"` + Name string `json:"name" yaml:"name"` + Api ApiList `json:"api" yaml:"api"` + Service ServiceList `json:"service" yaml:"service"` +} + +type ( + ApiList []Api + Api struct { + Name string `json:"name" yaml:"name"` + Root string `json:"root" yaml:"root"` + Type string `json:"type" yaml:"type"` + Port string `json:"port" yaml:"port"` + } +) + +type ( + ServiceList []Service + Service struct { + Name string `json:"name" yaml:"name"` + Root string `json:"root" yaml:"root"` + Type string `json:"type" yaml:"type"` + Port string `json:"port" yaml:"port"` + } +) + +var config Config + +func ReadConfig() error { + configFile, err := ioutil.ReadFile(DefaultConfigFile) + if err != nil { + return err + } + err = yaml.Unmarshal(configFile, &config) + if err != nil { + return err + } + return nil +} + +func (al ApiList) HasApp(appNameList []string) ApiList { + if len(appNameList) == 0 { + return al + } + + hasApp := make(ApiList, 0, len(al)) + +al: + for _, api := range al { + for _, a := range appNameList { + if a == api.Name { + hasApp = append(hasApp, api) + continue al + } + } + } + return hasApp +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..34b5f93 --- /dev/null +++ b/go.mod @@ -0,0 +1,15 @@ +module git.icechen.cn/pkg/wdt + +go 1.17 + +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-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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..38eb177 --- /dev/null +++ b/go.sum @@ -0,0 +1,27 @@ +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +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/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-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/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +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/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +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= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +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= +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= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/health.go b/health.go new file mode 100644 index 0000000..0547084 --- /dev/null +++ b/health.go @@ -0,0 +1,62 @@ +package main + +import ( + "bytes" + "os" +) + +type healthResp struct { + Dir bool // 应用目录健康度 + Deploy bool // 部署健康度 + Dockerfile bool // 构建镜像健康度 +} + +func (hr healthResp) IsHealth() bool { + return hr.Dir && hr.Deploy && hr.Dockerfile +} + +func (hr healthResp) String() string { + if hr.IsHealth() { + return "\t\t应用情况健康" + } + + b := bytes.Buffer{} + if !hr.Dir { + b.WriteString("\t\t应用目录:\t不存在") + } + if !hr.Deploy { + b.WriteString("\t\t部署文件:\t不存在") + } + if !hr.Dockerfile { + b.WriteString("\t\t构建文件:\t不存在") + } + return b.String() +} + +func healthApi(api Api) healthResp { + ret := healthResp{ + Dir: false, + Deploy: false, + Dockerfile: false, + } + + // Dir 健康度 + info, err := os.Stat(api.Root) + if err == nil && info.IsDir() { + ret.Dir = true + } + + // Deploy 健康度 + info, err = os.Stat(api.Root + "/deploy") + if err == nil && info.IsDir() { + ret.Deploy = true + } + + // Dockerfile 健康度 + info, err = os.Stat(api.Root + "/Dockerfile") + if err == nil && !info.IsDir() { + ret.Dockerfile = true + } + + return ret +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..d56eedf --- /dev/null +++ b/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "log" + "os" + + "github.com/urfave/cli/v2" +) + +func main() { + app := cli.NewApp() + app = &cli.App{ + Name: "wujian develop tool", + Usage: "无间开发者工具箱", + Version: "v0.0.1", + EnableBashCompletion: true, + Commands: cli.Commands{ + &cli.Command{ + Name: "api", + Usage: "api应用列表", + Action: ActionApi, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "all", + Aliases: []string{"a"}, + Usage: "所有应用", + }, + }, + }, + }, + } + + err := app.Run(os.Args) + if err != nil { + log.Fatal(err) + } +} diff --git a/util.go b/util.go new file mode 100644 index 0000000..5550e1b --- /dev/null +++ b/util.go @@ -0,0 +1,23 @@ +package main + +import ( + "bufio" + "os" +) + +var read = bufio.NewReader(os.Stdin) + +func ReadBool() bool { + ret, _ := read.ReadByte() + switch ret { + case 'y': + fallthrough + case 'Y': + return true + case 'n': + fallthrough + case 'N': + return false + } + return false +}