package cli import ( "context" "fmt" "os" "github.com/urfave/cli/v3" "git.morlana.online/f.weber/vault-tui/internal/config" ) func configCommand() *cli.Command { return &cli.Command{ Name: "config", Usage: "manage the vault-tui config file", Commands: []*cli.Command{ { Name: "init", Usage: "interactively create a new config file", Action: func(ctx context.Context, cmd *cli.Command) error { a, err := appFrom(ctx) if err != nil { return err } if config.Exists(a.ConfigPath) && !askOverwrite(a.ConfigPath) { return nil } if !IsInteractive() { return fmt.Errorf("config init requires an interactive terminal") } _, err = runWizard(a.ConfigPath) return err }, }, { Name: "path", Usage: "print the resolved config file path", Action: func(ctx context.Context, cmd *cli.Command) error { a, err := appFrom(ctx) if err != nil { return err } fmt.Println(a.ConfigPath) return nil }, }, { Name: "show", Usage: "print the fully resolved settings for the active profile", Flags: []cli.Flag{&cli.BoolFlag{Name: "json"}}, Action: func(ctx context.Context, cmd *cli.Command) error { a, err := appFrom(ctx) if err != nil { return err } if cmd.Bool("json") { return printJSON(os.Stdout, a.Settings) } s := a.Settings out := map[string]string{ "profile": s.Profile, "address": s.Address, "namespace": s.Namespace, "read_only": fmt.Sprint(s.ReadOnly), "auth.method": s.Auth.Method, "auth.mount": s.Auth.Mount, "tls.ca_cert": s.CACert, "tls.skip_verify": fmt.Sprint(s.SkipVerify), } printKV(os.Stdout, out) return nil }, }, }, } } func askOverwrite(path string) bool { fmt.Fprintf(os.Stderr, "%s already exists. Overwrite? (y/N): ", path) var resp string fmt.Fscanln(os.Stdin, &resp) return resp == "y" || resp == "yes" }