-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
135 lines (110 loc) · 3.62 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
package main
import (
"errors"
"flag"
"fmt"
"giff-token/generator"
"giff-token/token"
"os"
)
const HELP_TEXT = `
༼ つ ◕_◕ ༽つ Giff Token (giff-token)
Usage:
giff-token
giff-token -help
giff-token [-length <length> -mode <mode> -includeChars <characters> -excludeChars <characters>]
giff-token [-length <length> -characters <characters>]
giff-token -config <file>
Options:
-h | -help Show this screen.
-length Minimum is 10, maximum is 4096. Default: 24.
-mode Select predefined base characters. Choose from: "alphanumeric", "alphanumeric_lowercase",
"alphanumeric_uppercase", "allchars", "allchars_lowercase", "allchars_uppercase",
"alphabetic", "alphabetic_lowercase", "alphabetic_uppercase" and "digits".
Default: "alphanumeric".
-characters Use a custom set of characters. "-mode", "-includeChars" and "-excludeChars" will be ignored.
-includeChars Include custom set of characters to a selected mode.
-excludeChars Exclude custom set of characters from a selected mode.
-config Use a config file to specify the previous parameters.
No options equals to:
giff-token -length 24 -mode alphanumeric
If there is a valid config file called "config.giff" in the same directory as the binary, no options equals to:
giff-token -config config.giff
Examples:
giff-token -help
giff-token -length 100
giff-token -mode allchars
giff-token -length 24 -characters asdfghjklqwerty
giff-token -length 2000 -mode alphabetic -includeChars 123 -excludeChars abc
giff-token -config /path/to/config.giff
`
const MIN_TOKEN_LENGTH = 10
const MAX_TOKEN_LENGTH = 4096
const CONFIG_FILENAME = "config.giff"
func main() {
flag.Usage = func() {
fmt.Print(HELP_TEXT)
}
length := flag.Uint("length", 24, "Token length")
modeStr := flag.String("mode", "alphanumeric", "Mode to select base characters")
characters := flag.String("characters", "", "Use custom set of characters")
includeChars := flag.String("includeChars", "", "Custom charset to include")
excludeChars := flag.String("excludeChars", "", "Custom charset to exclude")
configFile := flag.String("config", "", "Configuration file")
flag.Parse()
var config token.TokenConfig
var mode token.Mode
var err error
if flag.NFlag() == 0 {
code, err := setConfigFromFile(CONFIG_FILENAME, &config)
if err != nil {
if code == 1 {
config = token.NewTokenConfig(24, token.Alphanumeric, "", "", "")
} else {
fmt.Println(err)
return
}
}
} else {
if *configFile != "" {
code, err := setConfigFromFile(*configFile, &config)
if err != nil {
if code == 1 {
config = token.NewTokenConfig(24, token.Alphanumeric, "", "", "")
} else {
fmt.Println(err)
return
}
}
} else {
mode, err = token.GetModeFromString(*modeStr)
if err != nil {
fmt.Println(err)
return
}
config = token.NewTokenConfig(uint16(*length), mode, *characters, *includeChars, *excludeChars)
}
}
// Apply token length constraints
cfgLength := config.Length
if cfgLength < MIN_TOKEN_LENGTH {
config.Length = MIN_TOKEN_LENGTH
} else if cfgLength > MAX_TOKEN_LENGTH {
config.Length = MAX_TOKEN_LENGTH
}
fmt.Println(generator.GenerateToken(config))
}
func setConfigFromFile(filepath string, config *token.TokenConfig) (uint8, error) {
_, err := os.Stat(filepath);
if err != nil {
return 1, errors.New("ERROR: Couldn't open config file.")
} else {
parser, err := token.ParseConfigFile(filepath)
if err != nil {
return 2, err
} else {
*config = *parser
return 0, nil
}
}
}