diff options
author | Michaël Ball <michael.ball@gmail.com> | 2017-01-07 18:07:51 +0000 |
---|---|---|
committer | Michaël Ball <michael.ball@gmail.com> | 2017-01-07 18:07:51 +0000 |
commit | b57ee0ffc26098560202c751de9142c88d80bcc6 (patch) | |
tree | 5ed722cfc8d9a262821e5c14cc687b7fd6f6b5ab | |
parent | 6068ab484fa377917514753becf61d2211460218 (diff) | |
parent | 7e0c7250e1a9afd9930fbb200c5cbd9a8095404e (diff) |
Merge branch 'release/0.2.0'0.2.0
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | CHANGELOG | 6 | ||||
-rw-r--r-- | generator/generator.go | 45 | ||||
-rw-r--r-- | generator/generator_test.go | 15 | ||||
-rw-r--r-- | passphrasegen.go | 35 |
5 files changed, 71 insertions, 31 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8ee6649 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +passphrasegen
\ No newline at end of file diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..630c2c1 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,6 @@ +0.2.0 +Add tests, refactor code + +0.1.0 + +Initial release.
\ No newline at end of file diff --git a/generator/generator.go b/generator/generator.go new file mode 100644 index 0000000..3ec5154 --- /dev/null +++ b/generator/generator.go @@ -0,0 +1,45 @@ +package generator + +import ( + "bufio" + "math/rand" + "os" + "time" +) + +func readLines(path string) ([]string, error) { + file, err := os.Open(path) + if err != nil { + return nil, err + } + defer file.Close() + + var lines []string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + lines = append(lines, scanner.Text()) + } + return lines, scanner.Err() +} + +// RandomWords picks numberOfWords random words from the file located at +// wordsFile +func RandomWords(wordsFile string, numberOfWords int) ([]string, error) { + rand.Seed(time.Now().UTC().UnixNano()) + + words, err := readLines(wordsFile) + + if err != nil { + return nil, err + } + + phraseWords := make([]string, numberOfWords) + + for index := 0; index < numberOfWords; index++ { + randInt := rand.Intn((len(words) - 1)) + + phraseWords[index] = words[randInt] + } + + return phraseWords, nil +} diff --git a/generator/generator_test.go b/generator/generator_test.go new file mode 100644 index 0000000..4cda593 --- /dev/null +++ b/generator/generator_test.go @@ -0,0 +1,15 @@ +package generator + +import "testing" + +func TestRandomWords(t *testing.T) { + words, err := RandomWords("/usr/share/dict/words", 4) + + if err != nil { + t.Errorf("%s", err) + } + + if len(words) != 4 { + t.Errorf("RandomWords does not return the correct number of words") + } +} diff --git a/passphrasegen.go b/passphrasegen.go index 32d708e..89b25d2 100644 --- a/passphrasegen.go +++ b/passphrasegen.go @@ -1,39 +1,20 @@ package main import ( - "bufio" "flag" "fmt" - "math/rand" - "os" "strings" - "time" + + generator "github.com/michael-ball/passphrasegen/generator" ) var numWords = flag.Int("w", 4, "Number of words in passphrase") -func readLines(path string) ([]string, error) { - file, err := os.Open(path) - if err != nil { - return nil, err - } - defer file.Close() - - var lines []string - scanner := bufio.NewScanner(file) - for scanner.Scan() { - lines = append(lines, scanner.Text()) - } - return lines, scanner.Err() -} - func init() { - rand.Seed(time.Now().UTC().UnixNano()) + flag.Parse() } func main() { - flag.Parse() - args := flag.Args() wordsFile := "/usr/share/dict/words" @@ -42,20 +23,12 @@ func main() { wordsFile = args[0] } - words, err := readLines(wordsFile) + phraseWords, err := generator.RandomWords(wordsFile, *numWords) if err != nil { fmt.Println(err) return } - phraseWords := make([]string, *numWords) - - for index := 0; index < *numWords; index++ { - randInt := rand.Intn((len(words) - 1)) - - phraseWords[index] = words[randInt] - } - fmt.Println(strings.Join(phraseWords, " ")) } |