-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (99 loc) · 3.25 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
package main
import (
"embed"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/pwiecz/go-fltk"
dice "github.com/charles-m-knox/go-dicewarelib"
)
//go:embed words-simple.txt
//go:embed words-complex.txt
var content embed.FS
// The version of the application; set at build time via:
//
// `go build -ldflags "-X main.version=1.2.3" main.go`
//
//nolint:revive
var version string = "dev"
// Flag for showing the version and subsequently quitting.
var flagVersion bool
var (
// If true, the app will always be rendered in portrait mode
forcePortrait bool
// If true, the app will always be rendered in landscape mode
forceLandscape bool
// app contains the shared state that is required for the entire app to
// function.
app App = App{
conf: &AppConfig{},
ui: &UI{},
words: dice.Words{},
}
)
// App contains the shared state that is required for the entire app to
// function.
type App struct {
// The configuration for the entire app, loaded and saved to the XDG config.
conf *AppConfig
// All of the UI elements for this app are contained in the UI struct.
ui *UI
// Data is stored between runs of this application in this yml config file.
configFilePath string
// The dictionary of diceware words, provided by the diceware lib.
words dice.Words
}
type AppConfig struct {
// If true, the app will start in dark mode
DarkMode bool `json:"darkMode"`
// If true, uses an extended word list
Extra bool `json:"useExtendedWordList"`
// The maximum permissible generated output length
MaxLen int `json:"maxLen"`
// The minimum permissible generated output length
MinLen int `json:"minLen"`
// The separator character (s) to place between generated words
Separator string `json:"separator"`
// The number of words to generate
WordCount int `json:"wordCount"`
}
func parseFlags() {
flag.BoolVar(&forcePortrait, "portrait", false, "force portrait orientation for the interface")
flag.BoolVar(&forceLandscape, "landscape", false, "force landscape orientation for the interface")
flag.StringVar(&app.configFilePath, "f", "", "the config file to write to, instead of the default provided by XDG config directories")
flag.StringVar(&app.conf.Separator, "s", " ", "the character(s) to place between each word")
flag.IntVar(&app.conf.MaxLen, "max", 64, "the longest permissible length of generated passwords")
flag.IntVar(&app.conf.MinLen, "min", 20, "the least permissible length of generated passwords")
flag.IntVar(&app.conf.WordCount, "wc", 3, "the number of words to generate")
flag.BoolVar(&app.conf.Extra, "extra", false, "if true, more complicated permutations of words will be used")
flag.BoolVar(&flagVersion, "v", false, "print version and exit")
flag.Parse()
}
func main() {
parseFlags()
if flagVersion {
//nolint:forbidigo
fmt.Println(version)
os.Exit(0)
}
app.loadConfig()
app.initDice()
app.initUI()
app.ui.theme(app.conf.DarkMode)
app.ui.responsive()
app.ui.upsize()
app.setCallbacks()
app.ui.win.End()
app.ui.win.Show()
go fltk.Run()
// start with an initial password populated in the output field
app.gen()
// Channel that receives OS signals, like ctrl+c to interrupt
var sc chan os.Signal = make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
// Block until a signal is received
<-sc
app.gracefulExit()
}