wrap calls with python2
This commit is contained in:
parent
f396602084
commit
6bcd0000e8
@ -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{}),
|
||||
}
|
||||
|
||||
@ -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{}),
|
||||
}
|
||||
|
||||
@ -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{}),
|
||||
}
|
||||
|
||||
@ -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{}),
|
||||
}
|
||||
|
||||
@ -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}),
|
||||
}
|
||||
|
||||
@ -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{}),
|
||||
}
|
||||
|
||||
@ -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{}),
|
||||
}
|
||||
|
||||
@ -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{})
|
||||
|
||||
@ -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{}),
|
||||
}
|
||||
|
||||
@ -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{}),
|
||||
}
|
||||
|
||||
29
cmd/utils.go
29
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)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user