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

Commit 777a826

Browse files
committed
Replace generate --include and --exclude flags with --charset flag
1 parent 804b6e9 commit 777a826

1 file changed

Lines changed: 27 additions & 29 deletions

File tree

internals/secrethub/generate.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ type GenerateSecretCommand struct {
4242
firstArg string
4343
secondArg string
4444
lengthArg intValue
45-
includes []string
46-
excludes []string
45+
charsetFlag charsetValue
4746
mins []string
4847
copyToClipboard bool
4948
clearClipboardAfter time.Duration
@@ -67,11 +66,9 @@ func (cmd *GenerateSecretCommand) Register(r command.Registerer) {
6766
clause.HelpLong("By default, it uses numbers (0-9), lowercase letters (a-z) and uppercase letters (A-Z) and a length of 22.")
6867
clause.Arg("secret-path", "The path to write the generated secret to").Required().PlaceHolder(secretPathPlaceHolder).StringVar(&cmd.firstArg)
6968
clause.Flag("length", "The length of the generated secret. Defaults to "+strconv.Itoa(defaultLength)).PlaceHolder(strconv.Itoa(defaultLength)).Short('l').SetValue(&cmd.lengthFlag)
70-
clause.Flag("include", "Include given characters in the set of characters to randomly choose a password from.").StringsVar(&cmd.includes)
71-
clause.Flag("exclude", "Ensure the password does not contain any characters from the given character set.").StringsVar(&cmd.excludes)
7269
clause.Flag("min", "<charset>:<n> Ensure that the resulting password contains at least n characters from the given character set.").StringsVar(&cmd.mins)
7370
clause.Flag("clip", "Copy the generated value to the clipboard. The clipboard is automatically cleared after "+units.HumanDuration(cmd.clearClipboardAfter)+".").Short('c').BoolVar(&cmd.copyToClipboard)
74-
71+
clause.Flag("charset", "Charset(s) to use when generating secret.").SetValue(&cmd.charsetFlag)
7572
clause.Flag("symbols", "Include symbols in secret.").Short('s').Hidden().SetValue(&cmd.symbolsFlag)
7673
clause.Arg("rand-command", "").Hidden().StringVar(&cmd.secondArg)
7774
clause.Arg("length", "").Hidden().SetValue(&cmd.lengthArg)
@@ -86,34 +83,11 @@ func (cmd *GenerateSecretCommand) before() error {
8683
return err
8784
}
8885

89-
charset := randchar.Alphanumeric
86+
charset := cmd.charsetFlag.charset
9087
if useSymbols {
9188
charset = charset.Add(randchar.Symbols)
9289
}
9390

94-
includedCharsets := make([]randchar.Charset, 0)
95-
for _, charsetName := range cmd.includes {
96-
charsetToInclude, found := randchar.CharsetByName(charsetName)
97-
if !found {
98-
return ErrCouldNotFindCharSet(charsetName)
99-
}
100-
charset = charset.Add(charsetToInclude)
101-
includedCharsets = append(includedCharsets, charsetToInclude)
102-
}
103-
104-
for _, charsetName := range cmd.excludes {
105-
charsetToExclude, found := randchar.CharsetByName(charsetName)
106-
if !found {
107-
return ErrCouldNotFindCharSet(charsetName)
108-
}
109-
charset = charset.Subtract(charsetToExclude)
110-
for i, includedCharset := range includedCharsets {
111-
if charsetToExclude.Equals(includedCharset) {
112-
return ErrFlagsMutuallyExclusive(cmd.includes[i], charsetName)
113-
}
114-
}
115-
}
116-
11791
options := make([]randchar.Option, 0)
11892
for _, minFlag := range cmd.mins {
11993
charset, count, err := parseMinFlag(minFlag)
@@ -259,6 +233,30 @@ func (cmd *GenerateSecretCommand) useSymbols() (bool, error) {
259233
return false, nil
260234
}
261235

236+
type charsetValue struct {
237+
charset randchar.Charset
238+
}
239+
240+
func (c *charsetValue) String() string {
241+
return ""
242+
}
243+
244+
func (c *charsetValue) Set(flagValue string) error {
245+
charsetNames := strings.Split(flagValue, ",")
246+
for _, charsetName := range charsetNames {
247+
charset, ok := randchar.CharsetByName(charsetName)
248+
if !ok {
249+
return ErrCouldNotFindCharSet(charsetName)
250+
}
251+
c.charset = c.charset.Add(charset)
252+
}
253+
return nil
254+
}
255+
256+
func (c *charsetValue) IsCumulative() bool {
257+
return true
258+
}
259+
262260
type intValue struct {
263261
v *int
264262
}

0 commit comments

Comments
 (0)