Skip to content

Commit 1a00575

Browse files
safe option implemented
1 parent de0ec40 commit 1a00575

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

ci/release_notes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
* `safe uuid` is a new command that will generate a UUIDv4 and insert it into
44
the specified path at the Vault. (thanks @gerardocorea)
5+
* `safe option` allows you to view and edit new safe CLI global options.
6+
Currently, the only option is `manage_vault_token`, which will have safe
7+
change the .vault-token file that the Vault CLI uses. (thanks @daviddob)
58

69
# Improvements
710

main.go

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"time"
2828

2929
"github.com/cloudfoundry-community/vaultkv"
30+
"github.com/jhunt/go-ansi"
3031
fmt "github.com/jhunt/go-ansi"
3132
"github.com/jhunt/go-cli"
3233
env "github.com/jhunt/go-envirotron"
@@ -259,9 +260,8 @@ type Options struct {
259260
DataOnly bool `cli:"--data-only"`
260261
} `cli:"curl"`
261262

262-
UUID struct {
263-
UUID string
264-
} `cli:"uuid"`
263+
UUID struct{} `cli:"uuid"`
264+
Option struct{} `cli:"option"`
265265

266266
X509 struct {
267267
Validate struct {
@@ -2882,6 +2882,82 @@ The following options are recognized:
28822882
return nil
28832883
})
28842884

2885+
r.Dispatch("option", &Help{
2886+
Summary: "View or edit global safe CLI options",
2887+
Usage: "safe option [optionname=value]",
2888+
Type: AdministrativeCommand,
2889+
Description: `
2890+
Currently available options are:
2891+
2892+
@G{manage_vault_token} If set to true, then when logging in or switching targets,
2893+
the '.vault-token' file in your $HOME directory that the Vault CLI uses will be
2894+
updated.
2895+
`,
2896+
}, func(command string, args ...string) error {
2897+
cfg := rc.Apply(opt.UseTarget)
2898+
2899+
optLookup := []struct {
2900+
opt string
2901+
val *bool
2902+
}{
2903+
{"manage_vault_token", &cfg.Options.ManageVaultToken},
2904+
}
2905+
2906+
if len(args) == 0 {
2907+
table := table{}
2908+
for _, entry := range optLookup {
2909+
value := "@R{false}"
2910+
if *entry.val {
2911+
value = "@G{true}"
2912+
}
2913+
table.addRow(entry.opt, ansi.Sprintf(value))
2914+
}
2915+
2916+
table.print()
2917+
return nil
2918+
}
2919+
2920+
for _, arg := range args {
2921+
argSplit := strings.Split(arg, "=")
2922+
if len(argSplit) != 2 {
2923+
return fmt.Errorf("Option arg syntax: option=value")
2924+
}
2925+
2926+
parseTrueFalse := func(s string) (bool, error) {
2927+
switch s {
2928+
case "true", "on", "yes":
2929+
return true, nil
2930+
case "false", "off", "no":
2931+
return false, nil
2932+
}
2933+
2934+
return false, fmt.Errorf("value must be one of true|on|yes|false|off|no")
2935+
}
2936+
2937+
optionKey := strings.ReplaceAll(argSplit[0], "-", "_")
2938+
optionVal, err := parseTrueFalse(argSplit[1])
2939+
if err != nil {
2940+
return err
2941+
}
2942+
2943+
found := false
2944+
for _, opt := range optLookup {
2945+
if opt.opt == optionKey {
2946+
found = true
2947+
*opt.val = optionVal
2948+
ansi.Printf("updated @G{%s}\n", opt.opt)
2949+
break
2950+
}
2951+
}
2952+
2953+
if !found {
2954+
return fmt.Errorf("unknown option: %s", argSplit[0])
2955+
}
2956+
}
2957+
2958+
return cfg.Write()
2959+
})
2960+
28852961
r.Dispatch("ssh", &Help{
28862962
Summary: "Generate one or more new SSH RSA keypair(s)",
28872963
Usage: "safe ssh [NBITS] PATH [PATH ...]",

0 commit comments

Comments
 (0)