diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 19b7cc8f..f8db1102 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -1,8 +1,7 @@ package cmd import ( - "fmt" - "os" + "log" "strings" homedir "github.com/mitchellh/go-homedir" @@ -29,8 +28,7 @@ easily, everywhere, every time.`, func Execute() { checkRootAlias() if err := rootCmd.Execute(); err != nil { - fmt.Println(err) - os.Exit(1) + panic(err) } } @@ -47,8 +45,7 @@ func initConfig() { // Find home directory. home, err := homedir.Dir() if err != nil { - fmt.Println(err) - os.Exit(1) + panic(err) } v.AddConfigPath(".") v.AddConfigPath(home) @@ -68,6 +65,6 @@ func initConfig() { panic(err) } } else { - fmt.Println("Using config file:", v.ConfigFileUsed()) + log.Println("Using config file:", v.ConfigFileUsed()) } } diff --git a/cli/cmd/serve.go b/cli/cmd/serve.go index d86fcf55..b4519c7e 100644 --- a/cli/cmd/serve.go +++ b/cli/cmd/serve.go @@ -29,7 +29,6 @@ func init() { rootCmd.AddCommand(serveCmd) f := serveCmd.PersistentFlags() - l := f.Lookup // Global settings port := 0 @@ -48,21 +47,13 @@ func init() { 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("Logger", logfile) - v.SetDefault("BaseURL", baseurl) - v.SetDefault("PrefixURL", prefixurl) - v.SetDefault("StaticGen", staticg) - - v.BindPFlag("Port", l("port")) - v.BindPFlag("Address", l("address")) - v.BindPFlag("Database", l("database")) - v.BindPFlag("Logger", l("log")) - v.BindPFlag("BaseURL", l("baseurl")) - v.BindPFlag("PrefixURL", l("prefixurl")) - v.BindPFlag("StaticGen", l("staticgen")) + 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 var defaults = struct { @@ -94,23 +85,14 @@ func init() { 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.ViewMode", defaults.viewMode) - v.SetDefault("Defaults.AllowCommmands", defaults.allowCommands) - v.SetDefault("Defaults.AllowEdit", defaults.allowEdit) - v.SetDefault("Defaults.AllowNew", defaults.allowNew) - v.SetDefault("Defaults.AllowPublish", defaults.allowPublish) - v.SetDefault("Defaults.Locale", defaults.locale) - - v.BindPFlag("Defaults.Scope", l("defaults.scope")) - v.BindPFlag("Defaults.Commands", l("defaults.commands")) - v.BindPFlag("Defaults.ViewMode", l("defaults.viewMode")) - v.BindPFlag("Defaults.AllowCommands", l("defaults.allowCommands")) - v.BindPFlag("Defaults.AllowEdit", l("defaults.allowEdit")) - v.BindPFlag("Defaults.AllowNew", l("defaults.allowNew")) - v.BindPFlag("Defaults.AllowPublish", l("defaults.allowPublish")) - v.BindPFlag("Defaults.Locale", l("defaults.locale")) + v.SetDefault("defaults.scope", defaults.scope) + v.SetDefault("defaults.commands", []string{"git", "svn", "hg"}) + v.SetDefault("defaults.viewMode", defaults.viewMode) + v.SetDefault("defaults.allowCommands", defaults.allowCommands) + v.SetDefault("defaults.allowEdit", defaults.allowEdit) + v.SetDefault("defaults.allowNew", defaults.allowNew) + v.SetDefault("defaults.allowPublish", defaults.allowPublish) + v.SetDefault("defaults.locale", defaults.locale) // Recaptcha settings var recaptcha = struct { @@ -127,13 +109,9 @@ func init() { 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) - - v.BindPFlag("Recaptcha.Host", l("recaptcha.host")) - v.BindPFlag("Recaptcha.Key", l("recaptcha.key")) - v.BindPFlag("Recaptcha.Secret", l("recaptcha.secret")) + v.SetDefault("recaptcha.host", recaptcha.host) + v.SetDefault("recaptcha.key", recaptcha.key) + v.SetDefault("recaptcha.secret", recaptcha.secret) // Auth settings var auth = struct { @@ -147,11 +125,13 @@ func init() { 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) + v.SetDefault("auth.method", auth.method) + v.SetDefault("auth.header", auth.header) - v.BindPFlag("Auth.Method", l("auth.method")) - v.BindPFlag("Auth.Header", l("auth.header")) + // Bind the full flag set to the configuration + if err := v.BindPFlags(f); err != nil { + panic(err) + } // Deprecated flags Deprecated(f, "no-auth", false, "Disables authentication", "use --auth.method='none' instead") diff --git a/cli/cmd/server.go b/cli/cmd/server.go index f182f3af..042e9877 100644 --- a/cli/cmd/server.go +++ b/cli/cmd/server.go @@ -20,7 +20,7 @@ import ( func Serve() { // Set up process log before anything bad happens. - switch l := viper.GetString("Logger"); l { + switch l := viper.GetString("log"); l { case "stdout": log.SetOutput(os.Stdout) case "stderr": @@ -44,14 +44,14 @@ func Serve() { validMethods["default"] = false validMethods["proxy"] = true - m := viper.GetString("Auth.Method") + m := viper.GetString("auth.method") b, ok := validMethods[m] if !ok { log.Fatal("The property 'auth.method' needs to be set to 'none', 'default' or 'proxy'.") } if b { - if viper.GetString("Auth.Header") == "" { + if viper.GetString("auth.header") == "" { log.Fatal("The 'auth.header' needs to be specified when '", m, "' authentication is used.") } log.Println("[WARN] Filebrowser authentication is configured to '", m, "' authentication. This can cause a huge security issue if the infrastructure is not configured correctly.") @@ -59,7 +59,7 @@ func Serve() { } // Builds the address and a listener. - laddr := viper.GetString("Address") + ":" + viper.GetString("Port") + laddr := viper.GetString("address") + ":" + viper.GetString("port") listener, err := net.Listen("tcp", laddr) if err != nil { log.Fatal(err) @@ -75,33 +75,33 @@ func Serve() { } func handler() http.Handler { - db, err := storm.Open(viper.GetString("Database")) + db, err := storm.Open(viper.GetString("database")) if err != nil { log.Fatal(err) } fb := &filebrowser.FileBrowser{ Auth: &filebrowser.Auth{ - Method: viper.GetString("Auth.Method"), - Header: viper.GetString("Auth.Header"), + Method: viper.GetString("auth.method"), + Header: viper.GetString("auth.header"), }, ReCaptcha: &filebrowser.ReCaptcha{ - Host: viper.GetString("Recaptcha.Host"), - Key: viper.GetString("Recaptcha.Key"), - Secret: viper.GetString("Recaptcha.Secret"), + Host: viper.GetString("recaptcha.host"), + Key: viper.GetString("recaptcha.key"), + Secret: viper.GetString("recaptcha.secret"), }, DefaultUser: &filebrowser.User{ - AllowCommands: viper.GetBool("Defaults.AllowCommands"), - AllowEdit: viper.GetBool("Defaults.AllowEdit"), - AllowNew: viper.GetBool("Defaults.AllowNew"), - AllowPublish: viper.GetBool("Defaults.AllowPublish"), - Commands: viper.GetStringSlice("Defaults.Commands"), + AllowCommands: viper.GetBool("defaults.allowCommands"), + AllowEdit: viper.GetBool("defaults.allowEdit"), + AllowNew: viper.GetBool("defaults.allowNew"), + AllowPublish: viper.GetBool("defaults.allowPublish"), + Commands: viper.GetStringSlice("defaults.commands"), Rules: []*filebrowser.Rule{}, - Locale: viper.GetString("Defaults.Locale"), + Locale: viper.GetString("defaults.locale"), CSS: "", - Scope: viper.GetString("Defaults.Scope"), - FileSystem: fileutils.Dir(viper.GetString("Defaults.Scope")), - ViewMode: viper.GetString("Defaults.ViewMode"), + Scope: viper.GetString("defaults.scope"), + FileSystem: fileutils.Dir(viper.GetString("defaults.scope")), + ViewMode: viper.GetString("defaults.viewMode"), }, Store: &filebrowser.Store{ Config: bolt.ConfigStore{DB: db}, @@ -113,15 +113,15 @@ func handler() http.Handler { }, } - fb.SetBaseURL(viper.GetString("BaseURL")) - fb.SetPrefixURL(viper.GetString("PrefixURL")) + fb.SetBaseURL(viper.GetString("baseurl")) + fb.SetPrefixURL(viper.GetString("prefixurl")) err = fb.Setup() if err != nil { log.Fatal(err) } - switch viper.GetString("StaticGen") { + switch viper.GetString("staticgen") { case "hugo": hugo := &staticgen.Hugo{ Root: viper.GetString("Scope"), diff --git a/lib/http/http.go b/lib/http/http.go index f75e4807..cadc893d 100644 --- a/lib/http/http.go +++ b/lib/http/http.go @@ -227,7 +227,7 @@ func renderFile(c *fb.Context, w http.ResponseWriter, file string) (int, error) w.Header().Set("Content-Type", contentType+"; charset=utf-8") data := map[string]interface{}{ - "BaseURL": c.RootURL(), + "baseurl": c.RootURL(), "NoAuth": c.Auth.Method == "none", "Version": fb.Version, "CSS": template.CSS(c.CSS), @@ -237,7 +237,7 @@ func renderFile(c *fb.Context, w http.ResponseWriter, file string) (int, error) } if c.StaticGen != nil { - data["StaticGen"] = c.StaticGen.Name() + data["staticgen"] = c.StaticGen.Name() } err := tpl.Execute(w, data) @@ -291,7 +291,7 @@ func sharePage(c *fb.Context, w http.ResponseWriter, r *http.Request) (int, erro w.Header().Set("Content-Type", "text/html; charset=utf-8") err := tpl.Execute(w, map[string]interface{}{ - "BaseURL": c.RootURL(), + "baseurl": c.RootURL(), "File": c.File, })