86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
|
|
package cmd
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
|
||
|
|
"code.gitea.io/sdk/gitea"
|
||
|
|
"github.com/spf13/cobra"
|
||
|
|
)
|
||
|
|
|
||
|
|
var adminCmd = &cobra.Command{
|
||
|
|
Use: "admin",
|
||
|
|
Aliases: []string{"a"},
|
||
|
|
Short: "Operations requiring admin access",
|
||
|
|
Long: "Administrative operations on the current host. These require an admin-scoped token.",
|
||
|
|
}
|
||
|
|
|
||
|
|
var adminUserCmd = &cobra.Command{
|
||
|
|
Use: "user",
|
||
|
|
Aliases: []string{"users", "u"},
|
||
|
|
Short: "Manage users on the host",
|
||
|
|
Long: "Admin-scoped user management.",
|
||
|
|
}
|
||
|
|
|
||
|
|
var adminUserListCmd = &cobra.Command{
|
||
|
|
Use: "list",
|
||
|
|
Aliases: []string{"ls"},
|
||
|
|
Short: "List all users on the host",
|
||
|
|
Example: ` # List users
|
||
|
|
fgj admin user list
|
||
|
|
|
||
|
|
# Limit and output as JSON
|
||
|
|
fgj admin user list --limit 100 --json`,
|
||
|
|
RunE: runAdminUserList,
|
||
|
|
}
|
||
|
|
|
||
|
|
func init() {
|
||
|
|
rootCmd.AddCommand(adminCmd)
|
||
|
|
adminCmd.AddCommand(adminUserCmd)
|
||
|
|
adminUserCmd.AddCommand(adminUserListCmd)
|
||
|
|
|
||
|
|
adminUserListCmd.Flags().IntP("limit", "L", 50, "Maximum number of users to list")
|
||
|
|
addJSONFlags(adminUserListCmd, "Output as JSON")
|
||
|
|
}
|
||
|
|
|
||
|
|
func runAdminUserList(cmd *cobra.Command, args []string) error {
|
||
|
|
client, err := loadClient()
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
limit, _ := cmd.Flags().GetInt("limit")
|
||
|
|
if limit <= 0 {
|
||
|
|
limit = 50
|
||
|
|
}
|
||
|
|
|
||
|
|
users, _, err := client.AdminListUsers(gitea.AdminListUsersOptions{
|
||
|
|
ListOptions: gitea.ListOptions{PageSize: limit},
|
||
|
|
})
|
||
|
|
if err != nil {
|
||
|
|
return fmt.Errorf("failed to list users (admin token required): %w", err)
|
||
|
|
}
|
||
|
|
|
||
|
|
if wantJSON(cmd) {
|
||
|
|
return outputJSON(cmd, users)
|
||
|
|
}
|
||
|
|
|
||
|
|
if len(users) == 0 {
|
||
|
|
fmt.Fprintln(ios.Out, "No users found.")
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
tp := ios.NewTablePrinter()
|
||
|
|
tp.AddHeader("LOGIN", "FULL NAME", "EMAIL", "ADMIN", "ACTIVE")
|
||
|
|
for _, u := range users {
|
||
|
|
admin, active := "", "yes"
|
||
|
|
if u.IsAdmin {
|
||
|
|
admin = "yes"
|
||
|
|
}
|
||
|
|
if !u.IsActive {
|
||
|
|
active = "no"
|
||
|
|
}
|
||
|
|
tp.AddRow(u.UserName, u.FullName, u.Email, admin, active)
|
||
|
|
}
|
||
|
|
return tp.Render()
|
||
|
|
}
|