Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions cli/command/delete/delete-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ var delete_groupCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return deleteID(cli.Context(), args, "Group")
},
}
}}

func init() {
deleteCmd.AddCommand(delete_groupCmd)
}
func init() { deleteCmd.AddCommand(delete_groupCmd) }
2 changes: 1 addition & 1 deletion cli/command/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func deleteID(ctx context.Context, args []string, IDtype string) (err error) {
case "AcmePlugin":
err = c.DeleteAcmePlugin(ctx, id)
case "Group":
err = proxmox.GroupName(id).Delete(ctx, c)
_, err = c.New().Group.Delete(ctx, proxmox.GroupName(id))
case "MetricServer":
err = c.DeleteMetricServer(ctx, id)
case "Pool":
Expand Down
7 changes: 2 additions & 5 deletions cli/command/get/get-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ var get_groupCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return getConfig(args, "Group")
},
}
}}

func init() {
GetCmd.AddCommand(get_groupCmd)
}
func init() { GetCmd.AddCommand(get_groupCmd) }
4 changes: 3 additions & 1 deletion cli/command/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ func getConfig(args []string, IDtype string) (err error) {
return
}
case "Group":
config, err = proxmox.NewConfigGroupFromApi(cli.Context(), proxmox.GroupName(id), c)
var rawConfig proxmox.RawGroupConfig
rawConfig, err = c.New().Group.Read(cli.Context(), proxmox.GroupName(id))
if err != nil {
return
}
config = rawConfig.Get()
case "MetricServer":
config, err = proxmox.NewConfigMetricsFromApi(cli.Context(), id, c)
if err != nil {
Expand Down
26 changes: 14 additions & 12 deletions cli/command/list/list-groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ var list_groupsCmd = &cobra.Command{
Use: "groups",
Short: "Prints a list of groups in raw json format",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) (err error) {
c := cli.NewClient()
groups, err := proxmox.ListGroups(cli.Context(), c)
RunE: func(cmd *cobra.Command, args []string) error {
raw, err := cli.NewClient().New().Group.List(cli.Context())
if err != nil {
return
return err
}
output, err := json.Marshal(groups)
rawGroups := raw.FormatArray()
groups := make([]proxmox.ConfigGroup, len(rawGroups))
for i := range rawGroups {
groups[i] = rawGroups[i].Get()
}
var output []byte
output, err = json.Marshal(groups)
if err != nil {
return
return err
}
fmt.Fprintln(listCmd.OutOrStdout(), string(output))
return
},
}
return nil
}}

func init() {
listCmd.AddCommand(list_groupsCmd)
}
func init() { listCmd.AddCommand(list_groupsCmd) }
14 changes: 6 additions & 8 deletions cli/command/member/group/add-users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package group
import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/cli/helpers"
"github.com/Telmate/proxmox-api-go/proxmox"
pveSDK "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

Expand All @@ -12,18 +12,16 @@ var group_addCmd = &cobra.Command{
Short: "Add members to the specified group",
Example: "add myGroup admin@pve,root@pam",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
RunE: func(cmd *cobra.Command, args []string) error {
users, err := helpers.ParseUserIDs(args[1])
if err != nil {
return
return err
}
c := cli.NewClient()
err = proxmox.GroupName(args[0]).AddUsersToGroup(cli.Context(), users, c)
if err != nil {
return
if err = cli.NewClient().New().Group.AddMembers(cli.Context(), []pveSDK.GroupName{pveSDK.GroupName(args[0])}, *users); err != nil {
return err
}
cli.PrintItemUpdated(member_GroupCmd.OutOrStdout(), args[0], "Group membership of")
return
return nil
}}

func init() {
Expand Down
14 changes: 6 additions & 8 deletions cli/command/member/group/remove-users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package group
import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/cli/helpers"
"github.com/Telmate/proxmox-api-go/proxmox"
pveSDK "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

Expand All @@ -12,18 +12,16 @@ var group_removeCmd = &cobra.Command{
Short: "Remove members from the specified group",
Example: "remove myGroup admin@pve,root@pam",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
RunE: func(cmd *cobra.Command, args []string) error {
users, err := helpers.ParseUserIDs(args[1])
if err != nil {
return
return err
}
c := cli.NewClient()
err = proxmox.GroupName(args[0]).RemoveUsersFromGroup(cli.Context(), users, c)
if err != nil {
return
if err = cli.NewClient().New().Group.RemoveMembers(cli.Context(), []pveSDK.GroupName{pveSDK.GroupName(args[0])}, *users); err != nil {
return err
}
cli.PrintItemUpdated(member_GroupCmd.OutOrStdout(), args[0], "Group membership of")
return
return nil
}}

func init() {
Expand Down
17 changes: 8 additions & 9 deletions cli/command/member/group/set-users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package group
import (
"github.com/Telmate/proxmox-api-go/cli"
"github.com/Telmate/proxmox-api-go/cli/helpers"
"github.com/Telmate/proxmox-api-go/proxmox"
pveSDK "github.com/Telmate/proxmox-api-go/proxmox"
"github.com/spf13/cobra"
)

Expand All @@ -14,21 +14,20 @@ var group_setCmd = &cobra.Command{
When no users are provided all users will be removed from the group.`,
Example: "clear myGroup admin@pve,root@pam",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
var users *[]proxmox.UserID
RunE: func(cmd *cobra.Command, args []string) error {
var users *[]pveSDK.UserID
var err error
if len(args) == 2 {
users, err = helpers.ParseUserIDs(args[1])
if err != nil {
return
return err
}
}
c := cli.NewClient()
err = proxmox.GroupName(args[0]).SetMembers(cli.Context(), users, c)
if err != nil {
return
if err = cli.NewClient().New().Group.Set(cli.Context(), pveSDK.ConfigGroup{Name: pveSDK.GroupName(args[0]), Members: users}); err != nil {
return err
}
cli.PrintItemUpdated(member_GroupCmd.OutOrStdout(), args[0], "Group membership of")
return
return nil
}}

func init() {
Expand Down
7 changes: 3 additions & 4 deletions cli/command/set/set-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ var (
return
}
}
config := proxmox.ConfigGroup{
err = cli.NewClient().New().Group.Set(cli.Context(), proxmox.ConfigGroup{
Name: groupname,
Comment: comment,
Comment: &comment,
Members: formattedMembers,
}
err = config.Set(cli.Context(), cli.NewClient())
})
if err != nil {
return
}
Expand Down
80 changes: 79 additions & 1 deletion internal/array/array.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,83 @@
package array

func Nil[T any]() []T { return nil }
import (
"fmt"
"strings"
)

// Combine two arrays of comparable types into one, removing duplicates.
// The output order is not guaranteed.
func Combine[T comparable](a, b []T) []T {
combinedMap := make(map[T]struct{})
for i := range a {
combinedMap[a[i]] = struct{}{}
}
for i := range b {
combinedMap[b[i]] = struct{}{}
}
combined := make([]T, len(combinedMap))
var index int
for e := range combinedMap {
combined[index] = e
index++
}
return combined
}

// CSV converts an array of fmt.Stringer to a CSV string
func CSV[T fmt.Stringer](array []T) string {
switch len(array) {
case 0:
return ""
case 1:
return array[0].String()
}
builder := strings.Builder{}
for i := range array {
builder.WriteString("," + array[i].String())
}
return builder.String()[1:]
}

func Empty[T any]() []T { return []T{} }

func Nil[T any]() []T { return nil }

// RemoveItem removes all occurrences of the specified item from the array.
func RemoveItem[T comparable](array []T, remove T) []T {
var length int
for i := range array {
if array[i] != remove {
length++
}
}
var index int
newArray := make([]T, length)
for i := range array {
if array[i] != remove {
newArray[index] = array[i]
index++
}
}
return newArray
}

// RemoveItems removes all occurrences of the specified items from the array.
// Duplicate items are reduced to a single instance.
// The order of the remaining items is not guaranteed.
func RemoveItems[T comparable](array []T, remove []T) []T {
removeMap := make(map[T]struct{})
for i := range array {
removeMap[array[i]] = struct{}{}
}
for i := range remove {
delete(removeMap, remove[i])
}
result := make([]T, len(removeMap))
var index int
for e := range removeMap {
result[index] = e
index++
}
return result
}
Loading