2019-01-06 06:44:33 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
usersCmd.AddCommand(usersFindCmd)
|
|
|
|
usersCmd.AddCommand(usersLsCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var usersFindCmd = &cobra.Command{
|
2019-01-06 21:21:31 +08:00
|
|
|
Use: "find <id|username>",
|
2019-01-06 06:44:33 +08:00
|
|
|
Short: "Find a user by username or id",
|
|
|
|
Long: `Find a user by username or id. If no flag is set, all users will be printed.`,
|
2019-01-06 21:21:31 +08:00
|
|
|
Args: cobra.ExactArgs(1),
|
2019-01-06 06:44:33 +08:00
|
|
|
Run: findUsers,
|
|
|
|
}
|
|
|
|
|
|
|
|
var usersLsCmd = &cobra.Command{
|
|
|
|
Use: "ls",
|
|
|
|
Short: "List all users.",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
Run: findUsers,
|
|
|
|
}
|
|
|
|
|
2019-01-08 04:24:23 +08:00
|
|
|
var findUsers = python(func(cmd *cobra.Command, args []string, d pythonData) {
|
2019-01-06 21:21:31 +08:00
|
|
|
var (
|
|
|
|
list []*users.User
|
|
|
|
user *users.User
|
|
|
|
err error
|
|
|
|
)
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-06 21:21:31 +08:00
|
|
|
if len(args) == 1 {
|
|
|
|
username, id := parseUsernameOrID(args[0])
|
|
|
|
if username != "" {
|
2019-01-08 04:24:23 +08:00
|
|
|
user, err = d.store.Users.Get("", username)
|
2019-01-06 21:21:31 +08:00
|
|
|
} else {
|
2019-01-08 04:24:23 +08:00
|
|
|
user, err = d.store.Users.Get("", id)
|
2019-01-06 21:21:31 +08:00
|
|
|
}
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-06 21:21:31 +08:00
|
|
|
list = []*users.User{user}
|
2019-01-06 06:44:33 +08:00
|
|
|
} else {
|
2019-01-08 04:24:23 +08:00
|
|
|
list, err = d.store.Users.Gets("")
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
checkErr(err)
|
|
|
|
printUsers(list)
|
2019-01-08 04:24:23 +08:00
|
|
|
}, pythonConfig{})
|