1
0
Fork 0

Improved CLI

master
Ambrose Chua 2017-07-17 17:31:25 +08:00
parent fcf752121d
commit 517929c364
2 changed files with 101 additions and 10 deletions

View File

@ -3,8 +3,6 @@ package pw
//go:generate go run words_generate.go //go:generate go run words_generate.go
import ( import (
"log"
"strings" "strings"
"crypto/rand" "crypto/rand"
"math/big" "math/big"
@ -37,16 +35,20 @@ func NewGenerator(m Mode, s Strength) Generator {
} }
} }
func (g Generator) Next() string { func (g Generator) Next() (string, error) {
n := int(g.Strength) n := int(g.Strength)
s := []string{} s := []string{}
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
s = append(s, g.GenerateWord()); w, err := g.GenerateWord()
if err != nil {
return "", err
}
s = append(s, w)
} }
return strings.Join(s, " ") return strings.Join(s, " "), nil
} }
func (g Generator) GenerateWord() string { func (g Generator) GenerateWord() (string, error) {
var wordsList []string var wordsList []string
if g.Mode == ModeShort { if g.Mode == ModeShort {
wordsList = WordsShort wordsList = WordsShort
@ -58,7 +60,7 @@ func (g Generator) GenerateWord() string {
c := len(wordsList) c := len(wordsList)
i, err := rand.Int(rand.Reader, big.NewInt(int64(c))) i, err := rand.Int(rand.Reader, big.NewInt(int64(c)))
if err != nil { if err != nil {
log.Fatal(err) return "", err
} }
return wordsList[i.Int64()] return wordsList[i.Int64()], nil
} }

View File

@ -2,11 +2,100 @@ package main
import ( import (
"fmt" "fmt"
"os"
"strconv"
"github.com/serverwentdown/pword/pw" "github.com/serverwentdown/pword/pw"
"github.com/urfave/cli"
) )
func main() { func main() {
g := pw.NewGenerator(pw.ModeShort, pw.StrengthOnline) app := cli.NewApp()
fmt.Println(g.Next())
app.Name = "pword"
app.Usage = "generate secure passwords"
app.Version = "0.1.0"
app.Flags = []cli.Flag {
cli.StringFlag{
Name: "count, c",
Value: "auto",
Usage: "Generates `NUM` passwords for you to choose from",
},
cli.BoolFlag{
Name: "1",
Usage: "Equivalent to --count 1",
},
cli.BoolFlag{
Name: "stronger",
Usage: "Chooses from a list of 7,776 words instead",
},
}
app.Commands = []cli.Command{
{
Name: "online",
Usage: "Generates passwords for use on websites",
Action: func(c *cli.Context) error {
return generate(c, pw.StrengthOnline)
},
},
{
Name: "offline",
Usage: "Generates passwords for use offline (laptops, encrypted drives)",
Action: func(c *cli.Context) error {
return generate(c, pw.StrengthOffline)
},
},
{
Name: "crypto",
Usage: "Generates extremely secure passwords",
Action: func(c *cli.Context) error {
return generate(c, pw.StrengthCrypto)
},
},
{
Name: "recall",
Usage: "Utility with autocomplete to help you recall passwords",
Action: func(c *cli.Context) error {
return recall(c)
},
},
}
app.Run(os.Args)
}
func generate(c *cli.Context, strength pw.Strength) error {
count := int(strength)
countString := c.GlobalString("count")
countParsed, err := strconv.ParseUint(countString, 10, 64)
if err == nil {
count = int(countParsed)
}
if c.GlobalBool("1") {
count = 1
}
mode := pw.ModeShort
if c.GlobalBool("stronger") {
mode = pw.ModeLong
}
g := pw.NewGenerator(mode, strength)
for i := 0; i < count; i++ {
pw, err := g.Next()
if err != nil {
return err
}
fmt.Println(pw)
}
return nil
}
func recall(c *cli.Context) error {
return nil
} }