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" serverPkg "github.com/ihexxa/quickshare/src/server"
) )
var opts = &serverPkg.Opts{} var args = &serverPkg.Args{}
func main() { func main() {
_, err := goflags.Parse(opts) _, err := goflags.Parse(args)
if err != nil { if err != nil {
panic(err) panic(err)
} }
cfg, err := serverPkg.LoadCfg(opts) cfg, err := serverPkg.LoadCfg(args)
if err != nil { if err != nil {
fmt.Printf("failed to load config: %s", err) fmt.Printf("failed to load config: %s", err)
os.Exit(1) os.Exit(1)

View file

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

View file

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

Binary file not shown.