From 030f6607f01718e3d7535aa85f1beb3279bd32ad Mon Sep 17 00:00:00 2001 From: cnone Date: Mon, 20 May 2019 04:55:36 +0300 Subject: [PATCH] Refactor the code --- cmd/config.go | 20 +++++++++++++------- cmd/config_set.go | 22 ++++++++-------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 0d0cc324..932c1229 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -46,14 +46,20 @@ func addConfigFlags(flags *pflag.FlagSet) { func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) { method := settings.AuthMethod(mustGetString(flags, "auth.method")) + var defaultAuther map[string]interface{} - for _, arg := range defaults { - switch def := arg.(type) { - case *settings.Settings: - method = settings.AuthMethod(def.AuthMethod) - case auth.Auther: - ms, _ := json.Marshal(def) - json.Unmarshal(ms, &defaultAuther) + if len(defaults) > 0 { + if hasAuth := defaults[0]; hasAuth != true { + for _, arg := range defaults { + switch def := arg.(type) { + case *settings.Settings: + method = settings.AuthMethod(def.AuthMethod) + case auth.Auther: + ms, err := json.Marshal(def) + checkErr(err) + json.Unmarshal(ms, &defaultAuther) + } + } } } diff --git a/cmd/config_set.go b/cmd/config_set.go index ffce2939..9b49b234 100644 --- a/cmd/config_set.go +++ b/cmd/config_set.go @@ -3,7 +3,6 @@ package cmd import ( "strings" - "github.com/filebrowser/filebrowser/v2/auth" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -63,20 +62,15 @@ you want to change. Other options will remain unchanged.`, getUserDefaults(flags, &set.Defaults, false) - var auther auth.Auther - if hasAuth { - set.AuthMethod, auther = getAuthentication(flags) - err = d.store.Auth.Save(auther) - checkErr(err) - } else { - auther, err = d.store.Auth.Get(set.AuthMethod) - checkErr(err) - // check if there are new flags for existing auth method - set.AuthMethod, auther = getAuthentication(flags, set, auther) - err = d.store.Auth.Save(auther) - checkErr(err) - } + // read the defaults + auther, err := d.store.Auth.Get(set.AuthMethod) + checkErr(err) + // check if there are new flags for existing auth method + set.AuthMethod, auther = getAuthentication(flags, hasAuth, set, auther) + + err = d.store.Auth.Save(auther) + checkErr(err) err = d.store.Settings.Save(set) checkErr(err) err = d.store.Settings.SaveServer(ser)