fix(config): use proper var name for arguments

This commit is contained in:
hexxa 2022-02-09 13:50:11 +08:00 committed by Hexxa
parent 755c5703c1
commit 33a269d220
4 changed files with 24 additions and 24 deletions

View file

@ -9,15 +9,15 @@ import (
serverPkg "github.com/ihexxa/quickshare/src/server"
)
var opts = &serverPkg.Opts{}
var args = &serverPkg.Args{}
func main() {
_, err := goflags.Parse(opts)
_, err := goflags.Parse(args)
if err != nil {
panic(err)
}
cfg, err := serverPkg.LoadCfg(opts)
cfg, err := serverPkg.LoadCfg(args)
if err != nil {
fmt.Printf("failed to load config: %s", err)
os.Exit(1)

View file

@ -12,14 +12,14 @@ import (
"github.com/ihexxa/quickshare/src/kvstore/boltdbpvd"
)
type Opts struct {
type Args struct {
Host string `short:"h" long:"host" description:"server hostname"`
Port int `short:"p" long:"port" description:"server port"`
DbPath string `short:"d" long:"db" description:"path of the quickshare.db"`
Configs []string `short:"c" long:"configs" description:"config path"`
}
func LoadCfg(opts *Opts) (*gocfg.Cfg, error) {
func LoadCfg(args *Args) (*gocfg.Cfg, error) {
defaultCfg, err := DefaultConfig()
if err != nil {
return nil, err
@ -30,14 +30,14 @@ func LoadCfg(opts *Opts) (*gocfg.Cfg, error) {
return nil, err
}
cfg, err = mergeConfigFiles(cfg, opts.Configs)
cfg, err = mergeConfigFiles(cfg, args.Configs)
if err != nil {
return nil, err
}
dbPath := cfg.GrabString("Db.DbPath")
if opts.DbPath != "" {
dbPath = opts.DbPath
if args.DbPath != "" {
dbPath = args.DbPath
_, err := os.Stat(dbPath)
if err == nil {
cfg, err = mergeDbConfig(cfg, dbPath)
@ -50,7 +50,7 @@ func LoadCfg(opts *Opts) (*gocfg.Cfg, error) {
}
}
return mergeArgs(cfg, opts)
return mergeArgs(cfg, args)
}
func mergeDbConfig(cfg *gocfg.Cfg, dbPath string) (*gocfg.Cfg, error) {
@ -94,15 +94,15 @@ func mergeConfigFiles(cfg *gocfg.Cfg, configPaths []string) (*gocfg.Cfg, error)
return cfg, nil
}
func mergeArgs(cfg *gocfg.Cfg, opts *Opts) (*gocfg.Cfg, error) {
if opts.Host != "" {
cfg.SetString("Server.Host", opts.Host)
func mergeArgs(cfg *gocfg.Cfg, args *Args) (*gocfg.Cfg, error) {
if args.Host != "" {
cfg.SetString("Server.Host", args.Host)
}
if opts.Port != 0 {
cfg.SetInt("Server.Port", opts.Port)
if args.Port != 0 {
cfg.SetInt("Server.Port", args.Port)
}
if opts.DbPath != "" {
cfg.SetString("Db.DbPath", opts.DbPath)
if args.DbPath != "" {
cfg.SetString("Db.DbPath", args.DbPath)
}
return cfg, nil

View file

@ -12,30 +12,30 @@ import (
)
func TestLoadCfg(t *testing.T) {
argsList := []*Opts{
argsList := []*Args{
// default config
&Opts{
&Args{
Host: "",
Port: 0,
DbPath: "",
Configs: []string{},
},
// default config + config_1
&Opts{
&Args{
Host: "",
Port: 0,
DbPath: "",
Configs: []string{"testdata/config_1.yml"},
},
// default config + config_1 + config_4
&Opts{
&Args{
Host: "",
Port: 0,
DbPath: "",
Configs: []string{"testdata/config_1.yml", "testdata/config_4.yml"},
},
// config partial overwrite
&Opts{
&Args{
Host: "",
Port: 0,
DbPath: "",
@ -47,7 +47,7 @@ func TestLoadCfg(t *testing.T) {
},
},
// default config + config_1 + config_4 + db_partial + args(db)
&Opts{
&Args{
Host: "",
Port: 0,
DbPath: "testdata/test_quickshare.db",
@ -331,8 +331,8 @@ func TestLoadCfg(t *testing.T) {
}
testLoadCfg := func(t *testing.T) {
for i, opts := range argsList {
gotCfg, err := LoadCfg(opts)
for i, args := range argsList {
gotCfg, err := LoadCfg(args)
if err != nil {
t.Fatal(err)
}

Binary file not shown.