mirror of
https://github.com/passbolt/go-passbolt-cli.git
synced 2025-05-12 02:58:20 +00:00
add resource commands
This commit is contained in:
parent
f952b51065
commit
b0303103da
14 changed files with 521 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/speatzle/go-passbolt-cli/resource"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
@ -14,4 +15,5 @@ var createCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
rootCmd.AddCommand(createCmd)
|
||||
createCmd.AddCommand(resource.ResourceCreateCmd)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/speatzle/go-passbolt-cli/resource"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
@ -14,6 +15,7 @@ var deleteCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
rootCmd.AddCommand(deleteCmd)
|
||||
deleteCmd.AddCommand(resource.ResourceDeleteCmd)
|
||||
|
||||
deleteCmd.PersistentFlags().String("id", "", "ID of the Entity to Delete")
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/speatzle/go-passbolt-cli/resource"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
@ -14,5 +15,5 @@ var getCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
rootCmd.AddCommand(getCmd)
|
||||
|
||||
getCmd.AddCommand(resource.ResourceGetCmd)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/speatzle/go-passbolt-cli/resource"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
@ -15,4 +16,5 @@ var listCmd = &cobra.Command{
|
|||
func init() {
|
||||
rootCmd.AddCommand(listCmd)
|
||||
|
||||
listCmd.AddCommand(resource.ResourceListCmd)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/speatzle/go-passbolt-cli/resource"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
@ -12,4 +13,6 @@ var moveCmd = &cobra.Command{
|
|||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(moveCmd)
|
||||
moveCmd.AddCommand(resource.ResourceMoveCmd)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/speatzle/go-passbolt-cli/resource"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
@ -13,4 +14,5 @@ var shareCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
rootCmd.AddCommand(shareCmd)
|
||||
shareCmd.AddCommand(resource.ResourceShareCmd)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/speatzle/go-passbolt-cli/resource"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
|
@ -14,4 +15,6 @@ var updateCmd = &cobra.Command{
|
|||
|
||||
func init() {
|
||||
rootCmd.AddCommand(updateCmd)
|
||||
|
||||
updateCmd.AddCommand(resource.ResourceUpdateCmd)
|
||||
}
|
||||
|
|
83
resource/create.go
Normal file
83
resource/create.go
Normal file
|
@ -0,0 +1,83 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/speatzle/go-passbolt-cli/util"
|
||||
"github.com/speatzle/go-passbolt/helper"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ResourceCreateCmd Creates a Passbolt Resource
|
||||
var ResourceCreateCmd = &cobra.Command{
|
||||
Use: "resource",
|
||||
Short: "Creates a Passbolt Resource",
|
||||
Long: `Creates a Passbolt Resource and Returns the Resources ID`,
|
||||
RunE: ResourceCreate,
|
||||
}
|
||||
|
||||
func init() {
|
||||
ResourceCreateCmd.Flags().StringP("name", "n", "", "Resource Name")
|
||||
ResourceCreateCmd.Flags().StringP("username", "u", "", "Resource Username")
|
||||
ResourceCreateCmd.Flags().String("uri", "", "Resource URI")
|
||||
ResourceCreateCmd.Flags().StringP("password", "p", "", "Resource Password")
|
||||
ResourceCreateCmd.Flags().StringP("description", "d", "", "Resource Description")
|
||||
ResourceCreateCmd.Flags().StringP("folderParentID", "f", "", "Folder in which to create the Resource")
|
||||
|
||||
ResourceCreateCmd.MarkFlagRequired("name")
|
||||
ResourceCreateCmd.MarkFlagRequired("password")
|
||||
}
|
||||
|
||||
func ResourceCreate(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
|
||||
}
|
||||
username, err := cmd.Flags().GetString("username")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uri, err := cmd.Flags().GetString("uri")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
password, err := cmd.Flags().GetString("password")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
description, err := cmd.Flags().GetString("description")
|
||||
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.CreateResource(
|
||||
ctx,
|
||||
client,
|
||||
folderParentID,
|
||||
name,
|
||||
username,
|
||||
uri,
|
||||
password,
|
||||
description,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Creating Resource: %w", err)
|
||||
}
|
||||
|
||||
fmt.Printf("ResourceID: %v\n", id)
|
||||
return nil
|
||||
}
|
43
resource/delete.go
Normal file
43
resource/delete.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/speatzle/go-passbolt-cli/util"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ResourceDeleteCmd Deletes a Resource
|
||||
var ResourceDeleteCmd = &cobra.Command{
|
||||
Use: "resource",
|
||||
Short: "Deletes a Passbolt Resource",
|
||||
Long: `Deletes a Passbolt Resource`,
|
||||
RunE: ResourceDelete,
|
||||
}
|
||||
|
||||
func ResourceDelete(cmd *cobra.Command, args []string) error {
|
||||
resourceID, err := cmd.Flags().GetString("id")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resourceID == "" {
|
||||
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.DeleteResource(ctx, resourceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Deleting Resource: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
56
resource/get.go
Normal file
56
resource/get.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/speatzle/go-passbolt-cli/util"
|
||||
"github.com/speatzle/go-passbolt/helper"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ResourceGetCmd Gets a Passbolt Resource
|
||||
var ResourceGetCmd = &cobra.Command{
|
||||
Use: "resource",
|
||||
Short: "Gets a Passbolt Resource",
|
||||
Long: `Gets a Passbolt Resource`,
|
||||
RunE: ResourceGet,
|
||||
}
|
||||
|
||||
func init() {
|
||||
ResourceGetCmd.Flags().String("id", "", "id of Resource to Get")
|
||||
|
||||
ResourceGetCmd.MarkFlagRequired("id")
|
||||
}
|
||||
|
||||
func ResourceGet(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, username, uri, password, description, err := helper.GetResource(
|
||||
ctx,
|
||||
client,
|
||||
id,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Getting Resource: %w", err)
|
||||
}
|
||||
fmt.Printf("FolderParentID: %v\n", folderParentID)
|
||||
fmt.Printf("Name: %v\n", name)
|
||||
fmt.Printf("Username: %v\n", username)
|
||||
fmt.Printf("URI: %v\n", uri)
|
||||
fmt.Printf("Password: %v\n", password)
|
||||
fmt.Printf("Description: %v\n", description)
|
||||
return nil
|
||||
}
|
117
resource/list.go
Normal file
117
resource/list.go
Normal file
|
@ -0,0 +1,117 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/speatzle/go-passbolt-cli/util"
|
||||
"github.com/speatzle/go-passbolt/api"
|
||||
"github.com/speatzle/go-passbolt/helper"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/pterm/pterm"
|
||||
)
|
||||
|
||||
// ResourceListCmd Lists a Passbolt Resource
|
||||
var ResourceListCmd = &cobra.Command{
|
||||
Use: "resource",
|
||||
Short: "Lists Passbolt Resources",
|
||||
Long: `Lists Passbolt Resources`,
|
||||
Aliases: []string{"resources"},
|
||||
RunE: ResourceList,
|
||||
}
|
||||
|
||||
func init() {
|
||||
ResourceListCmd.Flags().Bool("favorite", false, "Resources that are maked as favorite")
|
||||
ResourceListCmd.Flags().Bool("own", false, "Resources that are owned by me")
|
||||
|
||||
ResourceListCmd.Flags().StringArrayP("group", "g", []string{}, "Resources that are shared with group")
|
||||
ResourceListCmd.Flags().StringArrayP("folder", "f", []string{}, "Resources that are in folder")
|
||||
|
||||
ResourceListCmd.Flags().StringArrayP("columns", "c", []string{"ID", "FolderParentID", "Name", "Username", "URI"}, "Columns to return, possible Columns:\nID, FolderParentID, Name, Username, URI, Password, Description")
|
||||
}
|
||||
|
||||
func ResourceList(cmd *cobra.Command, args []string) error {
|
||||
favorite, err := cmd.Flags().GetBool("favorite")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
own, err := cmd.Flags().GetBool("own")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
groups, err := cmd.Flags().GetStringArray("group")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
folderParents, 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()
|
||||
|
||||
client, err := util.GetClient(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer client.Logout(context.TODO())
|
||||
cmd.SilenceUsage = true
|
||||
|
||||
resources, err := client.GetResources(ctx, &api.GetResourcesOptions{
|
||||
FilterIsFavorite: favorite,
|
||||
FilterIsOwnedByMe: own,
|
||||
FilterIsSharedWithGroup: groups,
|
||||
FilterHasParent: folderParents,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("Listing Resource: %w", err)
|
||||
}
|
||||
|
||||
data := pterm.TableData{columns}
|
||||
|
||||
for _, resource := range resources {
|
||||
entry := make([]string, len(columns))
|
||||
for i := range columns {
|
||||
switch strings.ToLower(columns[i]) {
|
||||
case "id":
|
||||
entry[i] = resource.ID
|
||||
case "folderparentid":
|
||||
entry[i] = resource.FolderParentID
|
||||
case "name":
|
||||
entry[i] = resource.Name
|
||||
case "username":
|
||||
entry[i] = resource.Username
|
||||
case "uri":
|
||||
entry[i] = resource.URI
|
||||
case "password":
|
||||
_, _, _, _, pass, _, err := helper.GetResource(ctx, client, resource.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Get Resource %w", err)
|
||||
}
|
||||
entry[i] = pass
|
||||
case "description":
|
||||
_, _, _, _, _, desc, err := helper.GetResource(ctx, client, resource.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Get Resource %w", err)
|
||||
}
|
||||
entry[i] = desc
|
||||
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
resource/move.go
Normal file
57
resource/move.go
Normal file
|
@ -0,0 +1,57 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/speatzle/go-passbolt-cli/util"
|
||||
"github.com/speatzle/go-passbolt/helper"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ResourceMoveCmd Moves a Passbolt Resource
|
||||
var ResourceMoveCmd = &cobra.Command{
|
||||
Use: "resource",
|
||||
Short: "Moves a Passbolt Resource into a Folder",
|
||||
Long: `Moves a Passbolt Resource into a Folder`,
|
||||
RunE: ResourceMove,
|
||||
}
|
||||
|
||||
func init() {
|
||||
ResourceMoveCmd.Flags().String("id", "", "id of Resource to Move")
|
||||
ResourceMoveCmd.Flags().StringP("folderParentID", "f", "", "Folder in which to Move the Resource")
|
||||
|
||||
ResourceMoveCmd.MarkFlagRequired("id")
|
||||
ResourceMoveCmd.MarkFlagRequired("folderParentID")
|
||||
}
|
||||
|
||||
func ResourceMove(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.MoveResource(
|
||||
ctx,
|
||||
client,
|
||||
id,
|
||||
folderParentID,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Moving Resource: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
69
resource/share.go
Normal file
69
resource/share.go
Normal file
|
@ -0,0 +1,69 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/speatzle/go-passbolt-cli/util"
|
||||
"github.com/speatzle/go-passbolt/helper"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ResourceShareCmd Shares a Passbolt Resource
|
||||
var ResourceShareCmd = &cobra.Command{
|
||||
Use: "resource",
|
||||
Short: "Shares a Passbolt Resource",
|
||||
Long: `Shares a Passbolt Resource`,
|
||||
RunE: ResourceShare,
|
||||
}
|
||||
|
||||
func init() {
|
||||
ResourceShareCmd.Flags().String("id", "", "id of Resource to Share")
|
||||
ResourceShareCmd.Flags().IntP("type", "t", 1, "Permission Type (1 Read Only, 7 Can Update, 15 Owner)")
|
||||
ResourceShareCmd.Flags().StringArrayP("users", "u", []string{}, "User id's to share with")
|
||||
ResourceShareCmd.Flags().StringArrayP("groups", "g", []string{}, "Group id's to share with")
|
||||
|
||||
ResourceShareCmd.MarkFlagRequired("id")
|
||||
ResourceShareCmd.MarkFlagRequired("type")
|
||||
}
|
||||
|
||||
func ResourceShare(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.ShareResourceWithUsersAndGroups(
|
||||
ctx,
|
||||
client,
|
||||
id,
|
||||
users,
|
||||
groups,
|
||||
pType,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Sharing Resource: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
80
resource/update.go
Normal file
80
resource/update.go
Normal file
|
@ -0,0 +1,80 @@
|
|||
package resource
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/speatzle/go-passbolt-cli/util"
|
||||
"github.com/speatzle/go-passbolt/helper"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// ResourceUpdateCmd Updates a Passbolt Resource
|
||||
var ResourceUpdateCmd = &cobra.Command{
|
||||
Use: "resource",
|
||||
Short: "Updates a Passbolt Resource",
|
||||
Long: `Updates a Passbolt Resource`,
|
||||
RunE: ResourceUpdate,
|
||||
}
|
||||
|
||||
func init() {
|
||||
ResourceUpdateCmd.Flags().String("id", "", "id of Resource to Update")
|
||||
ResourceUpdateCmd.Flags().StringP("name", "n", "", "Resource Name")
|
||||
ResourceUpdateCmd.Flags().StringP("username", "u", "", "Resource Username")
|
||||
ResourceUpdateCmd.Flags().String("uri", "", "Resource URI")
|
||||
ResourceUpdateCmd.Flags().StringP("password", "p", "", "Resource Password")
|
||||
ResourceUpdateCmd.Flags().StringP("description", "d", "", "Resource Description")
|
||||
|
||||
ResourceUpdateCmd.MarkFlagRequired("id")
|
||||
}
|
||||
|
||||
func ResourceUpdate(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
|
||||
}
|
||||
username, err := cmd.Flags().GetString("username")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
uri, err := cmd.Flags().GetString("uri")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
password, err := cmd.Flags().GetString("password")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
description, err := cmd.Flags().GetString("description")
|
||||
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.UpdateResource(
|
||||
ctx,
|
||||
client,
|
||||
id,
|
||||
name,
|
||||
username,
|
||||
uri,
|
||||
password,
|
||||
description,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("Updating Resource: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Add table
Reference in a new issue