add json output to command

This commit is contained in:
Alekzus 2022-03-14 17:25:20 +01:00
parent 7d0c6e3199
commit be07d31747
3 changed files with 83 additions and 44 deletions

View file

@ -2,10 +2,11 @@ package user
import (
"context"
"encoding/json"
"fmt"
"github.com/passbolt/go-passbolt-cli/util"
"github.com/passbolt/go-passbolt/helper"
"github.com/passbolt/go-passbolt/api"
"github.com/spf13/cobra"
)
@ -45,6 +46,11 @@ func UserCreate(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
jsonOutput, err := cmd.Flags().GetBool("json")
if err != nil {
return err
}
ctx := util.GetContext()
client, err := util.GetClient(ctx)
@ -54,18 +60,31 @@ func UserCreate(cmd *cobra.Command, args []string) error {
defer client.Logout(context.TODO())
cmd.SilenceUsage = true
id, err := helper.CreateUser(
user, err := client.CreateUser(
ctx,
client,
role,
username,
firstname,
lastname,
api.User{
Username: username,
Profile: &api.Profile{
FirstName: firstname,
LastName: lastname,
},
Role: &api.Role{
Name: role,
},
},
)
if err != nil {
return fmt.Errorf("Creating User: %w", err)
}
fmt.Printf("UserID: %v\n", id)
if jsonOutput {
jsonUser, err := json.MarshalIndent(user, "", " ")
if err != nil {
return err
}
fmt.Println(string(jsonUser))
} else {
fmt.Printf("UserID: %v\n", user.ID)
}
return nil
}

View file

@ -2,11 +2,11 @@ package user
import (
"context"
"encoding/json"
"fmt"
"github.com/alessio/shellescape"
"github.com/passbolt/go-passbolt-cli/util"
"github.com/passbolt/go-passbolt/helper"
"github.com/spf13/cobra"
)
@ -29,6 +29,10 @@ func UserGet(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
jsonOutput, err := cmd.Flags().GetBool("json")
if err != nil {
return err
}
ctx := util.GetContext()
@ -39,18 +43,22 @@ func UserGet(cmd *cobra.Command, args []string) error {
defer client.Logout(context.TODO())
cmd.SilenceUsage = true
role, username, firstname, lastname, err := helper.GetUser(
ctx,
client,
id,
)
user, err := client.GetUser(ctx, id)
if err != nil {
return fmt.Errorf("Getting User: %w", err)
}
fmt.Printf("Username: %v\n", shellescape.StripUnsafe(username))
fmt.Printf("FirstName: %v\n", shellescape.StripUnsafe(firstname))
fmt.Printf("LastName: %v\n", shellescape.StripUnsafe(lastname))
fmt.Printf("Role: %v\n", shellescape.StripUnsafe(role))
if jsonOutput {
jsonUser, err := json.MarshalIndent(*user, "", " ")
if err != nil {
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
}

View file

@ -2,6 +2,7 @@ package user
import (
"context"
"encoding/json"
"fmt"
"strings"
@ -56,7 +57,10 @@ func UserList(cmd *cobra.Command, args []string) error {
if len(columns) == 0 {
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()
client, err := util.GetClient(ctx)
@ -76,6 +80,13 @@ func UserList(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Listing User: %w", err)
}
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 {
@ -101,5 +112,6 @@ func UserList(cmd *cobra.Command, args []string) error {
}
pterm.DefaultTable.WithHasHeader().WithData(data).Render()
}
return nil
}