From aabf0843abda6752569ac7ee7b692526db60fd55 Mon Sep 17 00:00:00 2001 From: cnone Date: Sun, 19 May 2019 22:03:26 +0300 Subject: [PATCH] Make auth parameters optional --- cmd/config.go | 30 +++++++++++++++++++++++++++--- cmd/config_set.go | 2 +- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index 59ad9bfd..3a61fdbe 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -44,18 +44,32 @@ func addConfigFlags(flags *pflag.FlagSet) { flags.Bool("branding.disableExternal", false, "disable external links such as GitHub links") } -func getAuthentication(flags *pflag.FlagSet, defaults ...*settings.Settings) (settings.AuthMethod, auth.Auther) { +func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) { method := settings.AuthMethod(mustGetString(flags, "auth.method")) - if len(defaults) > 0 { - method = defaults[0].AuthMethod + + 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) + } } var auther auth.Auther if method == auth.MethodProxyAuth { header := mustGetString(flags, "auth.header") + + if header == "" { + header = defaultAuther["header"].(string) + } + if header == "" { panic(nerrors.New("you must set the flag 'auth.header' for method 'proxy'")) } + auther = &auth.ProxyAuth{Header: header} } @@ -70,6 +84,16 @@ func getAuthentication(flags *pflag.FlagSet, defaults ...*settings.Settings) (se key := mustGetString(flags, "recaptcha.key") secret := mustGetString(flags, "recaptcha.secret") + if key == "" { + kmap := defaultAuther["recaptcha"].(map[string]interface{}) + key = kmap["key"].(string) + } + + if secret == "" { + smap := defaultAuther["recaptcha"].(map[string]interface{}) + secret = smap["secret"].(string) + } + if key == "" || secret == "" { panic(nerrors.New("you must set the flag 'recaptcha.key' and 'recaptcha.secret' for method 'json'")) } diff --git a/cmd/config_set.go b/cmd/config_set.go index e1ffb6e5..ffce2939 100644 --- a/cmd/config_set.go +++ b/cmd/config_set.go @@ -72,7 +72,7 @@ you want to change. Other options will remain unchanged.`, 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) + set.AuthMethod, auther = getAuthentication(flags, set, auther) err = d.store.Auth.Save(auther) checkErr(err) }