wujian_develop_tool/configFile.go

81 lines
1.5 KiB
Go

package main
import (
"github.com/urfave/cli/v2"
"io/ioutil"
"gopkg.in/yaml.v3"
)
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"`
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
}