Skip to content

Commit

Permalink
small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
sylviamoss committed Dec 12, 2020
1 parent 4b1b2df commit adf3d63
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
1 change: 0 additions & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ builds:
- -trimpath #removes all file system paths from the compiled executable
goos:
- freebsd
- windows
- linux
- darwin
goarch:
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GOARCH=$(shell go env GOARCH)

build:
@go build -o ~/go/bin/diceware .
@go build -o ~/go/bin/diceware-cli .

build_windows:
@env GOOS=windows go build . \
Expand Down
2 changes: 1 addition & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var (
Long: `Generates strong passwords based on easily memorable passwords that are
also extremely resistant to attack.`,
Run: func(cmd *cobra.Command, args []string) {
if err := diceware.Generate(generateConfig, wordsBox); err != nil {
if err := generateConfig.Generate(wordsBox); err != nil {
fmt.Printf("Ops...something went wrong: %s", err.Error())
}
},
Expand Down
8 changes: 4 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import (
)

var rootCmd = &cobra.Command{
Use: "diceware",
Short: "A generator of diceware passwords.",
Long: `diceware-cli let's you generate strong passwords based on easily memorable passwords that are
Use: "diceware-cli",
Short: "A generator of strong passwords using diceware passphrase..",
Long: `diceware-cli let's you generate strong passwords based on easily memorable passphrases that are
also extremely resistant to attack.`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Use command 'generate' to start generating your strong passwords! Or 'help' for more instructions.")
fmt.Println("Use command 'generate' to start generating your strong passwords, or 'help' for instructions.")
},
}

Expand Down
16 changes: 8 additions & 8 deletions diceware/diceware.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,37 @@ type GenerateConfig struct {

var wordsBox packr.Box

func Generate(generateConfig GenerateConfig, box packr.Box) error {
func (c *GenerateConfig) Generate(box packr.Box) error {
wordsBox = box

separator := generateConfig.Separator
separator := c.Separator
if separator == "none" {
separator = ""
}

var words string
for i := 1; i <= int(generateConfig.Size); i++ {
for i := 1; i <= int(c.Size); i++ {
index, err := findDicewareWordIndex()
if err != nil {
return err
}
word, err := findDicewareWord(index, generateConfig.Lang)
word, err := findDicewareWord(index, c.Lang)
if err != nil {
return err
}
words = words + word + separator
}
words = words[:len(words)-len(separator)]

if generateConfig.Pbcopy || generateConfig.Hide {
if c.Pbcopy || c.Hide {
cmd := fmt.Sprintf("echo %s | pbcopy", words)
if err := exec.Command("sh", "-c", cmd).Run(); err != nil {
return fmt.Errorf("error copying passphrase: %s", err.Error())
}
fmt.Println("Password copied!")
}

if generateConfig.Hide {
if c.Hide {
return nil
}

Expand Down Expand Up @@ -99,14 +99,14 @@ func findDicewareWord(number string, lang string) (string, error) {
if err != nil {
word, err = findCustomDicewareWord(wordPath)
if err != nil {
return "", fmt.Errorf("unable to find word for index %s. err: %s", number, err.Error())
return "", fmt.Errorf("unable to find word for index %q. err: %s", number, err.Error())
}
}

t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC)
transformedWord, _, err := transform.String(t, word)
if err != nil {
return "", fmt.Errorf("unable to remove special characters from %s. err: %s", word, err.Error())
return "", fmt.Errorf("unable to remove special characters from %q. err: %s", word, err.Error())
}
return transformedWord, nil
}
Expand Down

0 comments on commit adf3d63

Please sign in to comment.