Skip to content

Commit

Permalink
Merge pull request #20 from passbolt/feature-json-output
Browse files Browse the repository at this point in the history
Json Output Support
  • Loading branch information
speatzle authored Dec 30, 2022
2 parents 45fe211 + 05dd936 commit 9a2944c
Show file tree
Hide file tree
Showing 20 changed files with 462 additions and 148 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
1 change: 1 addition & 0 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 18 additions & 1 deletion folder/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package folder

import (
"context"
"encoding/json"
"fmt"

"github.com/passbolt/go-passbolt-cli/util"
Expand Down Expand Up @@ -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()

Expand All @@ -53,6 +58,18 @@ func FolderCreate(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Creating Folder: %w", err)
}

fmt.Printf("FolderID: %v\n", id)
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
}
27 changes: 19 additions & 8 deletions folder/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()

Expand All @@ -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 changes: 11 additions & 0 deletions folder/json.go
Original file line number Diff line number Diff line change
@@ -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"`
}
65 changes: 44 additions & 21 deletions folder/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package folder

import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -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
Expand All @@ -64,30 +69,48 @@ func FolderList(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Listing Folder: %w", err)
}

data := pterm.TableData{columns}
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 {
entry := make([]string, len(columns))
for i := range columns {
switch strings.ToLower(columns[i]) {
case "id":
entry[i] = folder.ID
case "folderparentid":
entry[i] = folder.FolderParentID
case "name":
entry[i] = shellescape.StripUnsafe(folder.Name)
case "createdtimestamp":
entry[i] = folder.Created.Format(time.RFC3339)
case "modifiedtimestamp":
entry[i] = folder.Modified.Format(time.RFC3339)
default:
cmd.SilenceUsage = false
return fmt.Errorf("Unknown Column: %v", columns[i])
for _, folder := range folders {
entry := make([]string, len(columns))
for i := range columns {
switch strings.ToLower(columns[i]) {
case "id":
entry[i] = folder.ID
case "folderparentid":
entry[i] = folder.FolderParentID
case "name":
entry[i] = shellescape.StripUnsafe(folder.Name)
case "createdtimestamp":
entry[i] = folder.Created.Format(time.RFC3339)
case "modifiedtimestamp":
entry[i] = folder.Modified.Format(time.RFC3339)
default:
cmd.SilenceUsage = false
return fmt.Errorf("Unknown Column: %v", columns[i])
}
}
data = append(data, entry)
}
data = append(data, entry)
}

pterm.DefaultTable.WithHasHeader().WithData(data).Render()
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
}
return nil
}
19 changes: 18 additions & 1 deletion group/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package group

import (
"context"
"encoding/json"
"fmt"

"github.com/passbolt/go-passbolt-cli/util"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -74,6 +79,18 @@ func GroupCreate(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Creating Group: %w", err)
}

fmt.Printf("GroupID: %v\n", id)
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
}
77 changes: 53 additions & 24 deletions group/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package group

import (
"context"
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -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()

Expand All @@ -55,34 +60,58 @@ func GroupGet(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("Getting Group: %w", err)
}
fmt.Printf("Name: %v\n", name)
// Print Memberships
if len(columns) != 0 {
data := pterm.TableData{columns}

for _, membership := range memberships {
entry := make([]string, len(columns))
for i := range columns {
switch strings.ToLower(columns[i]) {
case "userid":
entry[i] = membership.UserID
case "isgroupmanager":
entry[i] = fmt.Sprint(membership.IsGroupManager)
case "username":
entry[i] = shellescape.StripUnsafe(membership.Username)
case "userfirstname":
entry[i] = shellescape.StripUnsafe(membership.UserFirstName)
case "userlastname":
entry[i] = shellescape.StripUnsafe(membership.UserLastName)
default:
cmd.SilenceUsage = false
return fmt.Errorf("Unknown Column: %v", columns[i])
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 {
data := pterm.TableData{columns}

for _, membership := range memberships {
entry := make([]string, len(columns))
for i := range columns {
switch strings.ToLower(columns[i]) {
case "userid":
entry[i] = membership.UserID
case "isgroupmanager":
entry[i] = fmt.Sprint(membership.IsGroupManager)
case "username":
entry[i] = shellescape.StripUnsafe(membership.Username)
case "userfirstname":
entry[i] = shellescape.StripUnsafe(membership.UserFirstName)
case "userlastname":
entry[i] = shellescape.StripUnsafe(membership.UserLastName)
default:
cmd.SilenceUsage = false
return fmt.Errorf("Unknown Column: %v", columns[i])
}
}
data = append(data, entry)
}
data = append(data, entry)
}

pterm.DefaultTable.WithHasHeader().WithData(data).Render()
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
}
}
return nil
}
19 changes: 19 additions & 0 deletions group/json.go
Original file line number Diff line number Diff line change
@@ -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"`
}
Loading

0 comments on commit 9a2944c

Please sign in to comment.