Skip to content

Commit

Permalink
Refactor the code
Browse files Browse the repository at this point in the history
  • Loading branch information
unicod3 committed May 20, 2019
1 parent c3a4e33 commit 030f660
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
20 changes: 13 additions & 7 deletions cmd/config.go
Expand Up @@ -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)
}
}
}
}

Expand Down
22 changes: 8 additions & 14 deletions cmd/config_set.go
Expand Up @@ -3,7 +3,6 @@ package cmd
import (
"strings"

"github.com/filebrowser/filebrowser/v2/auth"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 030f660

Please sign in to comment.