From 1b129332ae6abd0bc53ca319211c271b2a3e603b Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Wed, 8 Sep 2021 11:12:34 +0200 Subject: [PATCH] add folder commands --- cmd/create.go | 2 ++ cmd/delete.go | 2 ++ cmd/get.go | 2 ++ cmd/list.go | 3 +- cmd/update.go | 3 +- folder/create.go | 58 ++++++++++++++++++++++++++++++++ folder/delete.go | 43 ++++++++++++++++++++++++ folder/get.go | 52 +++++++++++++++++++++++++++++ folder/list.go | 86 ++++++++++++++++++++++++++++++++++++++++++++++++ folder/move.go | 57 ++++++++++++++++++++++++++++++++ folder/share.go | 69 ++++++++++++++++++++++++++++++++++++++ folder/update.go | 57 ++++++++++++++++++++++++++++++++ 12 files changed, 432 insertions(+), 2 deletions(-) create mode 100644 folder/create.go create mode 100644 folder/delete.go create mode 100644 folder/get.go create mode 100644 folder/list.go create mode 100644 folder/move.go create mode 100644 folder/share.go create mode 100644 folder/update.go diff --git a/cmd/create.go b/cmd/create.go index 9cafa10..339677a 100644 --- a/cmd/create.go +++ b/cmd/create.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/folder" "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -16,4 +17,5 @@ var createCmd = &cobra.Command{ func init() { rootCmd.AddCommand(createCmd) createCmd.AddCommand(resource.ResourceCreateCmd) + createCmd.AddCommand(folder.FolderCreateCmd) } diff --git a/cmd/delete.go b/cmd/delete.go index f93505f..85e4ff5 100644 --- a/cmd/delete.go +++ b/cmd/delete.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/folder" "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -16,6 +17,7 @@ var deleteCmd = &cobra.Command{ func init() { rootCmd.AddCommand(deleteCmd) deleteCmd.AddCommand(resource.ResourceDeleteCmd) + deleteCmd.AddCommand(folder.FolderDeleteCmd) deleteCmd.PersistentFlags().String("id", "", "ID of the Entity to Delete") } diff --git a/cmd/get.go b/cmd/get.go index 550fc19..cf632ec 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/folder" "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -16,4 +17,5 @@ var getCmd = &cobra.Command{ func init() { rootCmd.AddCommand(getCmd) getCmd.AddCommand(resource.ResourceGetCmd) + getCmd.AddCommand(folder.FolderGetCmd) } diff --git a/cmd/list.go b/cmd/list.go index 7ee83c3..f89c42c 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/folder" "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -15,6 +16,6 @@ var listCmd = &cobra.Command{ func init() { rootCmd.AddCommand(listCmd) - listCmd.AddCommand(resource.ResourceListCmd) + listCmd.AddCommand(folder.FolderListCmd) } diff --git a/cmd/update.go b/cmd/update.go index af8d6af..c332df9 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -1,6 +1,7 @@ package cmd import ( + "github.com/speatzle/go-passbolt-cli/folder" "github.com/speatzle/go-passbolt-cli/resource" "github.com/spf13/cobra" ) @@ -15,6 +16,6 @@ var updateCmd = &cobra.Command{ func init() { rootCmd.AddCommand(updateCmd) - updateCmd.AddCommand(resource.ResourceUpdateCmd) + updateCmd.AddCommand(folder.FolderUpdateCmd) } diff --git a/folder/create.go b/folder/create.go new file mode 100644 index 0000000..bf60448 --- /dev/null +++ b/folder/create.go @@ -0,0 +1,58 @@ +package folder + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// FolderCreateCmd Creates a Passbolt Folder +var FolderCreateCmd = &cobra.Command{ + Use: "folder", + Short: "Creates a Passbolt Folder", + Long: `Creates a Passbolt Folder and Returns the Folders ID`, + RunE: FolderCreate, +} + +func init() { + FolderCreateCmd.Flags().StringP("name", "n", "", "Folder Name") + FolderCreateCmd.Flags().StringP("folderParentID", "f", "", "Folder in which to create the Folder") + + FolderCreateCmd.MarkFlagRequired("name") +} + +func FolderCreate(cmd *cobra.Command, args []string) error { + folderParentID, err := cmd.Flags().GetString("folderParentID") + if err != nil { + return err + } + name, err := cmd.Flags().GetString("name") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + id, err := helper.CreateFolder( + ctx, + client, + folderParentID, + name, + ) + if err != nil { + return fmt.Errorf("Creating Folder: %w", err) + } + + fmt.Printf("FolderID: %v\n", id) + return nil +} diff --git a/folder/delete.go b/folder/delete.go new file mode 100644 index 0000000..35e32e3 --- /dev/null +++ b/folder/delete.go @@ -0,0 +1,43 @@ +package folder + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/spf13/cobra" +) + +// FolderDeleteCmd Deletes a Folder +var FolderDeleteCmd = &cobra.Command{ + Use: "folder", + Short: "Deletes a Passbolt Folder", + Long: `Deletes a Passbolt Folder`, + RunE: FolderDelete, +} + +func FolderDelete(cmd *cobra.Command, args []string) error { + folderID, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + + if folderID == "" { + return fmt.Errorf("No ID to Delete Provided") + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + client.DeleteFolder(ctx, folderID) + if err != nil { + return fmt.Errorf("Deleting Folder: %w", err) + } + return nil +} diff --git a/folder/get.go b/folder/get.go new file mode 100644 index 0000000..165e83f --- /dev/null +++ b/folder/get.go @@ -0,0 +1,52 @@ +package folder + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// FolderGetCmd Gets a Passbolt Folder +var FolderGetCmd = &cobra.Command{ + Use: "folder", + Short: "Gets a Passbolt Folder", + Long: `Gets a Passbolt Folder`, + RunE: FolderGet, +} + +func init() { + FolderGetCmd.Flags().String("id", "", "id of Folder to Get") + + FolderGetCmd.MarkFlagRequired("id") +} + +func FolderGet(cmd *cobra.Command, args []string) error { + id, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + folderParentID, name, err := helper.GetFolder( + ctx, + client, + id, + ) + if err != nil { + return fmt.Errorf("Getting Folder: %w", err) + } + fmt.Printf("FolderParentID: %v\n", folderParentID) + fmt.Printf("Name: %v\n", name) + return nil +} diff --git a/folder/list.go b/folder/list.go new file mode 100644 index 0000000..7da295c --- /dev/null +++ b/folder/list.go @@ -0,0 +1,86 @@ +package folder + +import ( + "context" + "fmt" + "strings" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/api" + "github.com/spf13/cobra" + + "github.com/pterm/pterm" +) + +// FolderListCmd Lists a Passbolt Folder +var FolderListCmd = &cobra.Command{ + Use: "folder", + Short: "Lists Passbolt Folders", + Long: `Lists Passbolt Folders`, + Aliases: []string{"folders"}, + RunE: FolderList, +} + +func init() { + FolderListCmd.Flags().StringP("search", "s", "", "Folders that have this in the name") + FolderListCmd.Flags().StringArrayP("group", "g", []string{}, "Folders that are shared with group") + FolderListCmd.Flags().StringArrayP("columns", "c", []string{"ID", "FolderParentID", "Name"}, "Columns to return, possible Columns:\nID, FolderParentID, Name") +} + +func FolderList(cmd *cobra.Command, args []string) error { + search, err := cmd.Flags().GetString("search") + if err != nil { + return err + } + parentFolders, err := cmd.Flags().GetStringArray("folder") + if err != nil { + return err + } + columns, err := cmd.Flags().GetStringArray("columns") + if err != nil { + return err + } + if len(columns) == 0 { + return fmt.Errorf("You need to Specify atleast one column to return") + } + + ctx := util.GetContext() + cmd.SilenceUsage = true + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + + folders, err := client.GetFolders(ctx, &api.GetFoldersOptions{ + FilterHasParent: parentFolders, + FilterSearch: search, + }) + if err != nil { + return fmt.Errorf("Listing Folder: %w", err) + } + + data := pterm.TableData{columns} + + for _, folder := range folders { + entry := make([]string, len(columns)) + for i := range columns { + switch strings.ToLower(columns[i]) { + case "id": + entry[i] = folder.ID + case "folderparentid": + entry[i] = folder.FolderParentID + case "name": + entry[i] = folder.Name + default: + cmd.SilenceUsage = false + return fmt.Errorf("Unknown Column: %v", columns[i]) + } + } + data = append(data, entry) + } + + pterm.DefaultTable.WithHasHeader().WithData(data).Render() + return nil +} diff --git a/folder/move.go b/folder/move.go new file mode 100644 index 0000000..65caa04 --- /dev/null +++ b/folder/move.go @@ -0,0 +1,57 @@ +package folder + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// FolderMoveCmd Moves a Passbolt Folder +var FolderMoveCmd = &cobra.Command{ + Use: "folder", + Short: "Moves a Passbolt Folder into a Folder", + Long: `Moves a Passbolt Folder into a Folder`, + RunE: FolderMove, +} + +func init() { + FolderMoveCmd.Flags().String("id", "", "id of Folder to Move") + FolderMoveCmd.Flags().StringP("folderParentID", "f", "", "Folder in which to Move the Folder") + + FolderMoveCmd.MarkFlagRequired("id") + FolderMoveCmd.MarkFlagRequired("folderParentID") +} + +func FolderMove(cmd *cobra.Command, args []string) error { + id, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + folderParentID, err := cmd.Flags().GetString("folderParentID") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + err = helper.MoveFolder( + ctx, + client, + id, + folderParentID, + ) + if err != nil { + return fmt.Errorf("Moving Folder: %w", err) + } + return nil +} diff --git a/folder/share.go b/folder/share.go new file mode 100644 index 0000000..f8b5243 --- /dev/null +++ b/folder/share.go @@ -0,0 +1,69 @@ +package folder + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// FolderShareCmd Shares a Passbolt Folder +var FolderShareCmd = &cobra.Command{ + Use: "folder", + Short: "Shares a Passbolt Folder", + Long: `Shares a Passbolt Folder`, + RunE: FolderShare, +} + +func init() { + FolderShareCmd.Flags().String("id", "", "id of Folder to Share") + FolderShareCmd.Flags().IntP("type", "t", 1, "Permission Type (1 Read Only, 7 Can Update, 15 Owner)") + FolderShareCmd.Flags().StringArrayP("users", "u", []string{}, "User id's to share with") + FolderShareCmd.Flags().StringArrayP("groups", "g", []string{}, "Group id's to share with") + + FolderShareCmd.MarkFlagRequired("id") + FolderShareCmd.MarkFlagRequired("type") +} + +func FolderShare(cmd *cobra.Command, args []string) error { + id, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + pType, err := cmd.Flags().GetInt("type") + if err != nil { + return err + } + users, err := cmd.Flags().GetStringArray("users") + if err != nil { + return err + } + groups, err := cmd.Flags().GetStringArray("groups") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + err = helper.ShareFolderWithUsersAndGroups( + ctx, + client, + id, + users, + groups, + pType, + ) + if err != nil { + return fmt.Errorf("Sharing Folder: %w", err) + } + return nil +} diff --git a/folder/update.go b/folder/update.go new file mode 100644 index 0000000..b365545 --- /dev/null +++ b/folder/update.go @@ -0,0 +1,57 @@ +package folder + +import ( + "context" + "fmt" + + "github.com/speatzle/go-passbolt-cli/util" + "github.com/speatzle/go-passbolt/helper" + "github.com/spf13/cobra" +) + +// FolderUpdateCmd Updates a Passbolt Folder +var FolderUpdateCmd = &cobra.Command{ + Use: "resource", + Short: "Updates a Passbolt Folder", + Long: `Updates a Passbolt Folder`, + RunE: FolderUpdate, +} + +func init() { + FolderUpdateCmd.Flags().String("id", "", "id of Folder to Update") + FolderUpdateCmd.Flags().StringP("name", "n", "", "Folder Name") + + FolderUpdateCmd.MarkFlagRequired("id") + FolderUpdateCmd.MarkFlagRequired("name") +} + +func FolderUpdate(cmd *cobra.Command, args []string) error { + id, err := cmd.Flags().GetString("id") + if err != nil { + return err + } + name, err := cmd.Flags().GetString("name") + if err != nil { + return err + } + + ctx := util.GetContext() + + client, err := util.GetClient(ctx) + if err != nil { + return err + } + defer client.Logout(context.TODO()) + cmd.SilenceUsage = true + + err = helper.UpdateFolder( + ctx, + client, + id, + name, + ) + if err != nil { + return fmt.Errorf("Updating Folder: %w", err) + } + return nil +}