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

Commit ad83279

Browse files
committed
Refactor generate --min flag parsing
1 parent 777a826 commit ad83279

1 file changed

Lines changed: 35 additions & 35 deletions

File tree

internals/secrethub/generate.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type GenerateSecretCommand struct {
4343
secondArg string
4444
lengthArg intValue
4545
charsetFlag charsetValue
46-
mins []string
46+
mins optionsValue
4747
copyToClipboard bool
4848
clearClipboardAfter time.Duration
4949
clipper clip.Clipper
@@ -66,7 +66,7 @@ func (cmd *GenerateSecretCommand) Register(r command.Registerer) {
6666
clause.HelpLong("By default, it uses numbers (0-9), lowercase letters (a-z) and uppercase letters (A-Z) and a length of 22.")
6767
clause.Arg("secret-path", "The path to write the generated secret to").Required().PlaceHolder(secretPathPlaceHolder).StringVar(&cmd.firstArg)
6868
clause.Flag("length", "The length of the generated secret. Defaults to "+strconv.Itoa(defaultLength)).PlaceHolder(strconv.Itoa(defaultLength)).Short('l').SetValue(&cmd.lengthFlag)
69-
clause.Flag("min", "<charset>:<n> Ensure that the resulting password contains at least n characters from the given character set.").StringsVar(&cmd.mins)
69+
clause.Flag("min", "<charset>:<n> Ensure that the resulting password contains at least n characters from the given character set.").SetValue(&cmd.mins)
7070
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)
7171
clause.Flag("charset", "Charset(s) to use when generating secret.").SetValue(&cmd.charsetFlag)
7272
clause.Flag("symbols", "Include symbols in secret.").Short('s').Hidden().SetValue(&cmd.symbolsFlag)
@@ -88,20 +88,11 @@ func (cmd *GenerateSecretCommand) before() error {
8888
charset = charset.Add(randchar.Symbols)
8989
}
9090

91-
options := make([]randchar.Option, 0)
92-
for _, minFlag := range cmd.mins {
93-
charset, count, err := parseMinFlag(minFlag)
94-
if err != nil {
95-
return err
96-
}
97-
options = append(options, randchar.Min(count, charset))
98-
}
99-
10091
if charset.Size() <= 0 {
10192
return ErrCharsetSizeNonPositive
10293
}
10394

104-
cmd.generator, err = randchar.NewRand(charset, options...)
95+
cmd.generator, err = randchar.NewRand(charset, cmd.mins.options...)
10596
if err != nil {
10697
return err
10798
}
@@ -167,29 +158,6 @@ func (cmd *GenerateSecretCommand) run() error {
167158
return nil
168159
}
169160

170-
// parseMinFlag takes a min flag value of the following format:
171-
// <charset>:<count>
172-
// and returns the charset and count specified in the flag or error
173-
// if the flag has an invalid format.
174-
func parseMinFlag(flag string) (randchar.Charset, int, error) {
175-
elements := strings.Split(flag, ":")
176-
if len(elements) != 2 {
177-
return randchar.Charset{}, 0, ErrInvalidMinFlag(flag)
178-
}
179-
180-
count, err := strconv.Atoi(elements[1])
181-
if err != nil {
182-
return randchar.Charset{}, 0, ErrMinFlagInvalidInteger(elements[1])
183-
}
184-
185-
charset, found := randchar.CharsetByName(elements[0])
186-
if !found {
187-
return randchar.Charset{}, 0, ErrCouldNotFindCharSet(elements[0])
188-
}
189-
190-
return charset, count, nil
191-
}
192-
193161
func (cmd *GenerateSecretCommand) length() (int, error) {
194162
if cmd.lengthArg.IsSet() && cmd.lengthFlag.IsSet() {
195163
return 0, ErrCannotUseLengthArgAndFlag
@@ -233,6 +201,38 @@ func (cmd *GenerateSecretCommand) useSymbols() (bool, error) {
233201
return false, nil
234202
}
235203

204+
type optionsValue struct {
205+
options []randchar.Option
206+
}
207+
208+
func (v *optionsValue) String() string {
209+
return ""
210+
}
211+
212+
func (v *optionsValue) Set(flagValue string) error {
213+
elements := strings.Split(flagValue, ":")
214+
if len(elements) != 2 {
215+
return ErrInvalidMinFlag(flagValue)
216+
}
217+
218+
count, err := strconv.Atoi(elements[1])
219+
if err != nil {
220+
return ErrMinFlagInvalidInteger(elements[1])
221+
}
222+
223+
charset, found := randchar.CharsetByName(elements[0])
224+
if !found {
225+
return ErrCouldNotFindCharSet(elements[0])
226+
}
227+
228+
v.options = append(v.options, randchar.Min(count, charset))
229+
return nil
230+
}
231+
232+
func (v *optionsValue) IsCumulative() bool {
233+
return true
234+
}
235+
236236
type charsetValue struct {
237237
charset randchar.Charset
238238
}

0 commit comments

Comments
 (0)