Skip to content

Commit

Permalink
Use MarkFlagsOneRequired to check for required flags
Browse files Browse the repository at this point in the history
  • Loading branch information
marevers committed Oct 13, 2023
1 parent 0609c09 commit 0c4e061
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 22 deletions.
3 changes: 2 additions & 1 deletion cmd/get-entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pleasant-cli get entry --path <path>
pleasant-cli get entry --id <id> --password
pleasant-cli get entry --path <path> --attachments`,
Run: func(cmd *cobra.Command, args []string) {
if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid(), pleasant.IsOneOfRequiredFlagsSet(cmd, "id", "path")) {
if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) {
return
}

Expand Down Expand Up @@ -98,6 +98,7 @@ func init() {
getEntryCmd.Flags().StringP("path", "p", "", "Path to entry")
getEntryCmd.Flags().StringP("id", "i", "", "Id of entry")
getEntryCmd.MarkFlagsMutuallyExclusive("path", "id")
getEntryCmd.MarkFlagsOneRequired("path", "id")

getEntryCmd.Flags().Bool("password", false, "Get the password of the entry")
getEntryCmd.Flags().Bool("attachments", false, "Gets the attachments of the entry")
Expand Down
3 changes: 2 additions & 1 deletion cmd/get-folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Examples:
pleasant-cli get folder --id <id>
pleasant-cli get folder --path <path>`,
Run: func(cmd *cobra.Command, args []string) {
if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid(), pleasant.IsOneOfRequiredFlagsSet(cmd, "id", "path")) {
if !pleasant.CheckPrerequisites(pleasant.IsServerUrlSet(), pleasant.IsTokenValid()) {
return
}

Expand Down Expand Up @@ -88,6 +88,7 @@ func init() {
getFolderCmd.Flags().StringP("path", "p", "", "Path to folder")
getFolderCmd.Flags().StringP("id", "i", "", "Id of folder")
getFolderCmd.MarkFlagsMutuallyExclusive("path", "id")
getFolderCmd.MarkFlagsOneRequired("path", "id")

getFolderCmd.Flags().Bool("useraccess", false, "Gets the users that have access to the folder")
}
20 changes: 0 additions & 20 deletions pleasant/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
"syscall"
"time"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/term"
"gopkg.in/yaml.v3"
Expand Down Expand Up @@ -73,25 +72,6 @@ func IsServerUrlSet() *prerequisite {
return pr
}

func IsOneOfRequiredFlagsSet(cmd *cobra.Command, flags ...string) *prerequisite {
var count int

for _, f := range flags {
if cmd.Flags().Changed(f) {
count++
}
}

b := count > 0

pr := &prerequisite{
Message: "At least one of the following flags must be set: " + strings.Join(flags, ", "),
PrerequisiteMet: b,
}

return pr
}

func StringPrompt(label string) string {
var s string

Expand Down

0 comments on commit 0c4e061

Please sign in to comment.