add viper to config, simplify shared flags
This commit is contained in:
parent
82363f1d12
commit
25c009a560
@ -12,6 +12,8 @@ import (
|
|||||||
"github.com/filebrowser/filebrowser/v2/errors"
|
"github.com/filebrowser/filebrowser/v2/errors"
|
||||||
"github.com/filebrowser/filebrowser/v2/settings"
|
"github.com/filebrowser/filebrowser/v2/settings"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
v "github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -26,21 +28,27 @@ var configCmd = &cobra.Command{
|
|||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
func addConfigFlags(cmd *cobra.Command) {
|
func addConfigFlags(f *pflag.FlagSet) {
|
||||||
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")
|
|
||||||
|
|
||||||
cmd.Flags().String("auth.method", string(auth.MethodJSONAuth), "authentication type")
|
addUserFlags(f)
|
||||||
cmd.Flags().String("auth.header", "", "HTTP header for auth.method=proxy")
|
|
||||||
|
|
||||||
cmd.Flags().String("recaptcha.host", "https://www.google.com", "use another host for ReCAPTCHA. recaptcha.net might be useful in China")
|
vaddP(f, "signup", "s", false, "allow users to signup")
|
||||||
cmd.Flags().String("recaptcha.key", "", "ReCaptcha site key")
|
vadd(f, "shell", "", "shell command to which other commands should be appended")
|
||||||
cmd.Flags().String("recaptcha.secret", "", "ReCaptcha secret")
|
|
||||||
|
|
||||||
cmd.Flags().String("branding.name", "", "replace 'File Browser' by this name")
|
vadd(f, "auth.method", string(auth.MethodJSONAuth), "authentication type")
|
||||||
cmd.Flags().String("branding.files", "", "path to directory with images and custom styles")
|
vadd(f, "auth.header", "", "HTTP header for auth.method=proxy")
|
||||||
cmd.Flags().Bool("branding.disableExternal", false, "disable external links such as GitHub links")
|
|
||||||
|
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) {
|
func getAuthentication(cmd *cobra.Command) (settings.AuthMethod, auth.Auther) {
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
configCmd.AddCommand(configInitCmd)
|
configCmd.AddCommand(configInitCmd)
|
||||||
addConfigFlags(configInitCmd)
|
addConfigFlags(configInitCmd.Flags())
|
||||||
}
|
}
|
||||||
|
|
||||||
var configInitCmd = &cobra.Command{
|
var configInitCmd = &cobra.Command{
|
||||||
|
|||||||
@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
configCmd.AddCommand(configSetCmd)
|
configCmd.AddCommand(configSetCmd)
|
||||||
addConfigFlags(configSetCmd)
|
addConfigFlags(configSetCmd.Flags())
|
||||||
}
|
}
|
||||||
|
|
||||||
var configSetCmd = &cobra.Command{
|
var configSetCmd = &cobra.Command{
|
||||||
@ -44,6 +44,7 @@ you want to change.`,
|
|||||||
getUserDefaults(cmd, &s.Defaults, false)
|
getUserDefaults(cmd, &s.Defaults, false)
|
||||||
|
|
||||||
var auther auth.Auther
|
var auther auth.Auther
|
||||||
|
var err error
|
||||||
if hasAuth {
|
if hasAuth {
|
||||||
s.AuthMethod, auther = getAuthentication(cmd)
|
s.AuthMethod, auther = getAuthentication(cmd)
|
||||||
err = d.store.Auth.Save(auther)
|
err = d.store.Auth.Save(auther)
|
||||||
|
|||||||
@ -9,8 +9,8 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/pflag"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
18
cmd/root.go
18
cmd/root.go
@ -2,7 +2,6 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"errors"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
@ -57,8 +56,8 @@ func init() {
|
|||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "filebrowser",
|
Use: "filebrowser",
|
||||||
Short: "A stylish web-based file browser",
|
|
||||||
Version: version.Version,
|
Version: version.Version,
|
||||||
|
Short: "A stylish web-based file browser",
|
||||||
Long: `File Browser CLI lets you create the database to use with 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
|
manage your users and all the configurations without acessing the
|
||||||
web interface.
|
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
|
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
|
the quick setup mode and a new database will be bootstraped and a new
|
||||||
user created with the credentials from options "username" and "password".`,
|
user created with the credentials from options "username" and "password".`,
|
||||||
|
|
||||||
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
||||||
|
|
||||||
|
log.Println(cfgFile)
|
||||||
|
|
||||||
switch logMethod := v.GetString("log"); logMethod {
|
switch logMethod := v.GetString("log"); logMethod {
|
||||||
case "stdout":
|
case "stdout":
|
||||||
log.SetOutput(os.Stdout)
|
log.SetOutput(os.Stdout)
|
||||||
@ -114,6 +117,8 @@ user created with the credentials from options "username" and "password".`,
|
|||||||
quickSetup(d)
|
quickSetup(d)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: check if these fields (including baseurl) are available in the DB. proceed according to --force
|
||||||
|
|
||||||
port := v.GetInt("port")
|
port := v.GetInt("port")
|
||||||
address := v.GetString("address")
|
address := v.GetString("address")
|
||||||
cert := v.GetString("cert")
|
cert := v.GetString("cert")
|
||||||
@ -158,6 +163,9 @@ user created with the credentials from options "username" and "password".`,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func quickSetup(d pythonData) {
|
func quickSetup(d pythonData) {
|
||||||
|
|
||||||
|
// TODO: save also port, address, cert, key, scope; if their values differ from defaults
|
||||||
|
|
||||||
set := &settings.Settings{
|
set := &settings.Settings{
|
||||||
Key: generateRandomBytes(64), // 256 bit
|
Key: generateRandomBytes(64), // 256 bit
|
||||||
BaseURL: v.GetString("baseurl"),
|
BaseURL: v.GetString("baseurl"),
|
||||||
@ -194,7 +202,7 @@ func quickSetup(d pythonData) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if username == "" || password == "" {
|
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{
|
user := &users.User{
|
||||||
@ -230,7 +238,7 @@ func initConfig() {
|
|||||||
if _, ok := err.(v.ConfigParseError); ok {
|
if _, ok := err.(v.ConfigParseError); ok {
|
||||||
panic(err)
|
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()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -42,14 +42,11 @@ func runRules(st *storage.Storage, cmd *cobra.Command, users func(*users.User),
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
settings, err := st.Settings.Get()
|
|
||||||
checkErr(err)
|
|
||||||
|
|
||||||
if global != nil {
|
if global != nil {
|
||||||
global(settings)
|
global(settings)
|
||||||
}
|
}
|
||||||
|
|
||||||
printRules(settings.Rules, id)
|
printRules(s.Rules, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUserIdentifier(cmd *cobra.Command) interface{} {
|
func getUserIdentifier(cmd *cobra.Command) interface{} {
|
||||||
|
|||||||
32
cmd/users.go
32
cmd/users.go
@ -59,22 +59,22 @@ func parseUsernameOrID(arg string) (string, uint) {
|
|||||||
return "", uint(id)
|
return "", uint(id)
|
||||||
}
|
}
|
||||||
|
|
||||||
func addUserFlags(cmd *cobra.Command) {
|
func addUserFlags(f *pflag.FlagSet) {
|
||||||
cmd.Flags().Bool("perm.admin", false, "admin perm for users")
|
f.Bool("perm.admin", false, "admin perm for users")
|
||||||
cmd.Flags().Bool("perm.execute", true, "execute perm for users")
|
f.Bool("perm.execute", true, "execute perm for users")
|
||||||
cmd.Flags().Bool("perm.create", true, "create perm for users")
|
f.Bool("perm.create", true, "create perm for users")
|
||||||
cmd.Flags().Bool("perm.rename", true, "rename perm for users")
|
f.Bool("perm.rename", true, "rename perm for users")
|
||||||
cmd.Flags().Bool("perm.modify", true, "modify perm for users")
|
f.Bool("perm.modify", true, "modify perm for users")
|
||||||
cmd.Flags().Bool("perm.delete", true, "delete perm for users")
|
f.Bool("perm.delete", true, "delete perm for users")
|
||||||
cmd.Flags().Bool("perm.share", true, "share perm for users")
|
f.Bool("perm.share", true, "share perm for users")
|
||||||
cmd.Flags().Bool("perm.download", true, "download perm for users")
|
f.Bool("perm.download", true, "download perm for users")
|
||||||
cmd.Flags().String("sorting.by", "name", "sorting mode (name, size or modified)")
|
f.String("sorting.by", "name", "sorting mode (name, size or modified)")
|
||||||
cmd.Flags().Bool("sorting.asc", false, "sorting by ascending order")
|
f.Bool("sorting.asc", false, "sorting by ascending order")
|
||||||
cmd.Flags().Bool("lockPassword", false, "lock password")
|
f.Bool("lockPassword", false, "lock password")
|
||||||
cmd.Flags().StringSlice("commands", nil, "a list of the commands a user can execute")
|
f.StringSlice("commands", nil, "a list of the commands a user can execute")
|
||||||
cmd.Flags().String("scope", ".", "scope for users")
|
f.String("scope", ".", "scope for users")
|
||||||
cmd.Flags().String("locale", "en", "locale for users")
|
f.String("locale", "en", "locale for users")
|
||||||
cmd.Flags().String("viewMode", string(users.ListViewMode), "view mode for users")
|
f.String("viewMode", string(users.ListViewMode), "view mode for users")
|
||||||
}
|
}
|
||||||
|
|
||||||
func getViewMode(cmd *cobra.Command) users.ViewMode {
|
func getViewMode(cmd *cobra.Command) users.ViewMode {
|
||||||
|
|||||||
@ -7,7 +7,7 @@ import (
|
|||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
usersCmd.AddCommand(usersAddCmd)
|
usersCmd.AddCommand(usersAddCmd)
|
||||||
addUserFlags(usersAddCmd)
|
addUserFlags(usersAddCmd.Flags())
|
||||||
}
|
}
|
||||||
|
|
||||||
var usersAddCmd = &cobra.Command{
|
var usersAddCmd = &cobra.Command{
|
||||||
|
|||||||
@ -11,7 +11,7 @@ func init() {
|
|||||||
|
|
||||||
usersUpdateCmd.Flags().StringP("password", "p", "", "new password")
|
usersUpdateCmd.Flags().StringP("password", "p", "", "new password")
|
||||||
usersUpdateCmd.Flags().StringP("username", "u", "", "new username")
|
usersUpdateCmd.Flags().StringP("username", "u", "", "new username")
|
||||||
addUserFlags(usersUpdateCmd)
|
addUserFlags(usersUpdateCmd.Flags())
|
||||||
}
|
}
|
||||||
|
|
||||||
var usersUpdateCmd = &cobra.Command{
|
var usersUpdateCmd = &cobra.Command{
|
||||||
@ -28,6 +28,7 @@ options you want to change.`,
|
|||||||
var err error
|
var err error
|
||||||
var user *users.User
|
var user *users.User
|
||||||
|
|
||||||
|
var err error
|
||||||
if id != 0 {
|
if id != 0 {
|
||||||
user, err = d.store.Users.Get("", id)
|
user, err = d.store.Users.Get("", id)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user