filebrowser/cmd/users_find.go
Henrique Dias f1a89f5ec4 feat: add global scope (#604)
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>

Former-commit-id: a44ebfc7a5687b5f51f0ff791335f66ab9f2e8e0 [formerly 9a044fadd8f2ebbb7dbb773273799c26a797f513] [formerly 7f374d016eaf756cfce215dd16a2274bbabe1915 [formerly f55f205ced]]
Former-commit-id: 31015ddc5f4fc28c895743f6fe9dcf5488bb4b01 [formerly e439027304a1e49667fafde011e07d043ef0d2ee]
Former-commit-id: 0394c60358673b56991364260b1cbe41fa457593
2019-01-06 13:01:42 +00:00

60 lines
1.2 KiB
Go

package cmd
import (
"github.com/filebrowser/filebrowser/v2/users"
"github.com/spf13/cobra"
)
func init() {
usersCmd.AddCommand(usersFindCmd)
usersCmd.AddCommand(usersLsCmd)
usersFindCmd.Flags().StringP("username", "u", "", "username to find")
usersFindCmd.Flags().UintP("id", "i", 0, "id to find")
}
var usersFindCmd = &cobra.Command{
Use: "find",
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.`,
Args: cobra.NoArgs,
Run: findUsers,
}
var usersLsCmd = &cobra.Command{
Use: "ls",
Short: "List all users.",
Args: cobra.NoArgs,
Run: findUsers,
}
var findUsers = func(cmd *cobra.Command, args []string) {
db := getDB()
defer db.Close()
st := getStorage(db)
settings, err := st.Settings.Get()
checkErr(err)
username, _ := cmd.Flags().GetString("username")
id, _ := cmd.Flags().GetUint("id")
var list []*users.User
var user *users.User
if username != "" {
user, err = st.Users.Get(settings.Scope, username)
} else if id != 0 {
user, err = st.Users.Get(settings.Scope, id)
} else {
list, err = st.Users.Gets(settings.Scope)
}
checkErr(err)
if user != nil {
list = []*users.User{user}
}
printUsers(list)
}