Skip to content

Commit

Permalink
feat: Add toggle for user list command
Browse files Browse the repository at this point in the history
Added a toggle to make the returning of the groups optional, as not all internal functions need te group information
  • Loading branch information
Tinyblargon committed Feb 7, 2023
1 parent b41fb57 commit daf13c5
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 11 deletions.
21 changes: 19 additions & 2 deletions cli/command/list/list-users.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package list

import (
"encoding/json"
"fmt"

"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

var list_usersCmd = &cobra.Command{
Use: "users",
Short: "Prints a list of Users in raw json format",
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
listRaw("Users")
RunE: func(cmd *cobra.Command, args []string) (err error) {
c := cli.NewClient()
groups, _ := cmd.Flags().GetBool("groups")
users, err := proxmox.ListUsers(c, groups)
if err != nil {
return
}
output, err := json.Marshal(users)
if err != nil {
return
}
fmt.Fprintln(listCmd.OutOrStdout(), string(output))
return
},
}

func init() {
listCmd.AddCommand(list_usersCmd)
list_usersCmd.PersistentFlags().Bool("groups", false, "Result will include group membership")
}
3 changes: 0 additions & 3 deletions cli/command/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package list

import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -34,8 +33,6 @@ func listRaw(IDtype string) {
list, err = c.GetPoolList()
case "Storages":
list, err = c.GetStorageList()
case "Users":
list, err = proxmox.ListUsers(c)
}
cli.LogFatalListing(IDtype, err)
cli.PrintRawJson(listCmd.OutOrStdout(), list)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ func main() {
log.Println(string(cj))

case "getUserList":
users, err := proxmox.ListUsers(c)
users, err := proxmox.ListUsers(c, true)
if err != nil {
log.Printf("Error listing users %+v\n", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion proxmox/config_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (group GroupName) setMembers(members *[]UserID, client *Client) (err error)
if members == nil {
return
}
users, err := listUsers(client)
users, err := listUsersFull(client)
if err != nil {
return
}
Expand Down
22 changes: 18 additions & 4 deletions proxmox/config_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,21 +266,35 @@ func (password UserPassword) Validate() error {
}

// List all users that exist in proxmox
func ListUsers(client *Client) (*[]ConfigUser, error) {
userList, err := listUsers(client)
// Setting full to TRUE the output wil include group information.
// Depending on the number of existing groups it take substantially longer to parse
func ListUsers(client *Client, full bool) (*[]ConfigUser, error) {
var err error
var userList []interface{}
if full {
userList, err = listUsersFull(client)
} else {
userList, err = listUsersPartial(client)
}
if err != nil {
return nil, err
}
return ConfigUser{}.mapToArray(userList), nil
}

func listUsers(client *Client) ([]interface{}, error) {
// Returns users without group information
func listUsersPartial(client *Client) ([]interface{}, error) {
return client.GetItemListInterfaceArray("/access/users")
}

// Returns users with group information
func listUsersFull(client *Client) ([]interface{}, error) {
return client.GetItemListInterfaceArray("/access/users?full=1")
}

// Check if the user already exists in proxmox.
func CheckUserExistence(userId UserID, client *Client) (existence bool, err error) {
list, err := listUsers(client)
list, err := listUsersPartial(client)
if err != nil {
return
}
Expand Down

0 comments on commit daf13c5

Please sign in to comment.