wrap calls with python2

This commit is contained in:
Henrique Dias 2019-01-07 19:59:42 +00:00
parent f396602084
commit 6bcd0000e8
11 changed files with 60 additions and 68 deletions

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"strings" "strings"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -15,18 +16,13 @@ var cmdsAddCmd = &cobra.Command{
Short: "Add a command to run on a specific event", Short: "Add a command to run on a specific event",
Long: `Add a command to run on a specific event.`, Long: `Add a command to run on a specific event.`,
Args: cobra.MinimumNArgs(2), Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) { Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
db := getDB()
defer db.Close()
st := getStorage(db)
s, err := st.Settings.Get() s, err := st.Settings.Get()
checkErr(err) checkErr(err)
command := strings.Join(args[1:], " ") command := strings.Join(args[1:], " ")
s.Commands[args[0]] = append(s.Commands[args[0]], command) s.Commands[args[0]] = append(s.Commands[args[0]], command)
err = st.Settings.Save(s) err = st.Settings.Save(s)
checkErr(err) checkErr(err)
printEvents(s.Commands) printEvents(s.Commands)
}, }, pythonConfig{}),
} }

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -14,10 +15,7 @@ var cmdsLsCmd = &cobra.Command{
Short: "List all commands for each event", Short: "List all commands for each event",
Long: `List all commands for each event.`, Long: `List all commands for each event.`,
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
db := getDB()
defer db.Close()
st := getStorage(db)
s, err := st.Settings.Get() s, err := st.Settings.Get()
checkErr(err) checkErr(err)
evt := mustGetString(cmd, "event") evt := mustGetString(cmd, "event")
@ -30,5 +28,5 @@ var cmdsLsCmd = &cobra.Command{
show["after_"+evt] = s.Commands["after_"+evt] show["after_"+evt] = s.Commands["after_"+evt]
printEvents(show) printEvents(show)
} }
}, }, pythonConfig{}),
} }

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"strconv" "strconv"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -27,10 +28,7 @@ var cmdsRmCmd = &cobra.Command{
return nil return nil
}, },
Run: func(cmd *cobra.Command, args []string) { Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
db := getDB()
defer db.Close()
st := getStorage(db)
s, err := st.Settings.Get() s, err := st.Settings.Get()
checkErr(err) checkErr(err)
evt := args[0] evt := args[0]
@ -47,5 +45,5 @@ var cmdsRmCmd = &cobra.Command{
err = st.Settings.Save(s) err = st.Settings.Save(s)
checkErr(err) checkErr(err)
printEvents(s.Commands) printEvents(s.Commands)
}, }, pythonConfig{}),
} }

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -13,14 +14,11 @@ var configCatCmd = &cobra.Command{
Short: "Prints the configuration", Short: "Prints the configuration",
Long: `Prints the configuration.`, Long: `Prints the configuration.`,
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
db := getDB()
defer db.Close()
st := getStorage(db)
s, err := st.Settings.Get() s, err := st.Settings.Get()
checkErr(err) checkErr(err)
auther, err := st.Auth.Get(s.AuthMethod) auther, err := st.Auth.Get(s.AuthMethod)
checkErr(err) checkErr(err)
printSettings(s, auther) printSettings(s, auther)
}, }, pythonConfig{}),
} }

View File

@ -1,15 +1,12 @@
package cmd package cmd
import ( import (
"errors"
"fmt" "fmt"
"os"
"strings" "strings"
"github.com/asdine/storm"
"github.com/filebrowser/filebrowser/v2/settings" "github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/spf13/cobra" "github.com/spf13/cobra"
v "github.com/spf13/viper"
) )
func init() { 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 to the defaults when creating new users and you don't
override the options.`, override the options.`,
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
databasePath := v.GetString("database")
if _, err := os.Stat(databasePath); err == nil {
panic(errors.New(databasePath + " already exists"))
}
defaults := settings.UserDefaults{} defaults := settings.UserDefaults{}
getUserDefaults(cmd, &defaults, true) getUserDefaults(cmd, &defaults, true)
authMethod, auther := getAuthentication(cmd) authMethod, auther := getAuthentication(cmd)
db, err := storm.Open(databasePath)
checkErr(err)
defer db.Close()
st := getStorage(db)
s := &settings.Settings{ s := &settings.Settings{
Key: generateRandomBytes(64), // 256 bit Key: generateRandomBytes(64), // 256 bit
Signup: mustGetBool(cmd, "signup"), Signup: mustGetBool(cmd, "signup"),
@ -53,7 +41,7 @@ override the options.`,
}, },
} }
err = st.Settings.Save(s) err := st.Settings.Save(s)
checkErr(err) checkErr(err)
err = st.Auth.Save(auther) err = st.Auth.Save(auther)
checkErr(err) 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. need to call the main command to boot up the server.
`) `)
printSettings(s, auther) printSettings(s, auther)
}, }, pythonConfig{noDB: true}),
} }

View File

@ -4,6 +4,7 @@ import (
"strings" "strings"
"github.com/filebrowser/filebrowser/v2/auth" "github.com/filebrowser/filebrowser/v2/auth"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/pflag" "github.com/spf13/pflag"
) )
@ -19,11 +20,7 @@ var configSetCmd = &cobra.Command{
Long: `Updates the configuration. Set the flags for the options Long: `Updates the configuration. Set the flags for the options
you want to change.`, you want to change.`,
Args: cobra.NoArgs, Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) { Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
db := getDB()
defer db.Close()
st := getStorage(db)
s, err := st.Settings.Get() s, err := st.Settings.Get()
checkErr(err) checkErr(err)
@ -60,5 +57,5 @@ you want to change.`,
err = st.Settings.Save(s) err = st.Settings.Save(s)
checkErr(err) checkErr(err)
printSettings(s, auther) printSettings(s, auther)
}, }, pythonConfig{}),
} }

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/users" "github.com/filebrowser/filebrowser/v2/users"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -15,11 +16,7 @@ var usersAddCmd = &cobra.Command{
Short: "Create a new user", Short: "Create a new user",
Long: `Create a new user and add it to the database.`, Long: `Create a new user and add it to the database.`,
Args: cobra.ExactArgs(2), Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) { Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
db := getDB()
defer db.Close()
st := getStorage(db)
s, err := st.Settings.Get() s, err := st.Settings.Get()
checkErr(err) checkErr(err)
getUserDefaults(cmd, &s.Defaults, false) getUserDefaults(cmd, &s.Defaults, false)
@ -37,5 +34,5 @@ var usersAddCmd = &cobra.Command{
err = st.Users.Save(user) err = st.Users.Save(user)
checkErr(err) checkErr(err)
printUsers([]*users.User{user}) printUsers([]*users.User{user})
}, }, pythonConfig{}),
} }

View File

@ -1,6 +1,7 @@
package cmd package cmd
import ( import (
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/users" "github.com/filebrowser/filebrowser/v2/users"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -25,11 +26,7 @@ var usersLsCmd = &cobra.Command{
Run: findUsers, Run: findUsers,
} }
var findUsers = func(cmd *cobra.Command, args []string) { var findUsers = python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
db := getDB()
defer db.Close()
st := getStorage(db)
var ( var (
list []*users.User list []*users.User
user *users.User user *users.User
@ -51,4 +48,4 @@ var findUsers = func(cmd *cobra.Command, args []string) {
checkErr(err) checkErr(err)
printUsers(list) printUsers(list)
} }, pythonConfig{})

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -15,11 +16,7 @@ var usersRmCmd = &cobra.Command{
Short: "Delete a user by username or id", Short: "Delete a user by username or id",
Long: `Delete a user by username or id`, Long: `Delete a user by username or id`,
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
db := getDB()
defer db.Close()
st := getStorage(db)
username, id := parseUsernameOrID(args[0]) username, id := parseUsernameOrID(args[0])
var err error var err error
@ -31,5 +28,5 @@ var usersRmCmd = &cobra.Command{
checkErr(err) checkErr(err)
fmt.Println("user deleted successfully") fmt.Println("user deleted successfully")
}, }, pythonConfig{}),
} }

View File

@ -2,6 +2,7 @@ package cmd
import ( import (
"github.com/filebrowser/filebrowser/v2/settings" "github.com/filebrowser/filebrowser/v2/settings"
"github.com/filebrowser/filebrowser/v2/storage"
"github.com/filebrowser/filebrowser/v2/users" "github.com/filebrowser/filebrowser/v2/users"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -20,11 +21,7 @@ var usersUpdateCmd = &cobra.Command{
Long: `Updates an existing user. Set the flags for the Long: `Updates an existing user. Set the flags for the
options you want to change.`, options you want to change.`,
Args: cobra.ExactArgs(1), Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: python(func(cmd *cobra.Command, args []string, st *storage.Storage) {
db := getDB()
defer db.Close()
st := getStorage(db)
set, err := st.Settings.Get() set, err := st.Settings.Get()
checkErr(err) checkErr(err)
@ -71,5 +68,5 @@ options you want to change.`,
err = st.Users.Update(user) err = st.Users.Update(user)
checkErr(err) checkErr(err)
printUsers([]*users.User{user}) printUsers([]*users.User{user})
}, }, pythonConfig{}),
} }

View File

@ -3,6 +3,7 @@ package cmd
import ( import (
"crypto/rand" "crypto/rand"
"errors" "errors"
"log"
"os" "os"
"github.com/asdine/storm" "github.com/asdine/storm"
@ -83,3 +84,31 @@ func generateRandomBytes(n int) []byte {
// Note that err == nil only if we read len(b) bytes. // Note that err == nil only if we read len(b) bytes.
return b 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)
}
}