Skip to content

Commit e8e262e

Browse files
committed
fix(cli): allow credential name before or after flags
Go's flag package stops parsing at the first non-flag argument, so 'sluice cred add myname --type oauth' silently ignored --type. Reorder args to move the positional name to the end before parsing. Both 'cred add name --flags' and 'cred add --flags name' now work.
1 parent 96fb5e3 commit e8e262e

1 file changed

Lines changed: 51 additions & 0 deletions

File tree

cmd/sluice/cred.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ func openVaultStore(dbPath string) (*vault.Store, error) {
119119
}
120120

121121
func handleCredAdd(args []string) error {
122+
// Reorder args so the positional name is always at the end.
123+
// Go's flag package stops at the first non-flag argument, so
124+
// "cred add myname --type oauth" silently ignores --type.
125+
// We move the name to the end: "cred add --type oauth myname".
126+
args = reorderPositionalLast(args)
127+
122128
fs := flag.NewFlagSet("cred add", flag.ContinueOnError)
123129
dbPath := fs.String("db", "sluice.db", "path to SQLite database")
124130
destination := fs.String("destination", "", "auto-create allow rule and binding for this destination")
@@ -529,3 +535,48 @@ func handleCredRemove(args []string) error {
529535
}
530536
return nil
531537
}
538+
539+
// reorderPositionalLast moves the first positional (non-flag) argument to the
540+
// end of the slice so Go's flag package sees all flags before stopping at the
541+
// positional. Flags and their values (e.g. "--db path") are kept in order.
542+
// This lets users write "cred add myname --type oauth" or
543+
// "cred add --type oauth myname" interchangeably.
544+
func reorderPositionalLast(args []string) []string {
545+
// Known flags that consume the next argument as a value.
546+
valueFlags := map[string]bool{
547+
"-db": true, "--db": true,
548+
"-destination": true, "--destination": true,
549+
"-ports": true, "--ports": true,
550+
"-header": true, "--header": true,
551+
"-template": true, "--template": true,
552+
"-type": true, "--type": true,
553+
"-token-url": true, "--token-url": true,
554+
}
555+
556+
var positional string
557+
var reordered []string
558+
skip := false
559+
for i, a := range args {
560+
if skip {
561+
skip = false
562+
reordered = append(reordered, a)
563+
continue
564+
}
565+
if strings.HasPrefix(a, "-") {
566+
reordered = append(reordered, a)
567+
if !strings.Contains(a, "=") && valueFlags[a] && i+1 < len(args) {
568+
skip = true
569+
}
570+
continue
571+
}
572+
if positional == "" {
573+
positional = a
574+
} else {
575+
reordered = append(reordered, a)
576+
}
577+
}
578+
if positional != "" {
579+
reordered = append(reordered, positional)
580+
}
581+
return reordered
582+
}

0 commit comments

Comments
 (0)