Skip to content

Commit

Permalink
Add user access assignment deletion/archival to delete entry and dele…
Browse files Browse the repository at this point in the history
…te folder commands
  • Loading branch information
marevers committed Mar 13, 2024
1 parent 6e39da6 commit 120b4da
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ Available Commands:
completion Generate the autocompletion script for the specified shell
config Interact with pleasant-cli configuration
create Creates entries or folders
get Gets entries, folders or access levels
delete Archives or deletes entries or folders or user access assignments for them
get Gets entries, folders, access levels, server info or password strength
help Help about any command
login Log in to Pleasant Password Server
search Search for entries and folders matching a query
Expand Down
27 changes: 23 additions & 4 deletions cmd/delete-entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,19 @@ import (
// deleteEntryCmd represents the entry command
var deleteEntryCmd = &cobra.Command{
Use: "entry",
Short: "Archives or deletes an entry",
Short: "Archives or deletes an entry or a user access assignment for it",
Long: `Archives or deletes an entry from the Pleasant Password tree by its id or path.
A path must be absolute and starts with 'Root/', e.g. 'Root/Folder1/Folder2/Entry'.
Instead of the entry, a user access assignment can also be archived or deleted by appending --useraccess <accessrowid>.
By default, the entry is archived. If it should be deleted, use --delete.
By default, the entry/user access assignment is archived. If it should be deleted, use --delete.
WARNING: Deletion is permanent, use at your own risk.
Examples:
pleasant-cli delete entry --id <id>
pleasant-cli delete entry --path <path>
pleasant-cli delete entry --id <id> --delete`,
pleasant-cli delete entry --id <id> --delete
pleasant-cli delete entry --id <id> --delete --useraccess <accessrowid>`,
Run: func(cmd *cobra.Command, args []string) {
if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) {
return
Expand Down Expand Up @@ -83,13 +85,29 @@ pleasant-cli delete entry --id <id> --delete`,

subPath := pleasant.PathEntry + "/" + identifier

var msg string

if cmd.Flags().Changed("useraccess") {
ua, err := cmd.Flags().GetString("useraccess")
if err != nil {
fmt.Println(err)
return
}

subPath = subPath + "/useraccess/" + ua

msg = fmt.Sprintf("User access assignment %v deleted from entry %v", ua, identifier)
} else {
msg = fmt.Sprintf("Entry with id %v archived/deleted", identifier)
}

_, err := pleasant.DeleteJsonString(baseUrl, subPath, json, bearerToken)
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Entry with id", identifier, "archived/deleted")
fmt.Println(msg)
},
}

Expand All @@ -101,5 +119,6 @@ func init() {
deleteEntryCmd.MarkFlagsMutuallyExclusive("path", "id")
deleteEntryCmd.MarkFlagsOneRequired("path", "id")

deleteEntryCmd.Flags().String("useraccess", "", "Archives/deletes the user access assignment with this id")
deleteEntryCmd.Flags().Bool("delete", false, "Deletes the entry instead of archiving")
}
25 changes: 22 additions & 3 deletions cmd/delete-folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,20 @@ import (
// deleteFolderCmd represents the entry command
var deleteFolderCmd = &cobra.Command{
Use: "folder",
Short: "Archives or deletes a folder",
Short: "Archives or deletes a folder or a user access assignment for it",
Long: `Archives or deletes a folder from the Pleasant Password tree by its id or path.
Anything contained in the folder is also archived/deleted.
A path must be absolute and starts with 'Root/', e.g. 'Root/Folder1/Folder2/Folder3'.
Instead of the folder, a user access assignment can also be archived or deleted by appending --useraccess <accessrowid>.
By default, the folder is archived. If it should be deleted, use --delete.
WARNING: Deletion is permanent, use at your own risk.
Examples:
pleasant-cli delete folder --id <id>
pleasant-cli delete folder --path <path>
pleasant-cli delete folder --id <id> --delete`,
pleasant-cli delete folder --id <id> --delete
pleasant-cli delete folder --id <id> --delete --useraccess <accessrowid>`,
Run: func(cmd *cobra.Command, args []string) {
if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) {
return
Expand Down Expand Up @@ -84,13 +86,29 @@ pleasant-cli delete folder --id <id> --delete`,

subPath := pleasant.PathFolders + "/" + identifier

var msg string

if cmd.Flags().Changed("useraccess") {
ua, err := cmd.Flags().GetString("useraccess")
if err != nil {
fmt.Println(err)
return
}

subPath = subPath + "/useraccess/" + ua

msg = fmt.Sprintf("User access assignment %v deleted from folder %v", ua, identifier)
} else {
msg = fmt.Sprintf("Folder with id %v archived/deleted", identifier)
}

_, err := pleasant.DeleteJsonString(baseUrl, subPath, json, bearerToken)
if err != nil {
fmt.Println(err)
return
}

fmt.Println("Folder with id", identifier, "archived/deleted")
fmt.Println(msg)
},
}

Expand All @@ -102,5 +120,6 @@ func init() {
deleteFolderCmd.MarkFlagsMutuallyExclusive("path", "id")
deleteFolderCmd.MarkFlagsOneRequired("path", "id")

deleteFolderCmd.Flags().String("useraccess", "", "Archives/deletes the user access assignment with this id")
deleteFolderCmd.Flags().Bool("delete", false, "Deletes the folder instead of archiving")
}
4 changes: 2 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
// deleteCmd represents the create command
var deleteCmd = &cobra.Command{
Use: "delete",
Short: "Archives or deletes entries or folders",
Long: `Archives or deletes entries or folders`,
Short: "Archives or deletes entries or folders or user access assignments for them",
Long: `Archives or deletes entries or folders or user access assignments for them`,
Args: cobra.MatchAll(cobra.MinimumNArgs(1)),
Run: func(cmd *cobra.Command, args []string) {
},
Expand Down
2 changes: 1 addition & 1 deletion pleasant/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
ErrAmbiguousResult = errors.New("error: ambiguous result, multiple matching entries or folders")
ErrDuplicateEntry = errors.New("error: duplicate entry found, skipping creation")
ErrDuplicateFolder = errors.New("error: duplicate folder found, skipping creation")
ErrArchiveNotEnabled = errors.New("error: archiving is possibly disabled or entry/folder does not exist")
ErrArchiveNotEnabled = errors.New("error: entry/folder/accessrowid does not exist or archiving is possibly disabled")
)

func generateError(statusCode int) error {
Expand Down

0 comments on commit 120b4da

Please sign in to comment.