mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 19:08:21 +00:00
add json output to command
This commit is contained in:
parent
7d0c6e3199
commit
be07d31747
3 changed files with 83 additions and 44 deletions
|
@ -2,10 +2,11 @@ package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/passbolt/go-passbolt-cli/util"
|
"github.com/passbolt/go-passbolt-cli/util"
|
||||||
"github.com/passbolt/go-passbolt/helper"
|
"github.com/passbolt/go-passbolt/api"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -45,6 +46,11 @@ func UserCreate(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()
|
||||||
|
|
||||||
client, err := util.GetClient(ctx)
|
client, err := util.GetClient(ctx)
|
||||||
|
@ -54,18 +60,31 @@ func UserCreate(cmd *cobra.Command, args []string) error {
|
||||||
defer client.Logout(context.TODO())
|
defer client.Logout(context.TODO())
|
||||||
cmd.SilenceUsage = true
|
cmd.SilenceUsage = true
|
||||||
|
|
||||||
id, err := helper.CreateUser(
|
user, err := client.CreateUser(
|
||||||
ctx,
|
ctx,
|
||||||
client,
|
api.User{
|
||||||
role,
|
Username: username,
|
||||||
username,
|
Profile: &api.Profile{
|
||||||
firstname,
|
FirstName: firstname,
|
||||||
lastname,
|
LastName: lastname,
|
||||||
|
},
|
||||||
|
Role: &api.Role{
|
||||||
|
Name: role,
|
||||||
|
},
|
||||||
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Creating User: %w", err)
|
return fmt.Errorf("Creating User: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf("UserID: %v\n", id)
|
if jsonOutput {
|
||||||
return nil
|
jsonUser, err := json.MarshalIndent(user, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonUser))
|
||||||
|
} else {
|
||||||
|
fmt.Printf("UserID: %v\n", user.ID)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
28
user/get.go
28
user/get.go
|
@ -2,11 +2,11 @@ package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/alessio/shellescape"
|
"github.com/alessio/shellescape"
|
||||||
"github.com/passbolt/go-passbolt-cli/util"
|
"github.com/passbolt/go-passbolt-cli/util"
|
||||||
"github.com/passbolt/go-passbolt/helper"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -29,6 +29,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()
|
||||||
|
|
||||||
|
@ -39,18 +43,22 @@ func UserGet(cmd *cobra.Command, args []string) error {
|
||||||
defer client.Logout(context.TODO())
|
defer client.Logout(context.TODO())
|
||||||
cmd.SilenceUsage = true
|
cmd.SilenceUsage = true
|
||||||
|
|
||||||
role, username, firstname, lastname, err := helper.GetUser(
|
user, err := client.GetUser(ctx, id)
|
||||||
ctx,
|
|
||||||
client,
|
|
||||||
id,
|
|
||||||
)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Getting User: %w", err)
|
return fmt.Errorf("Getting User: %w", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("Username: %v\n", shellescape.StripUnsafe(username))
|
if jsonOutput {
|
||||||
fmt.Printf("FirstName: %v\n", shellescape.StripUnsafe(firstname))
|
jsonUser, err := json.MarshalIndent(*user, "", " ")
|
||||||
fmt.Printf("LastName: %v\n", shellescape.StripUnsafe(lastname))
|
if err != nil {
|
||||||
fmt.Printf("Role: %v\n", shellescape.StripUnsafe(role))
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonUser))
|
||||||
|
} else {
|
||||||
|
fmt.Printf("Username: %v\n", shellescape.StripUnsafe(user.Username))
|
||||||
|
fmt.Printf("FirstName: %v\n", shellescape.StripUnsafe(user.Profile.FirstName))
|
||||||
|
fmt.Printf("LastName: %v\n", shellescape.StripUnsafe(user.Profile.LastName))
|
||||||
|
fmt.Printf("Role: %v\n", shellescape.StripUnsafe(user.Role.Name))
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
62
user/list.go
62
user/list.go
|
@ -2,6 +2,7 @@ package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -56,7 +57,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()
|
||||||
|
|
||||||
client, err := util.GetClient(ctx)
|
client, err := util.GetClient(ctx)
|
||||||
|
@ -76,30 +80,38 @@ func UserList(cmd *cobra.Command, args []string) error {
|
||||||
return fmt.Errorf("Listing User: %w", err)
|
return fmt.Errorf("Listing User: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
data := pterm.TableData{columns}
|
if jsonOutput {
|
||||||
|
jsonUser, err := json.MarshalIndent(users, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
fmt.Println(string(jsonUser))
|
||||||
|
} else {
|
||||||
|
data := pterm.TableData{columns}
|
||||||
|
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
entry := make([]string, len(columns))
|
entry := make([]string, len(columns))
|
||||||
for i := range columns {
|
for i := range columns {
|
||||||
switch strings.ToLower(columns[i]) {
|
switch strings.ToLower(columns[i]) {
|
||||||
case "id":
|
case "id":
|
||||||
entry[i] = user.ID
|
entry[i] = user.ID
|
||||||
case "username":
|
case "username":
|
||||||
entry[i] = shellescape.StripUnsafe(user.Username)
|
entry[i] = shellescape.StripUnsafe(user.Username)
|
||||||
case "firstname":
|
case "firstname":
|
||||||
entry[i] = shellescape.StripUnsafe(user.Profile.FirstName)
|
entry[i] = shellescape.StripUnsafe(user.Profile.FirstName)
|
||||||
case "lastname":
|
case "lastname":
|
||||||
entry[i] = shellescape.StripUnsafe(user.Profile.LastName)
|
entry[i] = shellescape.StripUnsafe(user.Profile.LastName)
|
||||||
case "role":
|
case "role":
|
||||||
entry[i] = shellescape.StripUnsafe(user.Role.Name)
|
entry[i] = shellescape.StripUnsafe(user.Role.Name)
|
||||||
default:
|
default:
|
||||||
cmd.SilenceUsage = false
|
cmd.SilenceUsage = false
|
||||||
return fmt.Errorf("Unknown Column: %v", columns[i])
|
return fmt.Errorf("Unknown Column: %v", columns[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
data = append(data, entry)
|
data = append(data, entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
|
||||||
return nil
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue