Refactor the code

This commit is contained in:
cnone 2019-05-20 04:55:36 +03:00
parent c3a4e33245
commit 030f6607f0
2 changed files with 21 additions and 21 deletions

View File

@ -46,16 +46,22 @@ func addConfigFlags(flags *pflag.FlagSet) {
func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) { func getAuthentication(flags *pflag.FlagSet, defaults ...interface{}) (settings.AuthMethod, auth.Auther) {
method := settings.AuthMethod(mustGetString(flags, "auth.method")) method := settings.AuthMethod(mustGetString(flags, "auth.method"))
var defaultAuther map[string]interface{} var defaultAuther map[string]interface{}
if len(defaults) > 0 {
if hasAuth := defaults[0]; hasAuth != true {
for _, arg := range defaults { for _, arg := range defaults {
switch def := arg.(type) { switch def := arg.(type) {
case *settings.Settings: case *settings.Settings:
method = settings.AuthMethod(def.AuthMethod) method = settings.AuthMethod(def.AuthMethod)
case auth.Auther: case auth.Auther:
ms, _ := json.Marshal(def) ms, err := json.Marshal(def)
checkErr(err)
json.Unmarshal(ms, &defaultAuther) json.Unmarshal(ms, &defaultAuther)
} }
} }
}
}
var auther auth.Auther var auther auth.Auther
if method == auth.MethodProxyAuth { if method == auth.MethodProxyAuth {

View File

@ -3,7 +3,6 @@ package cmd
import ( import (
"strings" "strings"
"github.com/filebrowser/filebrowser/v2/auth"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
@ -63,20 +62,15 @@ you want to change. Other options will remain unchanged.`,
getUserDefaults(flags, &set.Defaults, false) getUserDefaults(flags, &set.Defaults, false)
var auther auth.Auther // read the defaults
if hasAuth { auther, err := d.store.Auth.Get(set.AuthMethod)
set.AuthMethod, auther = getAuthentication(flags)
err = d.store.Auth.Save(auther)
checkErr(err) 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)
}
// 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) err = d.store.Settings.Save(set)
checkErr(err) checkErr(err)
err = d.store.Settings.SaveServer(ser) err = d.store.Settings.SaveServer(ser)