wujian_develop_tool/config/configFile.go

179 lines
3.4 KiB
Go
Raw Normal View History

2022-01-06 23:34:37 +08:00
package config
2022-01-05 20:25:29 +08:00
import (
2022-01-06 23:04:25 +08:00
"git.icechen.cn/pkg/wujian_develop_tool/util"
2022-01-05 20:25:29 +08:00
"io/ioutil"
2022-01-06 20:27:18 +08:00
"github.com/urfave/cli/v2"
2022-01-05 20:25:29 +08:00
"gopkg.in/yaml.v3"
)
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-06 22:32:03 +08:00
Name string `json:"name" yaml:"name"`
AliasName string `json:"alias_name" yaml:"aliasName"`
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-06 22:32:03 +08:00
Name string `json:"name" yaml:"name"`
AliasName string `json:"alias_name" yaml:"aliasName"`
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 {
if a.Name == api.Name {
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 {
if s.Name == service.Name {
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-06 22:32:03 +08:00
func (sl ApiList) GetApp(appName string) Api {
for _, api := range sl {
if api.Name == appName {
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
func (sl ServiceList) GetApp(appName string) Service {
for _, service := range sl {
if service.Name == appName {
return service
}
}
return Service{}
}