mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 19:08:21 +00:00
add json output to group commands
This commit is contained in:
parent
ff9aa904c6
commit
6a4fc45132
3 changed files with 107 additions and 46 deletions
|
@ -2,6 +2,7 @@ package group
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/passbolt/go-passbolt-cli/util"
|
||||
|
@ -40,6 +41,10 @@ func GroupCreate(cmd *cobra.Command, args []string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jsonOutput, err := cmd.Flags().GetBool("json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ops := []helper.GroupMembershipOperation{}
|
||||
for _, user := range users {
|
||||
|
@ -74,6 +79,18 @@ func GroupCreate(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Creating Group: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
jsonId, err := json.MarshalIndent(
|
||||
map[string]string{"id": id},
|
||||
"",
|
||||
" ",
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonId))
|
||||
} else {
|
||||
fmt.Printf("GroupID: %v\n", id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
29
group/get.go
29
group/get.go
|
@ -2,6 +2,7 @@ package group
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
|
@ -37,6 +38,10 @@ func GroupGet(cmd *cobra.Command, args []string) error {
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jsonOutput, err := cmd.Flags().GetBool("json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx := util.GetContext()
|
||||
|
||||
|
@ -55,6 +60,29 @@ func GroupGet(cmd *cobra.Command, args []string) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("Getting Group: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
group, err := client.GetGroup(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
jsonGroup, err := json.MarshalIndent(group, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var tempMap map[string]interface{}
|
||||
if err := json.Unmarshal(jsonGroup, &tempMap); err != nil {
|
||||
return err
|
||||
}
|
||||
tempMap["group_users"] = memberships
|
||||
jsonResult, err := json.MarshalIndent(tempMap, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println(string(jsonResult))
|
||||
} else {
|
||||
fmt.Printf("Name: %v\n", name)
|
||||
// Print Memberships
|
||||
if len(columns) != 0 {
|
||||
|
@ -84,5 +112,6 @@ func GroupGet(cmd *cobra.Command, args []string) error {
|
|||
|
||||
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package group
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
|
@ -45,7 +46,10 @@ func GroupList(cmd *cobra.Command, args []string) error {
|
|||
if len(columns) == 0 {
|
||||
return fmt.Errorf("You need to specify atleast one column to return")
|
||||
}
|
||||
|
||||
jsonOutput, err := cmd.Flags().GetBool("json")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx := util.GetContext()
|
||||
|
||||
client, err := util.GetClient(ctx)
|
||||
|
@ -58,11 +62,21 @@ func GroupList(cmd *cobra.Command, args []string) error {
|
|||
resources, err := client.GetGroups(ctx, &api.GetGroupsOptions{
|
||||
FilterHasUsers: users,
|
||||
FilterHasManagers: managers,
|
||||
ContainGroupsUsers: true,
|
||||
ContainGroupsUsersUser: true,
|
||||
ContainGroupsUsersUserProfile: true,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Listing Group: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
jsonGroup, err := json.MarshalIndent(resources, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonGroup))
|
||||
} else {
|
||||
data := pterm.TableData{columns}
|
||||
|
||||
for _, resource := range resources {
|
||||
|
@ -82,5 +96,6 @@ func GroupList(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
|
||||
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue