This commit is contained in:
Jay 2019-05-08 18:01:06 +08:00
parent fe4bda3d57
commit f828db0a38
6 changed files with 72 additions and 3 deletions

7
config.default.yml Normal file
View File

@ -0,0 +1,7 @@
url: ''
verify_value: ''
domains:
- name: ''
records:
- name: ''
type: 'A or AAAA'

5
go.mod
View File

@ -2,4 +2,7 @@ module git.trj.tw/golang/go-ddns-client
go 1.12 go 1.12
require git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 require (
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349
gopkg.in/yaml.v2 v2.2.2
)

3
go.sum
View File

@ -1,2 +1,5 @@
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 h1:V6ifeiJ3ExnjaUylTOz37n6z5uLwm6fjKjnztbTCaQI= git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349 h1:V6ifeiJ3ExnjaUylTOz37n6z5uLwm6fjKjnztbTCaQI=
git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349/go.mod h1:yE+qbsUsijCTdwsaQRkPT1CXYk7ftMzXsCaaYx/0QI0= git.trj.tw/golang/utils v0.0.0-20190225142552-b019626f0349/go.mod h1:yE+qbsUsijCTdwsaQRkPT1CXYk7ftMzXsCaaYx/0QI0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

19
main.go
View File

@ -1,7 +1,22 @@
package main package main
import "fmt" import (
"log"
"git.trj.tw/golang/go-ddns-client/module/config"
"git.trj.tw/golang/go-ddns-client/module/option"
)
func init() {
option.RegOptions()
}
func main() { func main() {
fmt.Println("vim-go") var err error
opts := option.GetOptions()
err = config.LoadConfig(opts.Config)
if err != nil {
log.Fatal(err)
}
} }

View File

@ -2,10 +2,12 @@ package config
import ( import (
"errors" "errors"
"io/ioutil"
"os" "os"
"path" "path"
"git.trj.tw/golang/utils" "git.trj.tw/golang/utils"
"gopkg.in/yaml.v2"
) )
// RecordData - // RecordData -
@ -27,6 +29,8 @@ type Config struct {
Domains []DomainData `yaml:"domains"` Domains []DomainData `yaml:"domains"`
} }
var conf *Config
// LoadConfig - // LoadConfig -
func LoadConfig(p ...string) error { func LoadConfig(p ...string) error {
var fp string var fp string
@ -42,9 +46,23 @@ func LoadConfig(p ...string) error {
fp = path.Join(wd, "config.yml") fp = path.Join(wd, "config.yml")
} }
fp = utils.ParsePath(fp)
if exists := utils.CheckExists(fp, false); !exists { if exists := utils.CheckExists(fp, false); !exists {
return errors.New("config file not 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 return nil
} }
// GetConfig -
func GetConfig() *Config { return conf }

23
module/option/option.go Normal file
View File

@ -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 }