@@ -4,9 +4,14 @@ import (
44 "context"
55 "encoding/json"
66 "fmt"
7+ "strconv"
8+ "strings"
9+ "time"
710
811 "al.essio.dev/pkg/shellescape"
912 "github.com/passbolt/go-passbolt-cli/util"
13+ "github.com/passbolt/go-passbolt/api"
14+ "github.com/pterm/pterm"
1015 "github.com/spf13/cobra"
1116)
1217
@@ -18,10 +23,25 @@ var FolderGetCmd = &cobra.Command{
1823 RunE : FolderGet ,
1924}
2025
26+ // FolderPermissionCmd Gets Permissions for Passbolt Folder
27+ var FolderPermissionCmd = & cobra.Command {
28+ Use : "permission" ,
29+ Short : "Gets Permissions for a Passbolt Folder" ,
30+ Long : `Gets Permissions for a Passbolt Folder` ,
31+ Aliases : []string {"permissions" },
32+ RunE : FolderPermission ,
33+ }
34+
2135func init () {
2236 FolderGetCmd .Flags ().String ("id" , "" , "id of Folder to Get" )
2337
2438 FolderGetCmd .MarkFlagRequired ("id" )
39+
40+ FolderGetCmd .AddCommand (FolderPermissionCmd )
41+ FolderPermissionCmd .Flags ().String ("id" , "" , "id of Folder to get permissions for" )
42+ FolderPermissionCmd .Flags ().StringArrayP ("column" , "c" , []string {"ID" , "Aco" , "AcoForeignKey" , "Aro" , "AroForeignKey" , "Type" }, "Columns to return, possible Columns:\n ID, Aco, AcoForeignKey, Aro, AroForeignKey, Type, CreatedTimestamp, ModifiedTimestamp" )
43+
44+ FolderPermissionCmd .MarkFlagRequired ("id" )
2545}
2646
2747func FolderGet (cmd * cobra.Command , args []string ) error {
@@ -62,3 +82,94 @@ func FolderGet(cmd *cobra.Command, args []string) error {
6282 }
6383 return nil
6484}
85+
86+ func FolderPermission (cmd * cobra.Command , args []string ) error {
87+ folderID , err := cmd .Flags ().GetString ("id" )
88+ if err != nil {
89+ return err
90+ }
91+ columns , err := cmd .Flags ().GetStringArray ("column" )
92+ if err != nil {
93+ return err
94+ }
95+ if len (columns ) == 0 {
96+ return fmt .Errorf ("You need to specify at least one column to return" )
97+ }
98+ jsonOutput , err := cmd .Flags ().GetBool ("json" )
99+ if err != nil {
100+ return err
101+ }
102+
103+ ctx := util .GetContext ()
104+
105+ client , err := util .GetClient (ctx )
106+ if err != nil {
107+ return err
108+ }
109+ defer util .SaveSessionKeysAndLogout (ctx , client )
110+ cmd .SilenceUsage = true
111+
112+ folder , err := client .GetFolder (ctx , folderID , & api.GetFolderOptions {
113+ ContainPermissions : true ,
114+ })
115+ if err != nil {
116+ return fmt .Errorf ("Listing Permission: %w" , err )
117+ }
118+
119+ permissions := folder .Permissions
120+
121+ if jsonOutput {
122+ outputPermissions := []util.PermissionJsonOutput {}
123+ for i := range permissions {
124+ outputPermissions = append (outputPermissions , util.PermissionJsonOutput {
125+ ID : & permissions [i ].ID ,
126+ Aco : & permissions [i ].ACO ,
127+ AcoForeignKey : & permissions [i ].ACOForeignKey ,
128+ Aro : & permissions [i ].ARO ,
129+ AroForeignKey : & permissions [i ].AROForeignKey ,
130+ Type : & permissions [i ].Type ,
131+ CreatedTimestamp : & permissions [i ].Created .Time ,
132+ ModifiedTimestamp : & permissions [i ].Modified .Time ,
133+ })
134+ }
135+ jsonPermissions , err := json .MarshalIndent (outputPermissions , "" , " " )
136+ if err != nil {
137+ return err
138+ }
139+ fmt .Println (string (jsonPermissions ))
140+ } else {
141+ data := pterm.TableData {columns }
142+
143+ for _ , permission := range permissions {
144+ entry := make ([]string , len (columns ))
145+ for i := range columns {
146+ switch strings .ToLower (columns [i ]) {
147+ case "id" :
148+ entry [i ] = permission .ID
149+ case "aco" :
150+ entry [i ] = permission .ACO
151+ case "acoforeignkey" :
152+ entry [i ] = permission .ACOForeignKey
153+ case "aro" :
154+ entry [i ] = permission .ARO
155+ case "aroforeignkey" :
156+ entry [i ] = permission .AROForeignKey
157+ case "type" :
158+ entry [i ] = strconv .Itoa (permission .Type )
159+ case "createdtimestamp" :
160+ entry [i ] = permission .Created .Format (time .RFC3339 )
161+ case "modifiedtimestamp" :
162+ entry [i ] = permission .Modified .Format (time .RFC3339 )
163+ default :
164+ cmd .SilenceUsage = false
165+ return fmt .Errorf ("Unknown Column: %v" , columns [i ])
166+ }
167+ }
168+ data = append (data , entry )
169+ }
170+
171+ pterm .DefaultTable .WithHasHeader ().WithData (data ).Render ()
172+ }
173+
174+ return nil
175+ }
0 commit comments