From a2257bc011438d096e41f5a0a8bead0f508fead4 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Fri, 30 Dec 2022 13:35:26 +0100 Subject: [PATCH] Add Json Output to Create Commands --- folder/create.go | 19 ++++++++++++++++++- group/create.go | 19 ++++++++++++++++++- resource/create.go | 19 ++++++++++++++++++- user/create.go | 20 +++++++++++++++++++- 4 files changed, 73 insertions(+), 4 deletions(-) diff --git a/folder/create.go b/folder/create.go index 5b18b0c..8e9c464 100644 --- a/folder/create.go +++ b/folder/create.go @@ -2,6 +2,7 @@ package folder import ( "context" + "encoding/json" "fmt" "github.com/passbolt/go-passbolt-cli/util" @@ -33,6 +34,10 @@ func FolderCreate(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() @@ -53,6 +58,18 @@ func FolderCreate(cmd *cobra.Command, args []string) error { return fmt.Errorf("Creating Folder: %w", err) } - fmt.Printf("FolderID: %v\n", id) + if jsonOutput { + jsonId, err := json.MarshalIndent( + map[string]string{"id": id}, + "", + " ", + ) + if err != nil { + return fmt.Errorf("Marshalling Json: %w", err) + } + fmt.Println(string(jsonId)) + } else { + fmt.Printf("FolderID: %v\n", id) + } return nil } diff --git a/group/create.go b/group/create.go index 4c1b8ae..6e8caa2 100644 --- a/group/create.go +++ b/group/create.go @@ -2,6 +2,7 @@ package group import ( "context" + "encoding/json" "fmt" "github.com/passbolt/go-passbolt-cli/util" @@ -40,6 +41,10 @@ func GroupCreate(cmd *cobra.Command, args []string) error { if err != nil { return err } + jsonOutput, err := cmd.Flags().GetBool("json") + if err != nil { + return err + } ops := []helper.GroupMembershipOperation{} for _, user := range users { @@ -74,6 +79,18 @@ func GroupCreate(cmd *cobra.Command, args []string) error { return fmt.Errorf("Creating Group: %w", err) } - fmt.Printf("GroupID: %v\n", id) + if jsonOutput { + jsonId, err := json.MarshalIndent( + map[string]string{"id": id}, + "", + " ", + ) + if err != nil { + return fmt.Errorf("Marshalling Json: %w", err) + } + fmt.Println(string(jsonId)) + } else { + fmt.Printf("GroupID: %v\n", id) + } return nil } diff --git a/resource/create.go b/resource/create.go index 799c0d5..a0866ca 100644 --- a/resource/create.go +++ b/resource/create.go @@ -2,6 +2,7 @@ package resource import ( "context" + "encoding/json" "fmt" "github.com/passbolt/go-passbolt-cli/util" @@ -54,6 +55,10 @@ func ResourceCreate(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() @@ -78,6 +83,18 @@ func ResourceCreate(cmd *cobra.Command, args []string) error { return fmt.Errorf("Creating Resource: %w", err) } - fmt.Printf("ResourceID: %v\n", id) + if jsonOutput { + jsonId, err := json.MarshalIndent( + map[string]string{"id": id}, + "", + " ", + ) + if err != nil { + return fmt.Errorf("Marshalling Json: %w", err) + } + fmt.Println(string(jsonId)) + } else { + fmt.Printf("ResourceID: %v\n", id) + } return nil } diff --git a/user/create.go b/user/create.go index 4c1b937..34465f6 100644 --- a/user/create.go +++ b/user/create.go @@ -2,6 +2,7 @@ package user import ( "context" + "encoding/json" "fmt" "github.com/passbolt/go-passbolt-cli/util" @@ -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) @@ -66,6 +72,18 @@ func UserCreate(cmd *cobra.Command, args []string) error { return fmt.Errorf("Creating User: %w", err) } - fmt.Printf("UserID: %v\n", id) + if jsonOutput { + jsonId, err := json.MarshalIndent( + map[string]string{"id": id}, + "", + " ", + ) + if err != nil { + return fmt.Errorf("Marshalling Json: %w", err) + } + fmt.Println(string(jsonId)) + } else { + fmt.Printf("UserID: %v\n", id) + } return nil }