wujian_develop_tool/config/configFile.go

180 lines
3.6 KiB
Go
Raw Permalink Normal View History

2022-01-06 23:34:37 +08:00
package config
2022-01-05 20:25:29 +08:00
import (
2022-01-11 00:03:14 +08:00
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
2022-01-05 20:25:29 +08:00
"io/ioutil"
2022-01-10 20:26:14 +08:00
"git.icechen.cn/pkg/wujian_develop_tool/util"
2022-01-05 20:25:29 +08:00
)
2022-01-06 00:19:22 +08:00
var DefaultConfigFile = ".drone.yml"
func Init(c *cli.Context) error {
// 读取配置
configFileName := c.String("config")
if configFileName != "" {
DefaultConfigFile = configFileName
}
2022-01-06 20:27:18 +08:00
if err := ReadConfig(); err != nil {
return err
}
appList := c.Args().Slice()
2022-01-06 23:34:37 +08:00
Config.HasApi = Config.Api.HasApp(appList)
Config.HasService = Config.Service.HasApp(appList)
2022-01-06 20:27:18 +08:00
return nil
2022-01-06 00:19:22 +08:00
}
2022-01-05 20:25:29 +08:00
2022-01-06 23:34:37 +08:00
type ConfigFile struct {
2022-01-05 20:25:29 +08:00
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"`
2022-01-06 20:27:18 +08:00
HasApi ApiList `json:"-" yaml:"-"`
HasService ServiceList `json:"-" yaml:"-"`
2022-01-05 20:25:29 +08:00
}
type (
ApiList []Api
Api struct {
2022-01-11 00:03:14 +08:00
Namespace string `json:"namespace" yaml:"namespace"`
2022-01-06 22:32:03 +08:00
Name string `json:"name" yaml:"name"`
2022-01-11 00:03:14 +08:00
AliasName string `json:"alias_name" yaml:"alias_name"`
2022-01-06 22:32:03 +08:00
Root string `json:"root" yaml:"root"`
Type string `json:"type" yaml:"type"`
Port string `json:"port" yaml:"port"`
Host string `json:"host,omitempty" yaml:"host,omitempty"`
Path string `json:"path,omitempty" yaml:"path,omitempty"`
2022-01-05 20:25:29 +08:00
}
)
type (
ServiceList []Service
Service struct {
2022-01-11 00:03:14 +08:00
Namespace string `json:"namespace" yaml:"namespace"`
2022-01-06 22:32:03 +08:00
Name string `json:"name" yaml:"name"`
2022-01-11 00:03:14 +08:00
AliasName string `json:"alias_name" yaml:"alias_name"`
2022-01-06 22:32:03 +08:00
Root string `json:"root" yaml:"root"`
Type string `json:"type" yaml:"type"`
Port string `json:"port" yaml:"port"`
2022-01-05 20:25:29 +08:00
}
)
2022-01-06 23:34:37 +08:00
var Config ConfigFile
2022-01-05 20:25:29 +08:00
func ReadConfig() error {
configFile, err := ioutil.ReadFile(DefaultConfigFile)
if err != nil {
return err
}
2022-01-06 23:34:37 +08:00
err = yaml.Unmarshal(configFile, &Config)
2022-01-05 20:25:29 +08:00
if err != nil {
return err
}
return nil
}
2022-01-06 23:34:37 +08:00
func (c *ConfigFile) AppendAPI(api Api) error {
2022-01-06 22:32:03 +08:00
has := false
for i, a := range c.Api {
2022-01-11 00:03:14 +08:00
if a.Name == api.Name && a.Namespace == api.Namespace {
2022-01-06 22:32:03 +08:00
c.Api[i] = api
has = true
break
}
}
if !has {
c.Api = append(c.Api, api)
}
out, err := yaml.Marshal(c)
if err != nil {
return err
}
return util.WriteFileWithNotEdit(DefaultConfigFile, out)
}
2022-01-06 23:34:37 +08:00
func (c *ConfigFile) AppendService(service Service) error {
2022-01-06 22:32:03 +08:00
has := false
for i, s := range c.Service {
2022-01-11 00:03:14 +08:00
if s.Name == service.Name && s.Namespace == service.Namespace {
2022-01-06 22:32:03 +08:00
c.Service[i] = service
has = true
break
}
}
if !has {
c.Service = append(c.Service, service)
}
out, err := yaml.Marshal(c)
if err != nil {
return err
}
return util.WriteFileWithNotEdit(DefaultConfigFile, out)
}
func (sl ApiList) HasApp(appNameList []string) ApiList {
2022-01-05 20:25:29 +08:00
if len(appNameList) == 0 {
2022-01-06 22:32:03 +08:00
return sl
2022-01-05 20:25:29 +08:00
}
2022-01-06 22:32:03 +08:00
hasApp := make(ApiList, 0, len(sl))
2022-01-05 20:25:29 +08:00
al:
2022-01-06 22:32:03 +08:00
for _, api := range sl {
2022-01-05 20:25:29 +08:00
for _, a := range appNameList {
if a == api.Name {
hasApp = append(hasApp, api)
continue al
}
}
}
return hasApp
}
2022-01-06 20:27:18 +08:00
2022-01-10 20:26:14 +08:00
func (sl ApiList) GetApp(namespace, appName string) Api {
2022-01-06 22:32:03 +08:00
for _, api := range sl {
2022-01-11 00:03:14 +08:00
if api.Name == appName && api.Namespace == namespace {
2022-01-06 22:32:03 +08:00
return api
}
}
return Api{}
}
2022-01-06 20:27:18 +08:00
func (sl ServiceList) HasApp(appNameList []string) ServiceList {
if len(appNameList) == 0 {
return sl
}
hasApp := make(ServiceList, 0, len(sl))
sl:
for _, service := range sl {
for _, a := range appNameList {
if a == service.Name {
hasApp = append(hasApp, service)
continue sl
}
}
}
return hasApp
}
2022-01-06 22:32:03 +08:00
2022-01-10 20:26:14 +08:00
func (sl ServiceList) GetApp(namespace, appName string) Service {
2022-01-06 22:32:03 +08:00
for _, service := range sl {
2022-01-11 00:03:14 +08:00
if service.Name == appName && service.Namespace == namespace {
2022-01-06 22:32:03 +08:00
return service
}
}
return Service{}
}