Add Json Output to Create Commands

This commit is contained in:
Samuel Lorch 2022-12-30 13:35:26 +01:00
parent 46ceb8176d
commit a2257bc011
4 changed files with 73 additions and 4 deletions

View file

@ -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
}