Skip to content
This repository was archived by the owner on Feb 16, 2023. It is now read-only.

Commit 192fd0b

Browse files
committed
Refactor: rename clearClipboardAfter and don't pass it to clip writer
It's not just the default value, it's non-configurable for the user. Because we're using the same value everywhere, there isn't really a point in having it be configurable. What's more: we're using the value of the constant in some texts that are displayed to the user, assuming that this "default" is the value being used. I believe it's clearer if we reflect this assumption in the name of the constant.
1 parent d0cecb4 commit 192fd0b

5 files changed

Lines changed: 9 additions & 14 deletions

File tree

internals/secrethub/clear_clipboard.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"golang.org/x/crypto/bcrypt"
1212
)
1313

14-
// defaultClearClipboardAfter defines the default TTL for data written to the clipboard.
15-
const defaultClearClipboardAfter = 45 * time.Second
14+
// clearClipboardAfter defines the TTL for data written to the clipboard.
15+
const clearClipboardAfter = 45 * time.Second
1616

1717
// ClearClipboardCommand is a command to clear the contents of the clipboard after some time passed.
1818
type ClearClipboardCommand struct {
@@ -67,7 +67,6 @@ type ClipboardWriter interface {
6767
}
6868

6969
type ClipboardWriterAutoClear struct {
70-
timeout time.Duration
7170
clipper clip.Clipper
7271
}
7372

@@ -85,7 +84,7 @@ func (clipWriter *ClipboardWriterAutoClear) Write(data []byte) error {
8584

8685
err = cloneproc.Spawn(
8786
"clipboard-clear", hex.EncodeToString(hash),
88-
"--timeout", clipWriter.timeout.String())
87+
"--timeout", clearClipboardAfter.String())
8988

9089
return err
9190
}

internals/secrethub/generate.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ func NewGenerateSecretCommand(io ui.IO, newClient newClientFunc) *GenerateSecret
5555
newClient: newClient,
5656
clipWriter: &ClipboardWriterAutoClear{
5757
clipper: clip.NewClipboard(),
58-
timeout: defaultClearClipboardAfter,
5958
},
6059
}
6160
}
@@ -66,7 +65,7 @@ func (cmd *GenerateSecretCommand) Register(r cli.Registerer) {
6665
clause.Flags().VarP(&cmd.lengthFlag, "length", "l", "The length of the generated secret.")
6766
clause.Cmd.Flag("length").DefValue = strconv.Itoa(defaultLength)
6867
clause.Flags().Var(&cmd.mins, "min", "<charset>:<n> Ensure that the resulting password contains at least n characters from the given character set. Note that adding constraints reduces the strength of the secret. When possible, avoid any constraints.")
69-
clause.Flags().BoolVarP(&cmd.copyToClipboard, "clip", "c", false, "Copy the generated value to the clipboard. The clipboard is automatically cleared after "+units.HumanDuration(defaultClearClipboardAfter)+".")
68+
clause.Flags().BoolVarP(&cmd.copyToClipboard, "clip", "c", false, "Copy the generated value to the clipboard. The clipboard is automatically cleared after "+units.HumanDuration(clearClipboardAfter)+".")
7069
_ = cmd.charsetFlag.Set("alphanumeric")
7170
clause.Flags().Var(&cmd.charsetFlag, "charset", "Define the set of characters to randomly generate a password from. Options are all, alphanumeric, numeric, lowercase, uppercase, letters, symbols and human-readable. Multiple character sets can be combined by supplying them in a comma separated list.")
7271
clause.Cmd.Flag("charset").DefValue = "alphanumeric"
@@ -155,7 +154,7 @@ func (cmd *GenerateSecretCommand) run() error {
155154
fmt.Fprintf(
156155
cmd.io.Output(),
157156
"The generated value has been copied to the clipboard. It will be cleared after %s.\n",
158-
units.HumanDuration(defaultClearClipboardAfter),
157+
units.HumanDuration(clearClipboardAfter),
159158
)
160159
}
161160

internals/secrethub/inject.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ func NewInjectCommand(io ui.IO, newClient newClientFunc) *InjectCommand {
4343
return &InjectCommand{
4444
clipWriter: &ClipboardWriterAutoClear{
4545
clipper: clip.NewClipboard(),
46-
timeout: defaultClearClipboardAfter,
4746
},
4847
osEnv: os.Environ(),
4948
io: io,
@@ -61,7 +60,7 @@ func (cmd *InjectCommand) Register(r cli.Registerer) {
6160
"clip", "c", false,
6261
fmt.Sprintf(
6362
"Copy the injected template to the clipboard instead of stdout. The clipboard is automatically cleared after %s.",
64-
units.HumanDuration(defaultClearClipboardAfter),
63+
units.HumanDuration(clearClipboardAfter),
6564
))
6665
clause.Flags().StringVarP(&cmd.inFile, "in-file", "i", "", "The filename of a template file to inject.")
6766
clause.Flags().StringVarP(&cmd.outFile, "out-file", "o", "", "Write the injected template to a file instead of stdout.")
@@ -136,7 +135,7 @@ func (cmd *InjectCommand) Run() error {
136135
return err
137136
}
138137

139-
_, err = fmt.Fprintf(cmd.io.Output(), "Copied injected template to clipboard. It will be cleared after %s.\n", units.HumanDuration(defaultClearClipboardAfter))
138+
_, err = fmt.Fprintf(cmd.io.Output(), "Copied injected template to clipboard. It will be cleared after %s.\n", units.HumanDuration(clearClipboardAfter))
140139
if err != nil {
141140
return err
142141
}

internals/secrethub/read.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ func NewReadCommand(io ui.IO, newClient newClientFunc) *ReadCommand {
3434
return &ReadCommand{
3535
clipWriter: &ClipboardWriterAutoClear{
3636
clipper: clip.NewClipboard(),
37-
timeout: defaultClearClipboardAfter,
3837
},
3938
io: io,
4039
newClient: newClient,
@@ -51,7 +50,7 @@ func (cmd *ReadCommand) Register(r cli.Registerer) {
5150
"clip", "c", false,
5251
fmt.Sprintf(
5352
"Copy the secret value to the clipboard. The clipboard is automatically cleared after %s.",
54-
units.HumanDuration(defaultClearClipboardAfter),
53+
units.HumanDuration(clearClipboardAfter),
5554
),
5655
)
5756
clause.Flags().StringVarP(&cmd.outFile, "out-file", "o", "", "Write the secret value to this file.")
@@ -84,7 +83,7 @@ func (cmd *ReadCommand) Run() error {
8483
cmd.io.Output(),
8584
"Copied %s to clipboard. It will be cleared after %s.\n",
8685
cmd.path,
87-
units.HumanDuration(defaultClearClipboardAfter),
86+
units.HumanDuration(clearClipboardAfter),
8887
)
8988
}
9089

internals/secrethub/service_init.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ func NewServiceInitCommand(io ui.IO, newClient newClientFunc) *ServiceInitComman
3737
return &ServiceInitCommand{
3838
clipWriter: &ClipboardWriterAutoClear{
3939
clipper: clip.NewClipboard(),
40-
timeout: defaultClearClipboardAfter,
4140
},
4241
io: io,
4342
newClient: newClient,

0 commit comments

Comments
 (0)