From fc06f642f2b1bce90430473fff9f6ac9638854d6 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Tue, 8 Jan 2019 10:50:37 +0000 Subject: [PATCH] feat: simplify flag adding License: MIT Signed-off-by: Henrique Dias --- cmd/config.go | 25 +++++++++++++------------ cmd/config_init.go | 2 +- cmd/config_set.go | 2 +- cmd/users.go | 32 ++++++++++++++++---------------- cmd/users_add.go | 2 +- cmd/users_update.go | 2 +- 6 files changed, 33 insertions(+), 32 deletions(-) diff --git a/cmd/config.go b/cmd/config.go index ac14bae5..2034e0aa 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -12,6 +12,7 @@ import ( "github.com/filebrowser/filebrowser/v2/errors" "github.com/filebrowser/filebrowser/v2/settings" "github.com/spf13/cobra" + "github.com/spf13/pflag" ) func init() { @@ -25,21 +26,21 @@ 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(flags *pflag.FlagSet) { + addUserFlags(flags) + flags.BoolP("signup", "s", false, "allow users to signup") + flags.String("shell", "", "shell command to which other commands should be appended") - cmd.Flags().String("auth.method", string(auth.MethodJSONAuth), "authentication type") - cmd.Flags().String("auth.header", "", "HTTP header for auth.method=proxy") + flags.String("auth.method", string(auth.MethodJSONAuth), "authentication type") + 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") - cmd.Flags().String("recaptcha.key", "", "ReCaptcha site key") - cmd.Flags().String("recaptcha.secret", "", "ReCaptcha secret") + flags.String("recaptcha.host", "https://www.google.com", "use another host for ReCAPTCHA. recaptcha.net might be useful in China") + flags.String("recaptcha.key", "", "ReCaptcha site key") + flags.String("recaptcha.secret", "", "ReCaptcha secret") - 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") + flags.String("branding.name", "", "replace 'File Browser' by this name") + flags.String("branding.files", "", "path to directory with images and custom styles") + flags.Bool("branding.disableExternal", false, "disable external links such as GitHub links") } func getAuthentication(cmd *cobra.Command) (settings.AuthMethod, auth.Auther) { diff --git a/cmd/config_init.go b/cmd/config_init.go index b46d669a..37a698a9 100644 --- a/cmd/config_init.go +++ b/cmd/config_init.go @@ -10,7 +10,7 @@ import ( func init() { configCmd.AddCommand(configInitCmd) - addConfigFlags(configInitCmd) + addConfigFlags(configInitCmd.Flags()) } var configInitCmd = &cobra.Command{ diff --git a/cmd/config_set.go b/cmd/config_set.go index 0e68adf7..b1e6388f 100644 --- a/cmd/config_set.go +++ b/cmd/config_set.go @@ -10,7 +10,7 @@ import ( func init() { configCmd.AddCommand(configSetCmd) - addConfigFlags(configSetCmd) + addConfigFlags(configSetCmd.Flags()) } var configSetCmd = &cobra.Command{ diff --git a/cmd/users.go b/cmd/users.go index fd707c64..9946c3b0 100644 --- a/cmd/users.go +++ b/cmd/users.go @@ -58,22 +58,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(flags *pflag.FlagSet) { + flags.Bool("perm.admin", false, "admin perm for users") + flags.Bool("perm.execute", true, "execute perm for users") + flags.Bool("perm.create", true, "create perm for users") + flags.Bool("perm.rename", true, "rename perm for users") + flags.Bool("perm.modify", true, "modify perm for users") + flags.Bool("perm.delete", true, "delete perm for users") + flags.Bool("perm.share", true, "share perm for users") + flags.Bool("perm.download", true, "download perm for users") + flags.String("sorting.by", "name", "sorting mode (name, size or modified)") + flags.Bool("sorting.asc", false, "sorting by ascending order") + flags.Bool("lockPassword", false, "lock password") + flags.StringSlice("commands", nil, "a list of the commands a user can execute") + flags.String("scope", ".", "scope for users") + flags.String("locale", "en", "locale for users") + flags.String("viewMode", string(users.ListViewMode), "view mode for users") } func getViewMode(cmd *cobra.Command) users.ViewMode { diff --git a/cmd/users_add.go b/cmd/users_add.go index a8f798d8..295dbb5c 100644 --- a/cmd/users_add.go +++ b/cmd/users_add.go @@ -7,7 +7,7 @@ import ( func init() { usersCmd.AddCommand(usersAddCmd) - addUserFlags(usersAddCmd) + addUserFlags(usersAddCmd.Flags()) } var usersAddCmd = &cobra.Command{ diff --git a/cmd/users_update.go b/cmd/users_update.go index 375101ef..e468f5a5 100644 --- a/cmd/users_update.go +++ b/cmd/users_update.go @@ -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{