From 6bcd0000e81e851ccd49f2ea9b38f5ce5d11b19d Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Mon, 7 Jan 2019 19:59:42 +0000 Subject: [PATCH] wrap calls with python2 --- cmd/cmds_add.go | 10 +++------- cmd/cmds_ls.go | 8 +++----- cmd/cmds_rm.go | 8 +++----- cmd/config_cat.go | 8 +++----- cmd/config_init.go | 20 ++++---------------- cmd/config_set.go | 9 +++------ cmd/users_add.go | 9 +++------ cmd/users_find.go | 9 +++------ cmd/users_rm.go | 9 +++------ cmd/users_update.go | 9 +++------ cmd/utils.go | 29 +++++++++++++++++++++++++++++ 11 files changed, 60 insertions(+), 68 deletions(-) diff --git a/cmd/cmds_add.go b/cmd/cmds_add.go index 1a6f4ba2..d026fd76 100644 --- a/cmd/cmds_add.go +++ b/cmd/cmds_add.go @@ -3,6 +3,7 @@ package cmd import ( "strings" + "github.com/filebrowser/filebrowser/v2/storage" "github.com/spf13/cobra" ) @@ -15,18 +16,13 @@ var cmdsAddCmd = &cobra.Command{ Short: "Add a command to run on a specific event", Long: `Add a command to run on a specific event.`, Args: cobra.MinimumNArgs(2), - Run: func(cmd *cobra.Command, args []string) { - db := getDB() - defer db.Close() - st := getStorage(db) + Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) { s, err := st.Settings.Get() checkErr(err) - command := strings.Join(args[1:], " ") - s.Commands[args[0]] = append(s.Commands[args[0]], command) err = st.Settings.Save(s) checkErr(err) printEvents(s.Commands) - }, + }, pythonConfig{}), } diff --git a/cmd/cmds_ls.go b/cmd/cmds_ls.go index e8a03ae1..090cebf2 100644 --- a/cmd/cmds_ls.go +++ b/cmd/cmds_ls.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/filebrowser/filebrowser/v2/storage" "github.com/spf13/cobra" ) @@ -14,10 +15,7 @@ var cmdsLsCmd = &cobra.Command{ Short: "List all commands for each event", Long: `List all commands for each event.`, Args: cobra.NoArgs, - Run: func(cmd *cobra.Command, args []string) { - db := getDB() - defer db.Close() - st := getStorage(db) + Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) { s, err := st.Settings.Get() checkErr(err) evt := mustGetString(cmd, "event") @@ -30,5 +28,5 @@ var cmdsLsCmd = &cobra.Command{ show["after_"+evt] = s.Commands["after_"+evt] printEvents(show) } - }, + }, pythonConfig{}), } diff --git a/cmd/cmds_rm.go b/cmd/cmds_rm.go index 002bdb6a..2a7bf9f1 100644 --- a/cmd/cmds_rm.go +++ b/cmd/cmds_rm.go @@ -3,6 +3,7 @@ package cmd import ( "strconv" + "github.com/filebrowser/filebrowser/v2/storage" "github.com/spf13/cobra" ) @@ -27,10 +28,7 @@ var cmdsRmCmd = &cobra.Command{ return nil }, - Run: func(cmd *cobra.Command, args []string) { - db := getDB() - defer db.Close() - st := getStorage(db) + Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) { s, err := st.Settings.Get() checkErr(err) evt := args[0] @@ -47,5 +45,5 @@ var cmdsRmCmd = &cobra.Command{ err = st.Settings.Save(s) checkErr(err) printEvents(s.Commands) - }, + }, pythonConfig{}), } diff --git a/cmd/config_cat.go b/cmd/config_cat.go index f58d4444..bb33972c 100644 --- a/cmd/config_cat.go +++ b/cmd/config_cat.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/filebrowser/filebrowser/v2/storage" "github.com/spf13/cobra" ) @@ -13,14 +14,11 @@ var configCatCmd = &cobra.Command{ Short: "Prints the configuration", Long: `Prints the configuration.`, Args: cobra.NoArgs, - Run: func(cmd *cobra.Command, args []string) { - db := getDB() - defer db.Close() - st := getStorage(db) + Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) { s, err := st.Settings.Get() checkErr(err) auther, err := st.Auth.Get(s.AuthMethod) checkErr(err) printSettings(s, auther) - }, + }, pythonConfig{}), } diff --git a/cmd/config_init.go b/cmd/config_init.go index e22713e0..bacd1fdb 100644 --- a/cmd/config_init.go +++ b/cmd/config_init.go @@ -1,15 +1,12 @@ package cmd import ( - "errors" "fmt" - "os" "strings" - "github.com/asdine/storm" "github.com/filebrowser/filebrowser/v2/settings" + "github.com/filebrowser/filebrowser/v2/storage" "github.com/spf13/cobra" - v "github.com/spf13/viper" ) func init() { @@ -26,20 +23,11 @@ this options can be changed in the future with the command to the defaults when creating new users and you don't override the options.`, Args: cobra.NoArgs, - Run: func(cmd *cobra.Command, args []string) { - databasePath := v.GetString("database") - if _, err := os.Stat(databasePath); err == nil { - panic(errors.New(databasePath + " already exists")) - } - + Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) { defaults := settings.UserDefaults{} getUserDefaults(cmd, &defaults, true) authMethod, auther := getAuthentication(cmd) - db, err := storm.Open(databasePath) - checkErr(err) - defer db.Close() - st := getStorage(db) s := &settings.Settings{ Key: generateRandomBytes(64), // 256 bit Signup: mustGetBool(cmd, "signup"), @@ -53,7 +41,7 @@ override the options.`, }, } - err = st.Settings.Save(s) + err := st.Settings.Save(s) checkErr(err) err = st.Auth.Save(auther) checkErr(err) @@ -64,5 +52,5 @@ Now add your first user via 'filebrowser users new' and then you just need to call the main command to boot up the server. `) printSettings(s, auther) - }, + }, pythonConfig{noDB: true}), } diff --git a/cmd/config_set.go b/cmd/config_set.go index ea0f0159..868ac514 100644 --- a/cmd/config_set.go +++ b/cmd/config_set.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/filebrowser/filebrowser/v2/auth" + "github.com/filebrowser/filebrowser/v2/storage" "github.com/spf13/cobra" "github.com/spf13/pflag" ) @@ -19,11 +20,7 @@ var configSetCmd = &cobra.Command{ Long: `Updates the configuration. Set the flags for the options you want to change.`, Args: cobra.NoArgs, - Run: func(cmd *cobra.Command, args []string) { - db := getDB() - defer db.Close() - - st := getStorage(db) + Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) { s, err := st.Settings.Get() checkErr(err) @@ -60,5 +57,5 @@ you want to change.`, err = st.Settings.Save(s) checkErr(err) printSettings(s, auther) - }, + }, pythonConfig{}), } diff --git a/cmd/users_add.go b/cmd/users_add.go index 06906d66..c86c436c 100644 --- a/cmd/users_add.go +++ b/cmd/users_add.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/filebrowser/filebrowser/v2/storage" "github.com/filebrowser/filebrowser/v2/users" "github.com/spf13/cobra" ) @@ -15,11 +16,7 @@ var usersAddCmd = &cobra.Command{ Short: "Create a new user", Long: `Create a new user and add it to the database.`, Args: cobra.ExactArgs(2), - Run: func(cmd *cobra.Command, args []string) { - db := getDB() - defer db.Close() - st := getStorage(db) - + Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) { s, err := st.Settings.Get() checkErr(err) getUserDefaults(cmd, &s.Defaults, false) @@ -37,5 +34,5 @@ var usersAddCmd = &cobra.Command{ err = st.Users.Save(user) checkErr(err) printUsers([]*users.User{user}) - }, + }, pythonConfig{}), } diff --git a/cmd/users_find.go b/cmd/users_find.go index 070959c6..4ca45275 100644 --- a/cmd/users_find.go +++ b/cmd/users_find.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/filebrowser/filebrowser/v2/storage" "github.com/filebrowser/filebrowser/v2/users" "github.com/spf13/cobra" ) @@ -25,11 +26,7 @@ var usersLsCmd = &cobra.Command{ Run: findUsers, } -var findUsers = func(cmd *cobra.Command, args []string) { - db := getDB() - defer db.Close() - st := getStorage(db) - +var findUsers = python(func(cmd *cobra.Command, args []string, st *storage.Storage) { var ( list []*users.User user *users.User @@ -51,4 +48,4 @@ var findUsers = func(cmd *cobra.Command, args []string) { checkErr(err) printUsers(list) -} +}, pythonConfig{}) diff --git a/cmd/users_rm.go b/cmd/users_rm.go index e942c62b..9a3a5902 100644 --- a/cmd/users_rm.go +++ b/cmd/users_rm.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" + "github.com/filebrowser/filebrowser/v2/storage" "github.com/spf13/cobra" ) @@ -15,11 +16,7 @@ var usersRmCmd = &cobra.Command{ Short: "Delete a user by username or id", Long: `Delete a user by username or id`, Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - db := getDB() - defer db.Close() - st := getStorage(db) - + Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) { username, id := parseUsernameOrID(args[0]) var err error @@ -31,5 +28,5 @@ var usersRmCmd = &cobra.Command{ checkErr(err) fmt.Println("user deleted successfully") - }, + }, pythonConfig{}), } diff --git a/cmd/users_update.go b/cmd/users_update.go index fb6f65a5..e2f865af 100644 --- a/cmd/users_update.go +++ b/cmd/users_update.go @@ -2,6 +2,7 @@ package cmd import ( "github.com/filebrowser/filebrowser/v2/settings" + "github.com/filebrowser/filebrowser/v2/storage" "github.com/filebrowser/filebrowser/v2/users" "github.com/spf13/cobra" ) @@ -20,11 +21,7 @@ var usersUpdateCmd = &cobra.Command{ Long: `Updates an existing user. Set the flags for the options you want to change.`, Args: cobra.ExactArgs(1), - Run: func(cmd *cobra.Command, args []string) { - db := getDB() - defer db.Close() - st := getStorage(db) - + Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) { set, err := st.Settings.Get() checkErr(err) @@ -71,5 +68,5 @@ options you want to change.`, err = st.Users.Update(user) checkErr(err) printUsers([]*users.User{user}) - }, + }, pythonConfig{}), } diff --git a/cmd/utils.go b/cmd/utils.go index b5f84915..3c988808 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -3,6 +3,7 @@ package cmd import ( "crypto/rand" "errors" + "log" "os" "github.com/asdine/storm" @@ -83,3 +84,31 @@ func generateRandomBytes(n int) []byte { // Note that err == nil only if we read len(b) bytes. return b } + +type cobraFunc func(cmd *cobra.Command, args []string) +type pythonFunc func(cmd *cobra.Command, args []string, st *storage.Storage) + +type pythonConfig struct { + noDB bool +} + +func python(fn pythonFunc, cfg pythonConfig) cobraFunc { + return func(cmd *cobra.Command, args []string) { + path := v.GetString("database") + _, err := os.Stat(path) + + if err != nil && !os.IsNotExist(err) { + panic(err) + } else if err != nil && !cfg.noDB { + log.Fatal(path + " does not exist. Please run 'filebrowser config init' first.") + } else if err == nil && cfg.noDB { + log.Fatal(path + " already exists") + } + + db, err := storm.Open(path) + checkErr(err) + defer db.Close() + sto := bolt.NewStorage(db) + fn(cmd, args, sto) + } +}