mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 02:58:20 +00:00
Merge pull request #20 from passbolt/feature-json-output
Json Output Support
This commit is contained in:
commit
9a2944c6fb
20 changed files with 471 additions and 157 deletions
|
@ -92,6 +92,11 @@ You can setup MFA also using the configuration sub command, only TOTP is support
|
|||
# Server Verification
|
||||
To enable Server Verification you need to run `passbolt verify` once, after that the server will always be verified if the same config is used
|
||||
|
||||
# Scripting
|
||||
For Scripting we have a -j or --json flag to convert the Output for the create, get and list commands to JSON for easier Parsing in Scripts.
|
||||
|
||||
Note: The JSON Output does not cover Error Messages, you can detect Errors by checking if the Exitcode is not 0
|
||||
|
||||
# Documentation
|
||||
Usage for all Subcommands is [here](https://github.com/passbolt/go-passbolt-cli/wiki/go-passbolt-cli).
|
||||
And is also available via `man passbolt`
|
||||
|
|
|
@ -18,6 +18,7 @@ var createCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
rootCmd.AddCommand(createCmd)
|
||||
createCmd.PersistentFlags().BoolP("json", "j", false, "Output JSON")
|
||||
createCmd.AddCommand(resource.ResourceCreateCmd)
|
||||
createCmd.AddCommand(folder.FolderCreateCmd)
|
||||
createCmd.AddCommand(group.GroupCreateCmd)
|
||||
|
|
|
@ -18,6 +18,7 @@ var getCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
rootCmd.AddCommand(getCmd)
|
||||
getCmd.PersistentFlags().BoolP("json", "j", false, "Output JSON")
|
||||
getCmd.AddCommand(resource.ResourceGetCmd)
|
||||
getCmd.AddCommand(folder.FolderGetCmd)
|
||||
getCmd.AddCommand(group.GroupGetCmd)
|
||||
|
|
|
@ -18,6 +18,7 @@ var listCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
listCmd.PersistentFlags().BoolP("json", "j", false, "Output JSON")
|
||||
listCmd.AddCommand(resource.ResourceListCmd)
|
||||
listCmd.AddCommand(folder.FolderListCmd)
|
||||
listCmd.AddCommand(group.GroupListCmd)
|
||||
|
|
|
@ -2,6 +2,7 @@ package folder
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/passbolt/go-passbolt-cli/util"
|
||||
|
@ -33,6 +34,10 @@ func FolderCreate(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()
|
||||
|
||||
|
@ -53,6 +58,18 @@ func FolderCreate(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Creating Folder: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
jsonId, err := json.MarshalIndent(
|
||||
map[string]string{"id": id},
|
||||
"",
|
||||
" ",
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Marshalling Json: %w", err)
|
||||
}
|
||||
fmt.Println(string(jsonId))
|
||||
} else {
|
||||
fmt.Printf("FolderID: %v\n", id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,11 +2,11 @@ package folder
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/alessio/shellescape"
|
||||
"github.com/passbolt/go-passbolt-cli/util"
|
||||
"github.com/passbolt/go-passbolt/helper"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
@ -29,6 +29,10 @@ func FolderGet(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()
|
||||
|
||||
|
@ -39,15 +43,22 @@ func FolderGet(cmd *cobra.Command, args []string) error {
|
|||
defer client.Logout(context.TODO())
|
||||
cmd.SilenceUsage = true
|
||||
|
||||
folderParentID, name, err := helper.GetFolder(
|
||||
ctx,
|
||||
client,
|
||||
id,
|
||||
)
|
||||
folder, err := client.GetFolder(ctx, id, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Getting Folder: %w", err)
|
||||
}
|
||||
fmt.Printf("FolderParentID: %v\n", folderParentID)
|
||||
fmt.Printf("Name: %v\n", shellescape.StripUnsafe(name))
|
||||
if jsonOutput {
|
||||
jsonGroup, err := json.MarshalIndent(FolderJsonOutput{
|
||||
FolderParentID: &folder.FolderParentID,
|
||||
Name: &folder.Name,
|
||||
}, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonGroup))
|
||||
} else {
|
||||
fmt.Printf("FolderParentID: %v\n", folder.FolderParentID)
|
||||
fmt.Printf("Name: %v\n", shellescape.StripUnsafe(folder.Name))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
11
folder/json.go
Normal file
11
folder/json.go
Normal file
|
@ -0,0 +1,11 @@
|
|||
package folder
|
||||
|
||||
import "time"
|
||||
|
||||
type FolderJsonOutput struct {
|
||||
ID *string `json:"id,omitempty"`
|
||||
FolderParentID *string `json:"folder_parent_id,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
CreatedTimestamp *time.Time `json:"created_timestamp,omitempty"`
|
||||
ModifiedTimestamp *time.Time `json:"modified_timestamp,omitempty"`
|
||||
}
|
|
@ -2,6 +2,7 @@ package folder
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -46,6 +47,10 @@ func FolderList(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()
|
||||
cmd.SilenceUsage = true
|
||||
|
@ -64,6 +69,23 @@ func FolderList(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Listing Folder: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
outputFolders := []FolderJsonOutput{}
|
||||
for i := range folders {
|
||||
outputFolders = append(outputFolders, FolderJsonOutput{
|
||||
ID: &folders[i].ID,
|
||||
FolderParentID: &folders[i].FolderParentID,
|
||||
Name: &folders[i].Name,
|
||||
CreatedTimestamp: &folders[i].Created.Time,
|
||||
ModifiedTimestamp: &folders[i].Modified.Time,
|
||||
})
|
||||
}
|
||||
jsonFolders, err := json.MarshalIndent(outputFolders, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonFolders))
|
||||
} else {
|
||||
data := pterm.TableData{columns}
|
||||
|
||||
for _, folder := range folders {
|
||||
|
@ -89,5 +111,6 @@ func FolderList(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"
|
||||
|
||||
"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 fmt.Errorf("Marshalling Json: %w", 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 {
|
||||
groupUserMemberships := []GroupUserMembershipJsonOutput{}
|
||||
for i := range memberships {
|
||||
groupUserMemberships = append(groupUserMemberships, GroupUserMembershipJsonOutput{
|
||||
ID: &memberships[i].UserID,
|
||||
Username: &memberships[i].Username,
|
||||
FirstName: &memberships[i].UserFirstName,
|
||||
LastName: &memberships[i].UserLastName,
|
||||
IsGroupManager: &memberships[i].IsGroupManager,
|
||||
})
|
||||
}
|
||||
|
||||
jsonGroup, err := json.MarshalIndent(GroupJsonOutput{
|
||||
Name: &name,
|
||||
Users: groupUserMemberships,
|
||||
}, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonGroup))
|
||||
|
||||
} 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
|
||||
}
|
||||
|
|
19
group/json.go
Normal file
19
group/json.go
Normal file
|
@ -0,0 +1,19 @@
|
|||
package group
|
||||
|
||||
import "time"
|
||||
|
||||
type GroupJsonOutput struct {
|
||||
ID *string `json:"id,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
Users []GroupUserMembershipJsonOutput `json:"users,omitempty"`
|
||||
CreatedTimestamp *time.Time `json:"created_timestamp,omitempty"`
|
||||
ModifiedTimestamp *time.Time `json:"modified_timestamp,omitempty"`
|
||||
}
|
||||
|
||||
type GroupUserMembershipJsonOutput struct {
|
||||
ID *string `json:"id,omitempty"`
|
||||
Username *string `json:"username,omitempty"`
|
||||
FirstName *string `json:"first_name,omitempty"`
|
||||
LastName *string `json:"last_name,omitempty"`
|
||||
IsGroupManager *bool `json:"is_group_manager,omitempty"`
|
||||
}
|
|
@ -2,6 +2,7 @@ package group
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -46,6 +47,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()
|
||||
|
||||
|
@ -64,6 +69,22 @@ func GroupList(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Listing Group: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
outputGroups := []GroupJsonOutput{}
|
||||
for i := range groups {
|
||||
outputGroups = append(outputGroups, GroupJsonOutput{
|
||||
ID: &groups[i].ID,
|
||||
Name: &groups[i].Name,
|
||||
CreatedTimestamp: &groups[i].Created.Time,
|
||||
ModifiedTimestamp: &groups[i].Modified.Time,
|
||||
})
|
||||
}
|
||||
jsonGroups, err := json.MarshalIndent(outputGroups, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonGroups))
|
||||
} else {
|
||||
data := pterm.TableData{columns}
|
||||
|
||||
for _, group := range groups {
|
||||
|
@ -87,5 +108,6 @@ func GroupList(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
|
||||
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package resource
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/passbolt/go-passbolt-cli/util"
|
||||
|
@ -54,6 +55,10 @@ func ResourceCreate(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()
|
||||
|
||||
|
@ -78,6 +83,18 @@ func ResourceCreate(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Creating Resource: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
jsonId, err := json.MarshalIndent(
|
||||
map[string]string{"id": id},
|
||||
"",
|
||||
" ",
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Marshalling Json: %w", err)
|
||||
}
|
||||
fmt.Println(string(jsonId))
|
||||
} else {
|
||||
fmt.Printf("ResourceID: %v\n", id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package resource
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/alessio/shellescape"
|
||||
|
@ -29,6 +30,10 @@ func ResourceGet(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()
|
||||
|
||||
|
@ -47,11 +52,27 @@ func ResourceGet(cmd *cobra.Command, args []string) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("Getting Resource: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
jsonResource, err := json.MarshalIndent(ResourceJsonOutput{
|
||||
FolderParentID: &folderParentID,
|
||||
Name: &name,
|
||||
Username: &username,
|
||||
URI: &uri,
|
||||
Password: &password,
|
||||
Description: &description,
|
||||
}, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonResource))
|
||||
} else {
|
||||
fmt.Printf("FolderParentID: %v\n", folderParentID)
|
||||
fmt.Printf("Name: %v\n", shellescape.StripUnsafe(name))
|
||||
fmt.Printf("Username: %v\n", shellescape.StripUnsafe(username))
|
||||
fmt.Printf("URI: %v\n", shellescape.StripUnsafe(uri))
|
||||
fmt.Printf("Password: %v\n", shellescape.StripUnsafe(password))
|
||||
fmt.Printf("Description: %v\n", shellescape.StripUnsafe(description))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
15
resource/json.go
Normal file
15
resource/json.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package resource
|
||||
|
||||
import "time"
|
||||
|
||||
type ResourceJsonOutput struct {
|
||||
ID *string `json:"id,omitempty"`
|
||||
FolderParentID *string `json:"folder_parent_id,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
Username *string `json:"username,omitempty"`
|
||||
URI *string `json:"uri,omitempty"`
|
||||
Password *string `json:"password,omitempty"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
CreatedTimestamp *time.Time `json:"created_timestamp,omitempty"`
|
||||
ModifiedTimestamp *time.Time `json:"modified_timestamp,omitempty"`
|
||||
}
|
|
@ -2,6 +2,7 @@ package resource
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -58,6 +59,10 @@ func ResourceList(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()
|
||||
|
||||
|
@ -78,6 +83,31 @@ func ResourceList(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Listing Resource: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
outputResources := []ResourceJsonOutput{}
|
||||
for i := range resources {
|
||||
_, _, _, _, pass, desc, err := helper.GetResource(ctx, client, resources[i].ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Get Resource %w", err)
|
||||
}
|
||||
outputResources = append(outputResources, ResourceJsonOutput{
|
||||
ID: &resources[i].ID,
|
||||
FolderParentID: &resources[i].FolderParentID,
|
||||
Name: &resources[i].Name,
|
||||
Username: &resources[i].Username,
|
||||
URI: &resources[i].URI,
|
||||
Password: &pass,
|
||||
Description: &desc,
|
||||
CreatedTimestamp: &resources[i].Created.Time,
|
||||
ModifiedTimestamp: &resources[i].Modified.Time,
|
||||
})
|
||||
}
|
||||
jsonResources, err := json.MarshalIndent(outputResources, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonResources))
|
||||
} else {
|
||||
data := pterm.TableData{columns}
|
||||
|
||||
for _, resource := range resources {
|
||||
|
@ -119,5 +149,6 @@ func ResourceList(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
|
||||
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package user
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/passbolt/go-passbolt-cli/util"
|
||||
|
@ -45,6 +46,11 @@ func UserCreate(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()
|
||||
|
||||
client, err := util.GetClient(ctx)
|
||||
|
@ -66,6 +72,18 @@ func UserCreate(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Creating User: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
jsonId, err := json.MarshalIndent(
|
||||
map[string]string{"id": id},
|
||||
"",
|
||||
" ",
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Marshalling Json: %w", err)
|
||||
}
|
||||
fmt.Println(string(jsonId))
|
||||
} else {
|
||||
fmt.Printf("UserID: %v\n", id)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
19
user/get.go
19
user/get.go
|
@ -2,6 +2,7 @@ package user
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/alessio/shellescape"
|
||||
|
@ -29,6 +30,10 @@ func UserGet(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()
|
||||
|
||||
|
@ -47,10 +52,22 @@ func UserGet(cmd *cobra.Command, args []string) error {
|
|||
if err != nil {
|
||||
return fmt.Errorf("Getting User: %w", err)
|
||||
}
|
||||
if jsonOutput {
|
||||
jsonUser, err := json.MarshalIndent(UserJsonOutput{
|
||||
Username: &username,
|
||||
FirstName: &firstname,
|
||||
LastName: &lastname,
|
||||
Role: &role,
|
||||
}, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonUser))
|
||||
} else {
|
||||
fmt.Printf("Username: %v\n", shellescape.StripUnsafe(username))
|
||||
fmt.Printf("FirstName: %v\n", shellescape.StripUnsafe(firstname))
|
||||
fmt.Printf("LastName: %v\n", shellescape.StripUnsafe(lastname))
|
||||
fmt.Printf("Role: %v\n", shellescape.StripUnsafe(role))
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
13
user/json.go
Normal file
13
user/json.go
Normal file
|
@ -0,0 +1,13 @@
|
|||
package user
|
||||
|
||||
import "time"
|
||||
|
||||
type UserJsonOutput struct {
|
||||
ID *string `json:"id,omitempty"`
|
||||
Username *string `json:"username,omitempty"`
|
||||
FirstName *string `json:"first_name,omitempty"`
|
||||
LastName *string `json:"last_name,omitempty"`
|
||||
Role *string `json:"role,omitempty"`
|
||||
CreatedTimestamp *time.Time `json:"created_timestamp,omitempty"`
|
||||
ModifiedTimestamp *time.Time `json:"modified_timestamp,omitempty"`
|
||||
}
|
25
user/list.go
25
user/list.go
|
@ -2,6 +2,7 @@ package user
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -57,6 +58,10 @@ func UserList(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()
|
||||
|
||||
|
@ -77,6 +82,25 @@ func UserList(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Listing User: %w", err)
|
||||
}
|
||||
|
||||
if jsonOutput {
|
||||
outputUsers := []UserJsonOutput{}
|
||||
for i := range users {
|
||||
outputUsers = append(outputUsers, UserJsonOutput{
|
||||
ID: &users[i].ID,
|
||||
Username: &users[i].Username,
|
||||
FirstName: &users[i].Profile.FirstName,
|
||||
LastName: &users[i].Profile.LastName,
|
||||
Role: &users[i].Role.Name,
|
||||
CreatedTimestamp: &users[i].Created.Time,
|
||||
ModifiedTimestamp: &users[i].Modified.Time,
|
||||
})
|
||||
}
|
||||
jsonUsers, err := json.MarshalIndent(outputUsers, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonUsers))
|
||||
} else {
|
||||
data := pterm.TableData{columns}
|
||||
|
||||
for _, user := range users {
|
||||
|
@ -106,5 +130,6 @@ func UserList(cmd *cobra.Command, args []string) error {
|
|||
}
|
||||
|
||||
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue