This repository has been archived by the owner on Mar 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
142 lines (120 loc) · 3.67 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*Copyright (C) 2010 Andreas Sinz
This file is part of GoPasswordCreator.
GoPasswordCreator is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; only version 2 of the License.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"fmt"
"os"
"strconv"
"strings"
)
var (
passwordLength = flag.Int("length", 8, "Length of the generated Password")
// Variables that define what characters to use in the password
lowerCase bool
upperCase bool
numerals bool
specialCharacters bool
usersCharacters string
passwordCount = flag.Int("count", 1, "Determine how many passwords to create")
file = flag.String("file", "", "Write passwords to the named file instead of standard output")
)
func usage() {
command := os.Args[0]
fmt.Fprintf(os.Stderr,
`Usage: %s [all] [alphanum] [lower] [upper] [numbers] [special] [own=CHARACTERS]
%s requires at least one of the following subcommands to specify what characters
may be used in the password:
all: Equivalent to 'alphanum special'
alphanum: Equivalent to 'lower upper numbers'
lower: Use lower-case letters
upper: Use upper-case letters
numbers: Use digits
special: Use special characters
own: Specifies a custom set of characters to use
'all', 'alphanum', 'lower', 'upper', 'numbers', and 'special' may be followed by
'=f' to nullify that character set.
Options:
`,
command, command)
flag.PrintDefaults()
}
// Sets a list of bool variables to the same value. If len(args) == 1, use true.
// Otherwise, parse args[1] as a bool and use that.
func setBool(args []string, vars ...*bool) {
on := true
if len(args) > 1 {
var err error
on, err = strconv.ParseBool(args[1])
if err != nil {
printError(err)
os.Exit(1)
}
}
for _, bp := range vars {
*bp = on
}
}
func main() {
flag.Usage = usage
flag.Parse()
for _, arg := range flag.Args() {
// Separate the subcommand from the value
parsed := strings.SplitN(arg, "=", 2)
// Group arguments by the data type of their value
switch parsed[0] {
case "own":
// Need a string value
if len(parsed) == 2 {
usersCharacters = parsed[1]
} else {
printError(fmt.Errorf("'own' requires a '=' to specify characters"))
}
// All other arguments take boolean values
case "all":
setBool(parsed, &lowerCase, &upperCase, &numerals, &specialCharacters)
case "alphanum":
setBool(parsed, &lowerCase, &upperCase, &numerals)
case "lower":
setBool(parsed, &lowerCase)
case "upper":
setBool(parsed, &upperCase)
case "numbers":
setBool(parsed, &numerals)
case "special":
setBool(parsed, &specialCharacters)
default:
printError(fmt.Errorf("Invalid argument: %s", parsed[0]))
}
}
var output *os.File
var fileErr error
if *file != "" {
if output, fileErr = os.Create(*file); fileErr != nil {
printError(fileErr)
output = os.Stdout
}
} else {
output = os.Stdout
}
creator, err := NewCreator(output, lowerCase, upperCase, numerals, specialCharacters, usersCharacters)
defer output.Close()
if err != nil {
printError(err)
} else {
writeErr := creator.WritePasswords(*passwordLength, *passwordCount)
if writeErr != nil {
printError(writeErr)
}
}
}
// Prints err.Error() to Stdout.
func printError(err error) {
fmt.Fprintln(os.Stderr, "Error: "+err.Error())
}