add viper to config, simplify shared flags

This commit is contained in:
1138-4EB 2019-01-06 19:40:37 +01:00
parent 82363f1d12
commit 25c009a560
9 changed files with 57 additions and 42 deletions

View File

@ -12,6 +12,8 @@ import (
"github.com/filebrowser/filebrowser/v2/errors"
"github.com/filebrowser/filebrowser/v2/settings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
v "github.com/spf13/viper"
)
func init() {
@ -26,21 +28,27 @@ var configCmd = &cobra.Command{
Args: cobra.NoArgs,
}
func addConfigFlags(cmd *cobra.Command) {
addUserFlags(cmd)
cmd.Flags().BoolP("signup", "s", false, "allow users to signup")
cmd.Flags().String("shell", "", "shell command to which other commands should be appended")
func addConfigFlags(f *pflag.FlagSet) {
cmd.Flags().String("auth.method", string(auth.MethodJSONAuth), "authentication type")
cmd.Flags().String("auth.header", "", "HTTP header for auth.method=proxy")
addUserFlags(f)
cmd.Flags().String("recaptcha.host", "https://www.google.com", "use another host for ReCAPTCHA. recaptcha.net might be useful in China")
cmd.Flags().String("recaptcha.key", "", "ReCaptcha site key")
cmd.Flags().String("recaptcha.secret", "", "ReCaptcha secret")
vaddP(f, "signup", "s", false, "allow users to signup")
vadd(f, "shell", "", "shell command to which other commands should be appended")
cmd.Flags().String("branding.name", "", "replace 'File Browser' by this name")
cmd.Flags().String("branding.files", "", "path to directory with images and custom styles")
cmd.Flags().Bool("branding.disableExternal", false, "disable external links such as GitHub links")
vadd(f, "auth.method", string(auth.MethodJSONAuth), "authentication type")
vadd(f, "auth.header", "", "HTTP header for auth.method=proxy")
vadd(f, "recaptcha.host", "https://www.google.com", "use another host for ReCAPTCHA. recaptcha.net might be useful in China")
vadd(f, "recaptcha.key", "", "ReCaptcha site key")
vadd(f, "recaptcha.secret", "", "ReCaptcha secret")
vadd(f, "branding.name", "", "replace 'File Browser' by this name")
vadd(f, "branding.files", "", "path to directory with images and custom styles")
vadd(f, "branding.disableExternal", false, "disable external links such as GitHub links")
if err := v.BindPFlags(f); err != nil {
panic(err)
}
}
func getAuthentication(cmd *cobra.Command) (settings.AuthMethod, auth.Auther) {

View File

@ -10,7 +10,7 @@ import (
func init() {
configCmd.AddCommand(configInitCmd)
addConfigFlags(configInitCmd)
addConfigFlags(configInitCmd.Flags())
}
var configInitCmd = &cobra.Command{

View File

@ -10,7 +10,7 @@ import (
func init() {
configCmd.AddCommand(configSetCmd)
addConfigFlags(configSetCmd)
addConfigFlags(configSetCmd.Flags())
}
var configSetCmd = &cobra.Command{
@ -44,6 +44,7 @@ you want to change.`,
getUserDefaults(cmd, &s.Defaults, false)
var auther auth.Auther
var err error
if hasAuth {
s.AuthMethod, auther = getAuthentication(cmd)
err = d.store.Auth.Save(auther)

View File

@ -9,8 +9,8 @@ import (
"sort"
"strings"
"github.com/spf13/pflag"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
func init() {

View File

@ -2,7 +2,6 @@ package cmd
import (
"crypto/tls"
"errors"
"io/ioutil"
"log"
"net"
@ -57,8 +56,8 @@ func init() {
var rootCmd = &cobra.Command{
Use: "filebrowser",
Short: "A stylish web-based file browser",
Version: version.Version,
Short: "A stylish web-based file browser",
Long: `File Browser CLI lets you create the database to use with File Browser,
manage your users and all the configurations without acessing the
web interface.
@ -93,7 +92,11 @@ set FB_DATABASE equals to the path.
Also, if the database path doesn't exist, File Browser will enter into
the quick setup mode and a new database will be bootstraped and a new
user created with the credentials from options "username" and "password".`,
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
log.Println(cfgFile)
switch logMethod := v.GetString("log"); logMethod {
case "stdout":
log.SetOutput(os.Stdout)
@ -114,6 +117,8 @@ user created with the credentials from options "username" and "password".`,
quickSetup(d)
}
// TODO: check if these fields (including baseurl) are available in the DB. proceed according to --force
port := v.GetInt("port")
address := v.GetString("address")
cert := v.GetString("cert")
@ -158,6 +163,9 @@ user created with the credentials from options "username" and "password".`,
}
func quickSetup(d pythonData) {
// TODO: save also port, address, cert, key, scope; if their values differ from defaults
set := &settings.Settings{
Key: generateRandomBytes(64), // 256 bit
BaseURL: v.GetString("baseurl"),
@ -194,7 +202,7 @@ func quickSetup(d pythonData) {
}
if username == "" || password == "" {
checkErr(errors.New("username and password cannot be empty during quick setup"))
log.Fatal("username and password cannot be empty during quick setup")
}
user := &users.User{
@ -230,7 +238,7 @@ func initConfig() {
if _, ok := err.(v.ConfigParseError); ok {
panic(err)
}
// TODO: log.Println("No config file provided")
cfgFile = "No config file used"
}
// else TODO: log.Println("Using config file:", v.ConfigFileUsed())
cfgFile = "Using config file: " + v.ConfigFileUsed()
}

View File

@ -42,14 +42,11 @@ func runRules(st *storage.Storage, cmd *cobra.Command, users func(*users.User),
return
}
settings, err := st.Settings.Get()
checkErr(err)
if global != nil {
global(settings)
}
printRules(settings.Rules, id)
printRules(s.Rules, id)
}
func getUserIdentifier(cmd *cobra.Command) interface{} {

View File

@ -59,22 +59,22 @@ func parseUsernameOrID(arg string) (string, uint) {
return "", uint(id)
}
func addUserFlags(cmd *cobra.Command) {
cmd.Flags().Bool("perm.admin", false, "admin perm for users")
cmd.Flags().Bool("perm.execute", true, "execute perm for users")
cmd.Flags().Bool("perm.create", true, "create perm for users")
cmd.Flags().Bool("perm.rename", true, "rename perm for users")
cmd.Flags().Bool("perm.modify", true, "modify perm for users")
cmd.Flags().Bool("perm.delete", true, "delete perm for users")
cmd.Flags().Bool("perm.share", true, "share perm for users")
cmd.Flags().Bool("perm.download", true, "download perm for users")
cmd.Flags().String("sorting.by", "name", "sorting mode (name, size or modified)")
cmd.Flags().Bool("sorting.asc", false, "sorting by ascending order")
cmd.Flags().Bool("lockPassword", false, "lock password")
cmd.Flags().StringSlice("commands", nil, "a list of the commands a user can execute")
cmd.Flags().String("scope", ".", "scope for users")
cmd.Flags().String("locale", "en", "locale for users")
cmd.Flags().String("viewMode", string(users.ListViewMode), "view mode for users")
func addUserFlags(f *pflag.FlagSet) {
f.Bool("perm.admin", false, "admin perm for users")
f.Bool("perm.execute", true, "execute perm for users")
f.Bool("perm.create", true, "create perm for users")
f.Bool("perm.rename", true, "rename perm for users")
f.Bool("perm.modify", true, "modify perm for users")
f.Bool("perm.delete", true, "delete perm for users")
f.Bool("perm.share", true, "share perm for users")
f.Bool("perm.download", true, "download perm for users")
f.String("sorting.by", "name", "sorting mode (name, size or modified)")
f.Bool("sorting.asc", false, "sorting by ascending order")
f.Bool("lockPassword", false, "lock password")
f.StringSlice("commands", nil, "a list of the commands a user can execute")
f.String("scope", ".", "scope for users")
f.String("locale", "en", "locale for users")
f.String("viewMode", string(users.ListViewMode), "view mode for users")
}
func getViewMode(cmd *cobra.Command) users.ViewMode {

View File

@ -7,7 +7,7 @@ import (
func init() {
usersCmd.AddCommand(usersAddCmd)
addUserFlags(usersAddCmd)
addUserFlags(usersAddCmd.Flags())
}
var usersAddCmd = &cobra.Command{

View File

@ -11,7 +11,7 @@ func init() {
usersUpdateCmd.Flags().StringP("password", "p", "", "new password")
usersUpdateCmd.Flags().StringP("username", "u", "", "new username")
addUserFlags(usersUpdateCmd)
addUserFlags(usersUpdateCmd.Flags())
}
var usersUpdateCmd = &cobra.Command{
@ -28,6 +28,7 @@ options you want to change.`,
var err error
var user *users.User
var err error
if id != 0 {
user, err = d.store.Users.Get("", id)
} else {