add folder commands

This commit is contained in:
Samuel Lorch 2021-09-08 11:12:34 +02:00
parent b0303103da
commit 1b129332ae
12 changed files with 432 additions and 2 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

58
folder/create.go Normal file
View file

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

43
folder/delete.go Normal file
View file

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

52
folder/get.go Normal file
View file

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

86
folder/list.go Normal file
View file

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

57
folder/move.go Normal file
View file

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

69
folder/share.go Normal file
View file

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

57
folder/update.go Normal file
View file

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