@@ -2,6 +2,7 @@ package resource
22
33import (
44 "context"
5+ "encoding/json"
56 "fmt"
67 "strings"
78 "time"
@@ -58,6 +59,10 @@ func ResourceList(cmd *cobra.Command, args []string) error {
5859 if len (columns ) == 0 {
5960 return fmt .Errorf ("You need to specify atleast one column to return" )
6061 }
62+ jsonOutput , err := cmd .Flags ().GetBool ("json" )
63+ if err != nil {
64+ return err
65+ }
6166
6267 ctx := util .GetContext ()
6368
@@ -78,46 +83,72 @@ func ResourceList(cmd *cobra.Command, args []string) error {
7883 return fmt .Errorf ("Listing Resource: %w" , err )
7984 }
8085
81- data := pterm.TableData {columns }
86+ if jsonOutput {
87+ outputResources := []ResourceJsonOutput {}
88+ for i := range resources {
89+ _ , _ , _ , _ , pass , desc , err := helper .GetResource (ctx , client , resources [i ].ID )
90+ if err != nil {
91+ return fmt .Errorf ("Get Resource %w" , err )
92+ }
93+ outputResources = append (outputResources , ResourceJsonOutput {
94+ ID : & resources [i ].ID ,
95+ FolderParentID : & resources [i ].FolderParentID ,
96+ Name : & resources [i ].Name ,
97+ Username : & resources [i ].Username ,
98+ URI : & resources [i ].URI ,
99+ Password : & pass ,
100+ Description : & desc ,
101+ CreatedTimestamp : & resources [i ].Created .Time ,
102+ ModifiedTimestamp : & resources [i ].Modified .Time ,
103+ })
104+ }
105+ jsonResources , err := json .MarshalIndent (outputResources , "" , " " )
106+ if err != nil {
107+ return err
108+ }
109+ fmt .Println (string (jsonResources ))
110+ } else {
111+ data := pterm.TableData {columns }
82112
83- for _ , resource := range resources {
84- entry := make ([]string , len (columns ))
85- for i := range columns {
86- switch strings .ToLower (columns [i ]) {
87- case "id" :
88- entry [i ] = resource .ID
89- case "folderparentid" :
90- entry [i ] = resource .FolderParentID
91- case "name" :
92- entry [i ] = shellescape .StripUnsafe (resource .Name )
93- case "username" :
94- entry [i ] = shellescape .StripUnsafe (resource .Username )
95- case "uri" :
96- entry [i ] = shellescape .StripUnsafe (resource .URI )
97- case "password" :
98- _ , _ , _ , _ , pass , _ , err := helper .GetResource (ctx , client , resource .ID )
99- if err != nil {
100- return fmt .Errorf ("Get Resource %w" , err )
101- }
102- entry [i ] = shellescape .StripUnsafe (pass )
103- case "description" :
104- _ , _ , _ , _ , _ , desc , err := helper .GetResource (ctx , client , resource .ID )
105- if err != nil {
106- return fmt .Errorf ("Get Resource %w" , err )
113+ for _ , resource := range resources {
114+ entry := make ([]string , len (columns ))
115+ for i := range columns {
116+ switch strings .ToLower (columns [i ]) {
117+ case "id" :
118+ entry [i ] = resource .ID
119+ case "folderparentid" :
120+ entry [i ] = resource .FolderParentID
121+ case "name" :
122+ entry [i ] = shellescape .StripUnsafe (resource .Name )
123+ case "username" :
124+ entry [i ] = shellescape .StripUnsafe (resource .Username )
125+ case "uri" :
126+ entry [i ] = shellescape .StripUnsafe (resource .URI )
127+ case "password" :
128+ _ , _ , _ , _ , pass , _ , err := helper .GetResource (ctx , client , resource .ID )
129+ if err != nil {
130+ return fmt .Errorf ("Get Resource %w" , err )
131+ }
132+ entry [i ] = shellescape .StripUnsafe (pass )
133+ case "description" :
134+ _ , _ , _ , _ , _ , desc , err := helper .GetResource (ctx , client , resource .ID )
135+ if err != nil {
136+ return fmt .Errorf ("Get Resource %w" , err )
137+ }
138+ entry [i ] = shellescape .StripUnsafe (desc )
139+ case "createdtimestamp" :
140+ entry [i ] = resource .Created .Format (time .RFC3339 )
141+ case "modifiedtimestamp" :
142+ entry [i ] = resource .Modified .Format (time .RFC3339 )
143+ default :
144+ cmd .SilenceUsage = false
145+ return fmt .Errorf ("Unknown Column: %v" , columns [i ])
107146 }
108- entry [i ] = shellescape .StripUnsafe (desc )
109- case "createdtimestamp" :
110- entry [i ] = resource .Created .Format (time .RFC3339 )
111- case "modifiedtimestamp" :
112- entry [i ] = resource .Modified .Format (time .RFC3339 )
113- default :
114- cmd .SilenceUsage = false
115- return fmt .Errorf ("Unknown Column: %v" , columns [i ])
116147 }
148+ data = append (data , entry )
117149 }
118- data = append (data , entry )
119- }
120150
121- pterm .DefaultTable .WithHasHeader ().WithData (data ).Render ()
151+ pterm .DefaultTable .WithHasHeader ().WithData (data ).Render ()
152+ }
122153 return nil
123154}
0 commit comments