@@ -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,23 @@ 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+ FolderGetCmd .AddCommand (FolderPermissionCmd )
40+ FolderPermissionCmd .Flags ().String ("id" , "" , "id of Folder to Get" )
41+ 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" )
42+
2543}
2644
2745func FolderGet (cmd * cobra.Command , args []string ) error {
@@ -62,3 +80,94 @@ func FolderGet(cmd *cobra.Command, args []string) error {
6280 }
6381 return nil
6482}
83+
84+ func FolderPermission (cmd * cobra.Command , args []string ) error {
85+ folderID , err := cmd .Flags ().GetString ("id" )
86+ if err != nil {
87+ return err
88+ }
89+ columns , err := cmd .Flags ().GetStringArray ("column" )
90+ if err != nil {
91+ return err
92+ }
93+ if len (columns ) == 0 {
94+ return fmt .Errorf ("You need to specify atleast one column to return" )
95+ }
96+ jsonOutput , err := cmd .Flags ().GetBool ("json" )
97+ if err != nil {
98+ return err
99+ }
100+
101+ ctx := util .GetContext ()
102+
103+ client , err := util .GetClient (ctx )
104+ if err != nil {
105+ return err
106+ }
107+ defer util .SaveSessionKeysAndLogout (ctx , client )
108+ cmd .SilenceUsage = true
109+
110+ folder , err := client .GetFolder (ctx , folderID , & api.GetFolderOptions {
111+ ContainPermissions : true ,
112+ })
113+ if err != nil {
114+ return fmt .Errorf ("Listing Permission: %w" , err )
115+ }
116+
117+ permissions := folder .Permissions
118+
119+ if jsonOutput {
120+ outputPermissions := []PermissionJsonOutput {}
121+ for i := range permissions {
122+ outputPermissions = append (outputPermissions , PermissionJsonOutput {
123+ ID : & permissions [i ].ID ,
124+ Aco : & permissions [i ].ACO ,
125+ AcoForeignKey : & permissions [i ].ACOForeignKey ,
126+ Aro : & permissions [i ].ARO ,
127+ AroForeignKey : & permissions [i ].AROForeignKey ,
128+ Type : & permissions [i ].Type ,
129+ CreatedTimestamp : & permissions [i ].Created .Time ,
130+ ModifiedTimestamp : & permissions [i ].Modified .Time ,
131+ })
132+ }
133+ jsonPermissions , err := json .MarshalIndent (outputPermissions , "" , " " )
134+ if err != nil {
135+ return err
136+ }
137+ fmt .Println (string (jsonPermissions ))
138+ } else {
139+ data := pterm.TableData {columns }
140+
141+ for _ , permission := range permissions {
142+ entry := make ([]string , len (columns ))
143+ for i := range columns {
144+ switch strings .ToLower (columns [i ]) {
145+ case "id" :
146+ entry [i ] = permission .ID
147+ case "aco" :
148+ entry [i ] = permission .ACO
149+ case "acoforeignkey" :
150+ entry [i ] = permission .ACOForeignKey
151+ case "aro" :
152+ entry [i ] = permission .ARO
153+ case "aroforeignkey" :
154+ entry [i ] = permission .AROForeignKey
155+ case "type" :
156+ entry [i ] = strconv .Itoa (permission .Type )
157+ case "createdtimestamp" :
158+ entry [i ] = permission .Created .Format (time .RFC3339 )
159+ case "modifiedtimestamp" :
160+ entry [i ] = permission .Modified .Format (time .RFC3339 )
161+ default :
162+ cmd .SilenceUsage = false
163+ return fmt .Errorf ("Unknown Column: %v" , columns [i ])
164+ }
165+ }
166+ data = append (data , entry )
167+ }
168+
169+ pterm .DefaultTable .WithHasHeader ().WithData (data ).Render ()
170+ }
171+
172+ return nil
173+ }
0 commit comments