2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
2020-05-31 23:12:36 +00:00
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
usersCmd.AddCommand(usersFindCmd)
|
|
|
|
usersCmd.AddCommand(usersLsCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var usersFindCmd = &cobra.Command{
|
2019-01-06 13:21:31 +00:00
|
|
|
Use: "find <id|username>",
|
2019-01-05 22:44:33 +00: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 13:21:31 +00:00
|
|
|
Args: cobra.ExactArgs(1),
|
2019-01-05 22:44:33 +00:00
|
|
|
Run: findUsers,
|
|
|
|
}
|
|
|
|
|
|
|
|
var usersLsCmd = &cobra.Command{
|
|
|
|
Use: "ls",
|
|
|
|
Short: "List all users.",
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
Run: findUsers,
|
|
|
|
}
|
|
|
|
|
2019-01-07 20:24:23 +00:00
|
|
|
var findUsers = python(func(cmd *cobra.Command, args []string, d pythonData) {
|
2019-01-06 13:21:31 +00:00
|
|
|
var (
|
|
|
|
list []*users.User
|
|
|
|
user *users.User
|
|
|
|
err error
|
|
|
|
)
|
2019-01-05 22:44:33 +00:00
|
|
|
|
2019-01-06 13:21:31 +00:00
|
|
|
if len(args) == 1 {
|
|
|
|
username, id := parseUsernameOrID(args[0])
|
|
|
|
if username != "" {
|
2019-01-07 20:24:23 +00:00
|
|
|
user, err = d.store.Users.Get("", username)
|
2019-01-06 13:21:31 +00:00
|
|
|
} else {
|
2019-01-07 20:24:23 +00:00
|
|
|
user, err = d.store.Users.Get("", id)
|
2019-01-06 13:21:31 +00:00
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
|
2019-01-06 13:21:31 +00:00
|
|
|
list = []*users.User{user}
|
2019-01-05 22:44:33 +00:00
|
|
|
} else {
|
2019-01-07 20:24:23 +00:00
|
|
|
list, err = d.store.Users.Gets("")
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
checkErr(err)
|
|
|
|
printUsers(list)
|
2019-01-07 20:24:23 +00:00
|
|
|
}, pythonConfig{})
|