feat: lazysecrets open --action goes, copies or shows without asking

This commit is contained in:
keb committed 2026-10-14 21:24:01 +02:00
1 parent b05d19209c
commit a4002a785c
4 files changed
+65 -19

No files matched your search

+22 -10
View File
@@ -90,19 +90,29 @@ func cmpOr(a, b string) string {
}
// refArg parses get's and open's arguments: a reference, and optionally
// the account to use when several could (lazysecrets list's column).
func refArg(cmd string, args []string) (ref.Ref, error) {
// the account to use when several could (lazysecrets list's column) and,
// for open, what to do with it rather than asking (--action).
func refArg(cmd string, args []string) (r ref.Ref, action string, err error) {
fs := flag.NewFlagSet(cmd, flag.ContinueOnError)
account := fs.String("account", "", "the account to use when several could, as lazysecrets list prints it")
usage := "usage: lazysecrets get [--account <account>] <ref>"
act := new(string)
if cmd == "open" {
act = fs.String("action", "", "do this rather than ask: go (to it), copy (go, and copy its value) or show (go, and show it)")
usage = "usage: lazysecrets open [--account <account>] [--action go|copy|show] <ref>"
}
if err := fs.Parse(args); err != nil {
return ref.Ref{}, err
return r, "", err
}
if fs.NArg() != 1 {
return ref.Ref{}, fmt.Errorf("usage: lazysecrets %s [--account <account>] <ref>", cmd)
return r, "", errors.New(usage)
}
r, err := ref.Parse(fs.Arg(0))
if _, ok := app.OpenActions[*act]; *act != "" && !ok {
return r, "", fmt.Errorf("--action: %q is not go, copy or show", *act)
}
r, err = ref.Parse(fs.Arg(0))
r.Account = *account
return r, err
return r, *act, err
}
// defaultConfigPath is $LAZYSECRETS_CONFIG_FILE, else config.yaml in the
@@ -131,7 +141,7 @@ func run() error {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), `Usage:
lazysecrets [flags] browse secrets
lazysecrets [flags] [open [--account <account>]] <ref>
lazysecrets [flags] [open [--account <account>] [--action go|copy|show]] <ref>
open a lazysecrets:// link (also a share link),
Key Vault URL, @Microsoft.KeyVault(...) or
{{secrets/scope/key}}
@@ -168,6 +178,7 @@ Flags:
}
var open, getRef *ref.Ref
var openAction string
var shareArgs, listArgs []string
printKeys := false
switch args := flag.Args(); {
@@ -181,7 +192,7 @@ Flags:
case len(args) == 1 && args[0] == "keybindings":
printKeys = true
case len(args) > 0 && args[0] == "get":
r, err := refArg("get", args[1:])
r, _, err := refArg("get", args[1:])
if err != nil {
return err
}
@@ -194,11 +205,11 @@ Flags:
return updateCmd(*configPath, args[1:])
case len(args) == 0:
case args[0] == "open" && len(args) >= 2:
r, err := refArg("open", args[1:])
r, act, err := refArg("open", args[1:])
if err != nil {
return err
}
open = &r
open, openAction = &r, act
case len(args) == 1 && looksLikeRef(args[0]):
// lazysecrets <link>, like lazysecrets open <link>
r, err := ref.Parse(args[0])
@@ -464,6 +475,7 @@ Flags:
ShareKey: cfg.Sharing.Key,
ShareRules: shareRules(cfg),
Open: open,
OpenAction: openAction,
Styles: &theme,
DefaultFg: defaultFg,
})