Files
f.weber ae30ba1240 feat(vault-tui): implement KV client and service for managing secrets
- Added internal/vault/client.go for creating a Vault client with configuration settings.
- Introduced internal/vault/errors.go to classify Vault API errors for better UI handling.
- Created internal/vault/kv.go to manage KV secrets, including listing, reading, writing, and deleting operations.
- Implemented internal/vault/mounts.go to list and describe secret engine mounts.
- Developed internal/vault/service.go to provide a unified entry point for Vault operations.
- Added internal/vault/kv_test.go for comprehensive testing of KV operations.
- Introduced internal/ui/toast.go for transient notifications in the UI.
- Added renovate.json for dependency management and updates.
2026-08-14 11:09:03 +02:00

67 lines
1.6 KiB
Go

package cli
import (
"context"
"fmt"
"os"
"github.com/urfave/cli/v3"
)
func readCommand() *cli.Command {
return &cli.Command{
Name: "read",
Usage: "read a secret (e.g. `vault-tui read secret/team/prod/db`)",
ArgsUsage: "<path>",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "json"},
&cli.IntFlag{Name: "version", Usage: "KV v2 version to read (default: latest)"},
&cli.StringFlag{Name: "field", Usage: "print only this field's raw value"},
},
Action: func(ctx context.Context, cmd *cli.Command) error {
a, err := appFrom(ctx)
if err != nil {
return err
}
if cmd.Args().Len() == 0 {
return fmt.Errorf("usage: vault-tui read <path>")
}
if _, err := a.EnsureLoggedIn(ctx, cmd.String("token")); err != nil {
return err
}
svc := a.Service()
mount, rel, err := splitMountPath(ctx, svc, cmd.Args().First())
if err != nil {
return err
}
sec, err := svc.KV.Read(ctx, mount, rel, int(cmd.Int("version")))
if err != nil {
return err
}
if sec == nil {
return fmt.Errorf("no secret found at %s", cmd.Args().First())
}
if field := cmd.String("field"); field != "" {
v, ok := sec.Data[field]
if !ok {
return fmt.Errorf("field %q not present", field)
}
fmt.Println(v)
return nil
}
if cmd.Bool("json") {
return printJSON(os.Stdout, sec)
}
out := map[string]string{}
for k, v := range sec.Data {
out[k] = fmt.Sprint(v)
}
printKV(os.Stdout, out)
if sec.Version > 0 {
fmt.Printf("\nversion: %d\n", sec.Version)
}
return nil
},
}
}