add helper funcs to set pflags and viper at once

This commit is contained in:
1138-4EB 2018-08-11 19:33:07 +01:00
parent c407502fd2
commit 531a07d393

View File

@ -3,7 +3,6 @@ package cmd
import ( import (
filebrowser "github.com/filebrowser/filebrowser/lib" filebrowser "github.com/filebrowser/filebrowser/lib"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag"
v "github.com/spf13/viper" v "github.com/spf13/viper"
) )
@ -30,103 +29,71 @@ func init() {
f := serveCmd.PersistentFlags() f := serveCmd.PersistentFlags()
flag := func(k string, i interface{}, u string) {
switch y := i.(type) {
case bool:
f.Bool(k, y, u)
case int:
f.Int(k, y, u)
case string:
f.String(k, y, u)
}
v.SetDefault(k, i)
}
flagP := func(k, p string, i interface{}, u string) {
switch y := i.(type) {
case bool:
f.BoolP(k, p, y, u)
case int:
f.IntP(k, p, y, u)
case string:
f.StringP(k, p, y, u)
}
v.SetDefault(k, i)
}
deprecated := func(k string, i interface{}, u, m string) {
switch y := i.(type) {
case bool:
f.Bool(k, y, u)
case int:
f.Int(k, y, u)
case string:
f.String(k, y, u)
}
f.MarkDeprecated(k, m)
}
// Global settings // Global settings
port := 0 flagP("port", "p", 0, "HTTP Port (default is random)")
addr := "" flagP("address", "a", "", "Address to listen to (default is all of them)")
database := "./filebrowser.db" flagP("database", "d", "./filebrowser.db", "Database file")
logfile := "stdout" flagP("log", "l", "stdout", "Errors logger; can use 'stdout', 'stderr' or file")
baseurl := "" flagP("baseurl", "b", "", "Base URL")
prefixurl := "" flag("prefixurl", "", "Prefix URL")
staticg := "" flag("staticgen", "", "Static Generator you want to enable")
f.IntP("port", "p", port, "HTTP Port (default is random)")
f.StringP("address", "a", addr, "Address to listen to (default is all of them)")
f.StringP("database", "d", database, "Database file")
f.StringP("log", "l", logfile, "Errors logger; can use 'stdout', 'stderr' or file")
f.StringP("baseurl", "b", baseurl, "Base URL")
f.String("prefixurl", prefixurl, "Prefix URL")
f.String("staticgen", staticg, "Static Generator you want to enable")
v.SetDefault("port", port)
v.SetDefault("address", addr)
v.SetDefault("database", database)
v.SetDefault("log", logfile)
v.SetDefault("baseurl", baseurl)
v.SetDefault("prefixurl", prefixurl)
v.SetDefault("staticgen", staticg)
// User default settings // User default settings
var defaults = struct { f.String("defaults.commands", "git svn hg", "Default commands option for new users")
commands string
scope string
viewMode string
allowCommands bool
allowEdit bool
allowNew bool
allowPublish bool
locale string
}{
"git svn hg",
".",
filebrowser.MosaicViewMode,
true,
true,
true,
true,
"",
}
f.String("defaults.commands", defaults.commands, "Default commands option for new users")
f.StringP("defaults.scope", "s", defaults.scope, "Default scope option for new users")
f.String("defaults.viewMode", defaults.viewMode, "Default view mode for new users")
f.Bool("defaults.allowCommands", defaults.allowCommands, "Default allow commands option for new users")
f.Bool("defaults.allowEdit", defaults.allowEdit, "Default allow edit option for new users")
f.Bool("defaults.allowNew", defaults.allowNew, "Default allow new option for new users")
f.Bool("defaults.allowPublish", defaults.allowPublish, "Default allow publish option for new users")
f.String("defaults.locale", defaults.locale, "Default locale for new users, set it empty to enable auto detect from browser")
v.SetDefault("defaults.scope", defaults.scope)
v.SetDefault("defaults.commands", []string{"git", "svn", "hg"}) v.SetDefault("defaults.commands", []string{"git", "svn", "hg"})
v.SetDefault("defaults.viewMode", defaults.viewMode)
v.SetDefault("defaults.allowCommands", defaults.allowCommands) flagP("defaults.scope", "s", ".", "Default scope option for new users")
v.SetDefault("defaults.allowEdit", defaults.allowEdit) flag("defaults.viewMode", filebrowser.MosaicViewMode, "Default view mode for new users")
v.SetDefault("defaults.allowNew", defaults.allowNew) flag("defaults.allowCommands", true, "Default allow commands option for new users")
v.SetDefault("defaults.allowPublish", defaults.allowPublish) flag("defaults.allowEdit", true, "Default allow edit option for new users")
v.SetDefault("defaults.locale", defaults.locale) flag("defaults.allowNew", true, "Default allow new option for new users")
flag("defaults.allowPublish", true, "Default allow publish option for new users")
flag("defaults.locale", "", "Default locale for new users, set it empty to enable auto detect from browser")
// Recaptcha settings // Recaptcha settings
var recaptcha = struct { flag("recaptcha.host", "https://www.google.com", "Use another host for ReCAPTCHA. recaptcha.net might be useful in China")
host string flag("recaptcha.key", "", "ReCaptcha site key")
key string flag("recaptcha.secret", "", "ReCaptcha secret")
secret string
}{
"https://www.google.com",
"",
"",
}
f.String("recaptcha.host", recaptcha.host, "Use another host for ReCAPTCHA. recaptcha.net might be useful in China")
f.String("recaptcha.key", recaptcha.key, "ReCaptcha site key")
f.String("recaptcha.secret", recaptcha.secret, "ReCaptcha secret")
v.SetDefault("recaptcha.host", recaptcha.host)
v.SetDefault("recaptcha.key", recaptcha.key)
v.SetDefault("recaptcha.secret", recaptcha.secret)
// Auth settings // Auth settings
var auth = struct { flag("auth.method", "default", "Switch between 'none', 'default' and 'proxy' authentication")
method string flag("auth.header", "X-Forwarded-User", "The header name used for proxy authentication")
header string
}{
"default",
"X-Forwarded-User",
}
f.String("auth.method", auth.method, "Switch between 'none', 'default' and 'proxy' authentication")
f.String("auth.header", auth.header, "The header name used for proxy authentication")
v.SetDefault("auth.method", auth.method)
v.SetDefault("auth.header", auth.header)
// Bind the full flag set to the configuration // Bind the full flag set to the configuration
if err := v.BindPFlags(f); err != nil { if err := v.BindPFlags(f); err != nil {
@ -134,26 +101,16 @@ func init() {
} }
// Deprecated flags // Deprecated flags
Deprecated(f, "no-auth", false, "Disables authentication", "use --auth.method='none' instead") deprecated("no-auth", false, "Disables authentication", "use --auth.method='none' instead")
Deprecated(f, "alternative-recaptcha", false, "Use recaptcha.net for serving and handling, useful in China", "use --recaptcha.host instead") deprecated("alternative-recaptcha", false, "Use recaptcha.net for serving and handling, useful in China", "use --recaptcha.host instead")
Deprecated(f, "recaptcha-key", "", "ReCaptcha site key", "use --recaptcha.key instead") deprecated("recaptcha-key", "", "ReCaptcha site key", "use --recaptcha.key instead")
Deprecated(f, "recaptcha-secret", "", "ReCaptcha secret", "use --recaptcha.secret instead") deprecated("recaptcha-secret", "", "ReCaptcha secret", "use --recaptcha.secret instead")
Deprecated(f, "scope", ".", "Default scope option for new users", "use --defaults.scope instead") deprecated("scope", ".", "Default scope option for new users", "use --defaults.scope instead")
Deprecated(f, "commands", "git svn hg", "Default commands option for new users", "use --defaults.commands instead") deprecated("commands", "git svn hg", "Default commands option for new users", "use --defaults.commands instead")
Deprecated(f, "view-mode", "mosaic", "Default view mode for new users", "use --defaults.viewMode instead") deprecated("view-mode", "mosaic", "Default view mode for new users", "use --defaults.viewMode instead")
Deprecated(f, "locale", "", "Default locale for new users, set it empty to enable auto detect from browser", "use --defaults.locale instead") deprecated("locale", "", "Default locale for new users, set it empty to enable auto detect from browser", "use --defaults.locale instead")
Deprecated(f, "allow-commands", true, "Default allow commands option for new users", "use --defaults.allowCommands instead") deprecated("allow-commands", true, "Default allow commands option for new users", "use --defaults.allowCommands instead")
Deprecated(f, "allow-edit", true, "Default allow edit option for new users", "use --defaults.allowEdit instead") deprecated("allow-edit", true, "Default allow edit option for new users", "use --defaults.allowEdit instead")
Deprecated(f, "allow-publish", true, "Default allow publish option for new users", "use --defaults.allowPublish instead") deprecated("allow-publish", true, "Default allow publish option for new users", "use --defaults.allowPublish instead")
Deprecated(f, "allow-new", true, "Default allow new option for new users", "use --defaults.allowNew instead") deprecated("allow-new", true, "Default allow new option for new users", "use --defaults.allowNew instead")
}
func Deprecated(f *pflag.FlagSet, k string, i interface{}, u, m string) {
switch v := i.(type) {
case bool:
f.Bool(k, v, u)
case string:
f.String(k, v, u)
}
f.MarkDeprecated(k, m)
} }