-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Raymond/hack week clipboard #19501
Raymond/hack week clipboard #19501
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added a few comments in case any are useful.
@@ -82,6 +82,10 @@ func (c *DeleteCommand) Run(args []string) int { | |||
return 1 | |||
} | |||
|
|||
if !validateClipboardFlag(c.BaseCommand) { | |||
return 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the places where we return early due to this, should we output something to the user (e.g. c.UI.Error(...)
)?
func validateClipboardFlag(c *BaseCommand) bool { | ||
if c.flagField == "" && c.flagClipboard { | ||
c.UI.Error("Field must be provided with the -clipboard flag") | ||
return false | ||
} | ||
|
||
if !c.flagClipboard && c.flagClipboardTTL > 0 { | ||
c.UI.Error("-clipboard flag must be set when using -clipboard-ttl") | ||
return false | ||
} | ||
|
||
return true | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be in a central/generic place as it's called by other commands?
Also, could you return the bool and an error and let the calling code deal with the output to the user?
func validateClipboardFlag(c *BaseCommand) (bool, error) {
var err error
if c.flagField == "" && c.flagClipboard {
err = multierror.Append(err, errors.New("Field must be provided with the -clipboard flag"))
}
if !c.flagClipboard && c.flagClipboardTTL > 0 {
err = multierror.Append(err, errors.New("-clipboard flag must be set when using -clipboard-ttl"))
}
return err == nil, err
}
So that you don't end up with every command having to parse the errors etc. maybe we could re-use/export the output func that Agent has to spit out multierrors?
https://github.com/hashicorp/vault/blob/main/command/agent.go#L1380-L1391
return PrintRaw(ui, str) | ||
} | ||
|
||
func writeClipboard(ui cli.Ui, str string, ttl time.Duration) int { | ||
if err := clipboard.WriteAll(str); err != nil { | ||
ui.Error(fmt.Sprintf("Error writing to clipboard: %s", err)) | ||
return 1 | ||
} | ||
ui.Info("Copied to clipboard!") | ||
if ttl.Seconds() > 0 { | ||
ui.Info(fmt.Sprintf("Clearing clipboard in %s..", ttl.String())) | ||
time.Sleep(ttl) | ||
_ = clipboard.WriteAll("") | ||
} | ||
|
||
return PrintRaw(ui, string(b)) | ||
return 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just wondering if there's a way to run this in a goroutine or something so it doesn't hog the cmd line?
Does it feel a bit nicer?
Could it allow users to script pulling and putting secrets in places (e.g. with pbpaste
in a piped command)?
|
||
- `-clipboard-ttl` `(duration: "", optional)` - When combined with `-clipboard`, | ||
the output will be saved to the user's clipboard until the specified TTL. After the | ||
TTL is up, the value is automatically removed from the clipboard. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it more correct to say:
the clipboard is automatically cleared.
@@ -32,6 +32,7 @@ require ( | |||
github.com/armon/go-metrics v0.4.1 | |||
github.com/armon/go-radix v1.0.0 | |||
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef | |||
github.com/atotto/clipboard v0.1.4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does any investigation need to happen into this package before it's brought into Vault (I have no idea so this is a curiosity question).
The repo says it supports:
Windows 7 (probably work on other Windows)
is this OK for our users as we do support running Vault on Windows (not just Windows 7).
Adds a clipboard feature to address #7556
Concerns:
init()
Move scary $PATH clipboard utility scan out of init() to as-needed atotto/clipboard#63