mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-15 03:58:21 +00:00
Add User Json Output
This commit is contained in:
parent
a2257bc011
commit
c8dc067697
3 changed files with 87 additions and 32 deletions
19
user/get.go
19
user/get.go
|
@ -2,6 +2,7 @@ package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/alessio/shellescape"
|
"github.com/alessio/shellescape"
|
||||||
|
@ -29,6 +30,10 @@ func UserGet(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,10 +52,22 @@ func UserGet(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Getting User: %w", err)
|
return fmt.Errorf("Getting User: %w", err)
|
||||||
}
|
}
|
||||||
|
if jsonOutput {
|
||||||
|
jsonUser, err := json.MarshalIndent(UserJsonOutput{
|
||||||
|
Username: &username,
|
||||||
|
FirstName: &firstname,
|
||||||
|
LastName: &lastname,
|
||||||
|
Role: &role,
|
||||||
|
}, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonUser))
|
||||||
|
} else {
|
||||||
fmt.Printf("Username: %v\n", shellescape.StripUnsafe(username))
|
fmt.Printf("Username: %v\n", shellescape.StripUnsafe(username))
|
||||||
fmt.Printf("FirstName: %v\n", shellescape.StripUnsafe(firstname))
|
fmt.Printf("FirstName: %v\n", shellescape.StripUnsafe(firstname))
|
||||||
fmt.Printf("LastName: %v\n", shellescape.StripUnsafe(lastname))
|
fmt.Printf("LastName: %v\n", shellescape.StripUnsafe(lastname))
|
||||||
fmt.Printf("Role: %v\n", shellescape.StripUnsafe(role))
|
fmt.Printf("Role: %v\n", shellescape.StripUnsafe(role))
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
13
user/json.go
Normal file
13
user/json.go
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
package user
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
type UserJsonOutput struct {
|
||||||
|
ID *string `json:"id,omitempty"`
|
||||||
|
Username *string `json:"username,omitempty"`
|
||||||
|
FirstName *string `json:"first_name,omitempty"`
|
||||||
|
LastName *string `json:"last_name,omitempty"`
|
||||||
|
Role *string `json:"role,omitempty"`
|
||||||
|
CreatedTimestamp *time.Time `json:"created_timestamp,omitempty"`
|
||||||
|
ModifiedTimestamp *time.Time `json:"modified_timestamp,omitempty"`
|
||||||
|
}
|
25
user/list.go
25
user/list.go
|
@ -2,6 +2,7 @@ package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
@ -57,6 +58,10 @@ func UserList(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()
|
||||||
|
|
||||||
|
@ -77,6 +82,25 @@ func UserList(cmd *cobra.Command, args []string) error {
|
||||||
return fmt.Errorf("Listing User: %w", err)
|
return fmt.Errorf("Listing User: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if jsonOutput {
|
||||||
|
outputUsers := []UserJsonOutput{}
|
||||||
|
for i := range users {
|
||||||
|
outputUsers = append(outputUsers, UserJsonOutput{
|
||||||
|
ID: &users[i].ID,
|
||||||
|
Username: &users[i].Username,
|
||||||
|
FirstName: &users[i].Profile.FirstName,
|
||||||
|
LastName: &users[i].Profile.LastName,
|
||||||
|
Role: &users[i].Role.Name,
|
||||||
|
CreatedTimestamp: &users[i].Created.Time,
|
||||||
|
ModifiedTimestamp: &users[i].Modified.Time,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
jsonUsers, err := json.MarshalIndent(outputUsers, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonUsers))
|
||||||
|
} else {
|
||||||
data := pterm.TableData{columns}
|
data := pterm.TableData{columns}
|
||||||
|
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
|
@ -106,5 +130,6 @@ func UserList(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue