update
This commit is contained in:
@@ -2,10 +2,12 @@ package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.trj.tw/golang/utils"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
// RecordData -
|
||||
@@ -27,6 +29,8 @@ type Config struct {
|
||||
Domains []DomainData `yaml:"domains"`
|
||||
}
|
||||
|
||||
var conf *Config
|
||||
|
||||
// LoadConfig -
|
||||
func LoadConfig(p ...string) error {
|
||||
var fp string
|
||||
@@ -42,9 +46,23 @@ func LoadConfig(p ...string) error {
|
||||
fp = path.Join(wd, "config.yml")
|
||||
}
|
||||
|
||||
fp = utils.ParsePath(fp)
|
||||
if exists := utils.CheckExists(fp, false); !exists {
|
||||
return errors.New("config file not exists")
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile(fp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conf = &Config{}
|
||||
err = yaml.Unmarshal(data, conf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConfig -
|
||||
func GetConfig() *Config { return conf }
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package option
|
||||
|
||||
import "flag"
|
||||
|
||||
// Options -
|
||||
type Options struct {
|
||||
Help bool
|
||||
Config string
|
||||
}
|
||||
|
||||
var opts *Options
|
||||
|
||||
// RegOptions -
|
||||
func RegOptions() {
|
||||
opts = &Options{}
|
||||
flag.StringVar(&opts.Config, "config", "", "config file path - default: `pwd/config.yml`")
|
||||
flag.StringVar(&opts.Config, "f", "", "config file path - default: `pwd/config.yml`")
|
||||
flag.BoolVar(&opts.Help, "help", false, "show help")
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
// GetOptions -
|
||||
func GetOptions() *Options { return opts }
|
||||
Reference in New Issue
Block a user