Add Resource Json Output

This commit is contained in:
Samuel Lorch 2022-12-30 16:08:46 +01:00
parent e8bb791686
commit 269e117fbf
3 changed files with 111 additions and 44 deletions

View file

@ -2,6 +2,7 @@ package resource
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"github.com/alessio/shellescape" "github.com/alessio/shellescape"
@ -29,6 +30,10 @@ func ResourceGet(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return err return err
} }
jsonOutput, err := cmd.Flags().GetBool("json")
if err != nil {
return err
}
ctx := util.GetContext() ctx := util.GetContext()
@ -47,11 +52,27 @@ func ResourceGet(cmd *cobra.Command, args []string) error {
if err != nil { if err != nil {
return fmt.Errorf("Getting Resource: %w", err) return fmt.Errorf("Getting Resource: %w", err)
} }
fmt.Printf("FolderParentID: %v\n", folderParentID)
fmt.Printf("Name: %v\n", shellescape.StripUnsafe(name)) if jsonOutput {
fmt.Printf("Username: %v\n", shellescape.StripUnsafe(username)) jsonResource, err := json.MarshalIndent(ResourceJsonOutput{
fmt.Printf("URI: %v\n", shellescape.StripUnsafe(uri)) FolderParentID: &folderParentID,
fmt.Printf("Password: %v\n", shellescape.StripUnsafe(password)) Name: &name,
fmt.Printf("Description: %v\n", shellescape.StripUnsafe(description)) 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 return nil
} }

15
resource/json.go Normal file
View 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"`
}

View file

@ -2,6 +2,7 @@ package resource
import ( import (
"context" "context"
"encoding/json"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -58,6 +59,10 @@ func ResourceList(cmd *cobra.Command, args []string) error {
if len(columns) == 0 { if len(columns) == 0 {
return fmt.Errorf("You need to specify atleast one column to return") 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() ctx := util.GetContext()
@ -78,46 +83,72 @@ func ResourceList(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Listing Resource: %w", err) return fmt.Errorf("Listing Resource: %w", err)
} }
data := pterm.TableData{columns} if jsonOutput {
outputResources := []ResourceJsonOutput{}
for _, resource := range resources { for i := range resources {
entry := make([]string, len(columns)) _, _, _, _, pass, desc, err := helper.GetResource(ctx, client, resources[i].ID)
for i := range columns { if err != nil {
switch strings.ToLower(columns[i]) { return fmt.Errorf("Get Resource %w", err)
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])
} }
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 return nil
} }