Skip to content

Commit

Permalink
fix custom config read and rename config dir from 'diceware' to 'dice…
Browse files Browse the repository at this point in the history
…ware-cli.d'
  • Loading branch information
sylviamoss committed Mar 14, 2021
1 parent a2a3d4c commit 807a3c0
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 29 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
diceware-cli
pkg/
.DS_Store
.vscode/
.vscode/
es_config.txt
7 changes: 1 addition & 6 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
# Make sure to check the documentation at http://goreleaser.com
env:
- CGO_ENABLED=0
before:
hooks:
- go mod download
- go test ./...

builds:
-
mod_timestamp: '{{ .CommitTimestamp }}'
flags:
- -trimpath
ldflags:
- '-s -w -X cmd.Version={{.Version}} -X cmd.VersionPrerelease= '
- '-s -w -X github.com/sylviamoss/diceware-cli/cmd.Version={{.Version}} -X github.com/sylviamoss/diceware-cli/cmd.VersionPrerelease= '
goos:
- freebsd
- linux
Expand Down
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@ Usage:
Flags:
--copy pbcopy password
-h, --help help for generate
--hide pbcopy and hide password. Password WON'T be printed out
--hide pbcopy and hide password. You WON'T see the password
--lang string password language
available langs: en, pt (default "en")
--lower remove capitalized first letters
--remove-number removes the random number we add by default
--separator string character that separates the words.
use --separator=none to remove reparator (default "/")
--size int32 the amount words the password will have (default 6)
```
```

Examples:

Generate a Portuguese passphrase and copy it automatically:
```
~> diceware-cli generate --lang=pt --copy
Pungir/Bip4/Quorum/Vau/Vida/Censor
```

Generate an English (default) passphrase with seven words, backslash separator, and copy it automatically:
```
~> diceware-cli generate --copy --size=7 --separator=\\
Unashamed2\Sublime\Rejoin\Justly\Audition\Glove\Cahoots
```


### Add custom language dictionary
Expand All @@ -38,11 +52,18 @@ Flags:
--source string dictionary source file
```

Example:
Example of adding an Spanish diceware dictionary:
```
diceware config --add-lang --source=/Users/diceware-cli/dictionary_file.txt --name=es
```
diceware-cli config --add-lang --source=/Users/diceware-cli/dictionary_file.txt --name=es
```

The `dictionary_file.txt` content must be in the same format as this [example of word list](https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt).
The custom dictionary configuration will be ketp under `$HOME/.diceware-cli.d/diceware_words_{language}`

To further generate Spanish diceware passphrase, you'd do:
```
~> diceware-cli generate --lang=es
```

## Installation Guide

Expand Down
4 changes: 2 additions & 2 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

var (
generateConfig diceware.GenerateConfig
dicewareConfig diceware.Config

generateCmd = &cobra.Command{
Use: "generate",
Short: "Generates a diceware password with custom configuration.",
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 := generateConfig.Generate(); err != nil {
if err := dicewareConfig.Generate(); err != nil {
fmt.Printf("Ops...something went wrong: %s", err.Error())
}
},
Expand Down
14 changes: 7 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ var (
func Execute() {
rootCmd.AddCommand(versionCmd)

generateCmd.Flags().StringVar(&generateConfig.Lang, "lang", "en", "password language\n available langs: en, pt")
generateCmd.Flags().StringVar(&generateConfig.Separator, "separator", "/", "character that separates the words.\nuse --separator=none to remove reparator")
generateCmd.Flags().Int32Var(&generateConfig.Size, "size", 6, "the amount words the password will have")
generateCmd.Flags().BoolVar(&generateConfig.Pbcopy, "copy", false, "pbcopy password")
generateCmd.Flags().BoolVar(&generateConfig.Hide, "hide", false, "pbcopy and hide password. Password WON'T be printed out")
generateCmd.Flags().BoolVar(&generateConfig.Lower, "lower", false, "remove capitalized first letters")
generateCmd.Flags().BoolVar(&generateConfig.RemoveNumber, "remove-number", false, "removes the random number we add by default")
generateCmd.Flags().StringVar(&dicewareConfig.Lang, "lang", "en", "password language\n available langs: en, pt")
generateCmd.Flags().StringVar(&dicewareConfig.Separator, "separator", "/", "character that separates the words.\nuse --separator=none to remove reparator")
generateCmd.Flags().Int32Var(&dicewareConfig.Size, "size", 6, "the amount words the password will have")
generateCmd.Flags().BoolVar(&dicewareConfig.Pbcopy, "copy", false, "pbcopy password")
generateCmd.Flags().BoolVar(&dicewareConfig.Hide, "hide", false, "pbcopy and hide password. You WON'T see the password")
generateCmd.Flags().BoolVar(&dicewareConfig.Lower, "lower", false, "remove capitalized first letters")
generateCmd.Flags().BoolVar(&dicewareConfig.RemoveNumber, "remove-number", false, "removes the random number we add by default")
rootCmd.AddCommand(generateCmd)

configCmd.Flags().BoolVar(&customConfig.AddLang, "add-lang", false, "add new config language")
Expand Down
2 changes: 1 addition & 1 deletion diceware/custom_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (c *CustomConfig) newLanguage() error {
return err
}

dicewarePath := home + "/.diceware/diceware_words_" + c.Name
dicewarePath := home + "/.diceware-cli.d/diceware_words_" + c.Name
if _, err := os.Stat(dicewarePath); os.IsNotExist(err) {
err = os.MkdirAll(dicewarePath, os.ModePerm)
}
Expand Down
13 changes: 6 additions & 7 deletions diceware/diceware.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"golang.org/x/text/unicode/norm"
)

type GenerateConfig struct {
type Config struct {
Lang string
Size int32
Pbcopy bool
Expand All @@ -31,7 +31,7 @@ type GenerateConfig struct {
//go:embed words
var words embed.FS

func (c *GenerateConfig) Generate() error {
func (c *Config) Generate() error {
separator := c.Separator
if separator == "none" {
separator = ""
Expand Down Expand Up @@ -69,7 +69,6 @@ func (c *GenerateConfig) Generate() error {
if err := exec.Command("sh", "-c", cmd).Run(); err != nil {
return fmt.Errorf("error copying passphrase: %s", err.Error())
}
fmt.Println("Password copied!")
}

if c.Hide {
Expand Down Expand Up @@ -109,12 +108,12 @@ func throwDice() (int64, error) {
return number, nil
}

func (c *GenerateConfig) findDicewareWord(number string, lang string) (string, error) {
func (c *Config) findDicewareWord(number string, lang string) (string, error) {
wordPath := filepath.Join("words", "diceware_words_"+lang, number+".txt")
word := ""
wordBytes, err := words.ReadFile(wordPath)
if err != nil {
word, err = findCustomDicewareWord(string(wordBytes))
word, err = findCustomDicewareWord(filepath.Join("diceware_words_"+lang, number+".txt"))
if err != nil {
return "", fmt.Errorf("unable to find word for index %q. err: %s", number, err.Error())
}
Expand All @@ -139,7 +138,7 @@ func findCustomDicewareWord(wordPath string) (string, error) {
if err != nil {
return "", err
}
path := filepath.Join(home, ".diceware", wordPath)
path := filepath.Join(home, ".diceware-cli.d", wordPath)
file, err := os.Open(path)
if err != nil {
return "", err
Expand All @@ -151,5 +150,5 @@ func findCustomDicewareWord(wordPath string) (string, error) {
return scanner.Text(), nil
}

return "", fmt.Errorf("couldn't read word from custom dictionary.")
return "", fmt.Errorf("couldn't read word from custom dictionary.\n")
}

0 comments on commit 807a3c0

Please sign in to comment.