mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 02:58:20 +00:00
Add Resource Json Output
This commit is contained in:
parent
e8bb791686
commit
269e117fbf
3 changed files with 111 additions and 44 deletions
|
@ -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)
|
||||
}
|
||||
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))
|
||||
|
||||
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"`
|
||||
}
|
107
resource/list.go
107
resource/list.go
|
@ -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,46 +83,72 @@ func ResourceList(cmd *cobra.Command, args []string) error {
|
|||
return fmt.Errorf("Listing Resource: %w", err)
|
||||
}
|
||||
|
||||
data := pterm.TableData{columns}
|
||||
|
||||
for _, resource := range resources {
|
||||
entry := make([]string, len(columns))
|
||||
for i := range columns {
|
||||
switch strings.ToLower(columns[i]) {
|
||||
case "id":
|
||||
entry[i] = resource.ID
|
||||
case "folderparentid":
|
||||
entry[i] = resource.FolderParentID
|
||||
case "name":
|
||||
entry[i] = shellescape.StripUnsafe(resource.Name)
|
||||
case "username":
|
||||
entry[i] = shellescape.StripUnsafe(resource.Username)
|
||||
case "uri":
|
||||
entry[i] = shellescape.StripUnsafe(resource.URI)
|
||||
case "password":
|
||||
_, _, _, _, pass, _, err := helper.GetResource(ctx, client, resource.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Get Resource %w", err)
|
||||
}
|
||||
entry[i] = shellescape.StripUnsafe(pass)
|
||||
case "description":
|
||||
_, _, _, _, _, desc, err := helper.GetResource(ctx, client, resource.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Get Resource %w", err)
|
||||
}
|
||||
entry[i] = shellescape.StripUnsafe(desc)
|
||||
case "createdtimestamp":
|
||||
entry[i] = resource.Created.Format(time.RFC3339)
|
||||
case "modifiedtimestamp":
|
||||
entry[i] = resource.Modified.Format(time.RFC3339)
|
||||
default:
|
||||
cmd.SilenceUsage = false
|
||||
return fmt.Errorf("Unknown Column: %v", columns[i])
|
||||
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,
|
||||
})
|
||||
}
|
||||
data = append(data, entry)
|
||||
}
|
||||
jsonResources, err := json.MarshalIndent(outputResources, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println(string(jsonResources))
|
||||
} else {
|
||||
data := pterm.TableData{columns}
|
||||
|
||||
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
||||
for _, resource := range resources {
|
||||
entry := make([]string, len(columns))
|
||||
for i := range columns {
|
||||
switch strings.ToLower(columns[i]) {
|
||||
case "id":
|
||||
entry[i] = resource.ID
|
||||
case "folderparentid":
|
||||
entry[i] = resource.FolderParentID
|
||||
case "name":
|
||||
entry[i] = shellescape.StripUnsafe(resource.Name)
|
||||
case "username":
|
||||
entry[i] = shellescape.StripUnsafe(resource.Username)
|
||||
case "uri":
|
||||
entry[i] = shellescape.StripUnsafe(resource.URI)
|
||||
case "password":
|
||||
_, _, _, _, pass, _, err := helper.GetResource(ctx, client, resource.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Get Resource %w", err)
|
||||
}
|
||||
entry[i] = shellescape.StripUnsafe(pass)
|
||||
case "description":
|
||||
_, _, _, _, _, desc, err := helper.GetResource(ctx, client, resource.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Get Resource %w", err)
|
||||
}
|
||||
entry[i] = shellescape.StripUnsafe(desc)
|
||||
case "createdtimestamp":
|
||||
entry[i] = resource.Created.Format(time.RFC3339)
|
||||
case "modifiedtimestamp":
|
||||
entry[i] = resource.Modified.Format(time.RFC3339)
|
||||
default:
|
||||
cmd.SilenceUsage = false
|
||||
return fmt.Errorf("Unknown Column: %v", columns[i])
|
||||
}
|
||||
}
|
||||
data = append(data, entry)
|
||||
}
|
||||
|
||||
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue