feat: lazysecrets list prints the account, and open and get take it (--account)

This commit is contained in:
keb committed 2026-10-14 21:24:01 +02:00
1 parent 1f330bc619
commit b05d19209c
9 files changed
+118 -43

No files matched your search

+7 -2
View File
@@ -37,10 +37,15 @@ func value(ctx context.Context, providers []provider.Provider, st *store.Store,
return v, err
}
// readKey is value, also returning the version read.
// readKey is value, also returning the version read. The account asked
// for (--account) goes first, else the one that last read it.
func readKey(ctx context.Context, providers []provider.Provider, st *store.Store, r ref.Ref) (provider.SecretValue, string, error) {
key := r
key.Version = ""
node := st.Node(key.String())
return ref.Read(ctx, providers, r, node.Account, node.Source)
account, source := node.Account, node.Source
if r.Account != "" && r.Account != account {
account, source = r.Account, ""
}
return ref.Read(ctx, providers, r, account, source)
}
+8 -8
View File
@@ -26,11 +26,11 @@ import (
// listCmd prints every source, vault and secret lazysecrets can find, one
// per line, for fzf, editor pickers and scripts:
//
// lazysecrets list | fzf --ansi --with-nth='2..-2' --accept-nth=-1
// lazysecrets list | fzf --ansi --with-nth='2..-3' --accept-nth=-1
//
// Each line is the kind, the name and the link, tab-separated (the kind
// a word, the link without spaces, so even fields split on spaces keep
// them first and last): the name coloured from the theme,
// Each line is the kind, the name, the account and the link,
// tab-separated (all but the name one word each, so even fields split on
// spaces keep them first and last): the name coloured from the theme,
// with the provider's icon (as in the TUI: --icons, LAZYSECRETS_ICONS,
// ui.icons); the link never is.
//
@@ -42,7 +42,7 @@ func listCmd(providers []provider.Provider, cachePath string, timeout func(provi
fs := flag.NewFlagSet("list", flag.ContinueOnError)
cached := fs.Bool("cached", false, "only what's cached, and repos: load nothing from Azure or Databricks")
maxAge := fs.Duration("max-age", 2*time.Minute, "load cached lists older than this again")
format := fs.String("format", "text", "text: kind, name and link, tab-separated; json: an object per line")
format := fs.String("format", "text", "text: kind, name, account and link, tab-separated; json: an object per line")
color := fs.String("color", "always", "colour the names (text): always or never; fzf needs --ansi")
iconsOn := fs.Bool("icons", withIcons, "put the provider's Nerd Font icon before names (text)")
kinds := fs.String("kind", "", "only these kinds, comma-separated: "+strings.Join(listing.Kinds, ", ")+" (vault: also scopes and repo files)")
@@ -75,7 +75,7 @@ func listCmd(providers []provider.Provider, cachePath string, timeout func(provi
case "text":
write = func(e listing.Entry) error {
// A tab or line break in a name would break the line up.
_, err := fmt.Fprintf(out, "%s\t%s\t%s\n", e.Kind, oneLine.Replace(nameOf(e, theme, iconSet, *color == "always")), e.Link)
_, err := fmt.Fprintf(out, "%s\t%s\t%s\t%s\n", e.Kind, oneLine.Replace(nameOf(e, theme, iconSet, *color == "always")), e.Account, e.Link)
return err
}
case "json":
@@ -150,8 +150,8 @@ func nameOf(e listing.Entry, theme styles.Styles, iconSet icons.Set, color bool)
}
name += e.Path[n-1]
}
if e.Account != "" {
name += render(theme.Muted, " · "+e.Account)
if e.AccountLabel != "" {
name += render(theme.Muted, " · "+e.AccountLabel)
}
if e.Error != "" {
name += " " + render(theme.Error, "✗ "+e.Error)
+1 -1
View File
@@ -10,7 +10,7 @@ import (
)
func TestListNames(t *testing.T) {
e := listing.Entry{Provider: "azure", Path: []string{"platform-prod", "kv-app-prod", "db-password"}, Account: "alice"}
e := listing.Entry{Provider: "azure", Path: []string{"platform-prod", "kv-app-prod", "db-password"}, AccountLabel: "alice"}
if got := nameOf(e, styles.Default(), icons.None, false); got != "platform-prod / kv-app-prod / db-password · alice" {
t.Errorf("plain: %q", got)
}
+23 -6
View File
@@ -89,6 +89,22 @@ func cmpOr(a, b string) string {
return b
}
// 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) {
fs := flag.NewFlagSet(cmd, flag.ContinueOnError)
account := fs.String("account", "", "the account to use when several could, as lazysecrets list prints it")
if err := fs.Parse(args); err != nil {
return ref.Ref{}, err
}
if fs.NArg() != 1 {
return ref.Ref{}, fmt.Errorf("usage: lazysecrets %s [--account <account>] <ref>", cmd)
}
r, err := ref.Parse(fs.Arg(0))
r.Account = *account
return r, err
}
// defaultConfigPath is $LAZYSECRETS_CONFIG_FILE, else config.yaml in the
// config directory.
func defaultConfigPath() string {
@@ -115,11 +131,12 @@ func run() error {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), `Usage:
lazysecrets [flags] browse secrets
lazysecrets [flags] [open] <ref>
lazysecrets [flags] [open [--account <account>]] <ref>
open a lazysecrets:// link (also a share link),
Key Vault URL, @Microsoft.KeyVault(...) or
{{secrets/scope/key}}
lazysecrets [flags] get <ref> print the value a lazysecrets:// link points at,
lazysecrets [flags] get [--account <account>] <ref>
print the value a lazysecrets:// link points at,
for scripts: VAR=$(lazysecrets get <ref>)
lazysecrets [flags] list [--kind secret,…] [--cached] [--max-age 2m] [--format text|json]
[--color always|never] [--icons=false] [--errors=false]
@@ -163,8 +180,8 @@ Flags:
listArgs = append([]string{}, args[1:]...) // not nil: listing
case len(args) == 1 && args[0] == "keybindings":
printKeys = true
case len(args) == 2 && args[0] == "get":
r, err := ref.Parse(args[1])
case len(args) > 0 && args[0] == "get":
r, err := refArg("get", args[1:])
if err != nil {
return err
}
@@ -176,8 +193,8 @@ Flags:
case len(args) > 0 && args[0] == "update":
return updateCmd(*configPath, args[1:])
case len(args) == 0:
case args[0] == "open" && len(args) == 2:
r, err := ref.Parse(args[1])
case args[0] == "open" && len(args) >= 2:
r, err := refArg("open", args[1:])
if err != nil {
return err
}