forked from gopasspw/gopass
/
generate.go
74 lines (59 loc) · 1.6 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package action
import (
"fmt"
"strconv"
"github.com/fatih/color"
"github.com/justwatchcom/gopass/pwgen"
"github.com/urfave/cli"
)
const (
defaultLength = 24
)
// Generate & save a password
func (s *Action) Generate(c *cli.Context) error {
force := c.Bool("force")
noSymbols := c.Bool("no-symbols")
name := c.Args().Get(0)
length := c.Args().Get(1)
if name == "" {
var err error
name, err = askForString("Which name do you want to use?", "")
if err != nil || name == "" {
return fmt.Errorf(color.RedString("provide a password name"))
}
}
replacing, err := s.Store.Exists(name)
if err != nil {
return fmt.Errorf("failed to see if %s exists: %s", name, err)
}
if !force { // don't check if it's force anyway
if replacing && !askForConfirmation(fmt.Sprintf("An entry already exists for %s. Overwrite it?", name)) {
return fmt.Errorf("not overwriting your current password")
}
}
if length == "" {
length = strconv.Itoa(defaultLength)
if l, err := askForInt("How long should the password be?", defaultLength); err == nil {
length = strconv.Itoa(l)
}
}
pwlen, err := strconv.Atoi(length)
if err != nil {
return fmt.Errorf("password length must be a number")
}
if pwlen < 1 {
return fmt.Errorf("password length must be bigger than 0")
}
password := pwgen.GeneratePassword(pwlen, !noSymbols)
if err := s.Store.SetConfirm(name, password, s.confirmRecipients); err != nil {
return err
}
if c.Bool("clip") {
return s.copyToClipboard(name, password)
}
fmt.Printf(
"The generated password for %s is:\n%s\n", name,
color.YellowString(string(password)),
)
return nil
}