-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
129 lines (113 loc) · 3.39 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
package main
import (
"os"
"time"
"github.com/pterm/pterm"
"github.com/pterm/pterm/putils"
"github.com/reznik99/go-hsm-doc/internal"
)
const (
HSMInfoCMD = "List HSM Info"
SlotsCMD = "List Slots"
TokensCMD = "List Tokens"
FindTokenCMD = "Find Token"
GenerateKeysCMD = "Generate Keys"
ImportKeysCMD = "Import Keys"
ExitCMD = "Exit"
)
var (
Version = "Development"
logger = pterm.Logger{
Formatter: pterm.LogFormatterColorful,
Writer: os.Stdout,
Level: pterm.LogLevelTrace,
KeyStyles: map[string]pterm.Style{},
MaxWidth: 100,
}
TopLevelOptions = []string{HSMInfoCMD, SlotsCMD, TokensCMD, FindTokenCMD, GenerateKeysCMD, ImportKeysCMD, ExitCMD}
InteractiveText = pterm.DefaultInteractiveTextInput.WithOnInterruptFunc(ExitFunc)
InteractiveConfirm = pterm.DefaultInteractiveConfirm.WithOnInterruptFunc(ExitFunc)
InteractiveSelect = pterm.DefaultInteractiveSelect.WithOnInterruptFunc(ExitFunc).WithMaxHeight(len(TopLevelOptions))
TitlePrefix = putils.LettersFromStringWithStyle("HSM", pterm.FgCyan.ToStyle())
Title = putils.LettersFromStringWithStyle("-DOCTOR", pterm.FgLightMagenta.ToStyle())
)
func fatal(message string, args ...any) {
pterm.Error.Printfln(message, args...)
os.Exit(1)
}
func PrintTitle() {
pterm.Info.Println("\033[H\033[2J")
pterm.DefaultBigText.WithLetters(TitlePrefix, Title).Render()
pterm.Info.Printfln("Version %q", Version)
}
func PressEnterToContinue() {
_, err := InteractiveText.Show("Press any key to continue")
if err != nil {
logger.Error("Error reading user input", logger.Args("", err))
}
}
func main() {
PrintTitle()
modulePath, err := InteractiveText.Show("Input Cryptoki Library path (.dll / .so)")
if err != nil {
fatal("Error reading user input: %s", err)
}
if modulePath == "" {
fatal("Module path is empty but required")
}
multi := pterm.DefaultMultiPrinter
loader, _ := pterm.DefaultSpinner.WithWriter(multi.NewWriter()).Start("Loading Cryptoki module")
multi.Start()
time.Sleep(time.Second / 2)
mod, err := internal.NewP11(modulePath, logger)
if err != nil {
fatal("Error loading module: '%s'", err)
}
loader.Info("Loaded cryptoki module -> ", modulePath)
multi.Stop()
// Main program loop
for {
PrintTitle()
option, err := InteractiveSelect.WithOptions(TopLevelOptions).Show("Select Operation")
if err != nil {
logger.Error("Option selection error", logger.Args("", err))
continue
}
switch option {
case HSMInfoCMD:
err := ListHSMInfo(mod)
if err != nil {
logger.Error("Error getting HSM info", logger.Args("", err))
}
case SlotsCMD:
err := ListSlots(mod)
if err != nil {
logger.Error("Error listing slots", logger.Args("", err))
}
case TokensCMD:
err := ListTokens(mod)
if err != nil {
logger.Error("Error listing tokens", logger.Args("", err))
}
case FindTokenCMD:
err := FindToken(mod)
if err != nil {
logger.Error("Error during Find Token operation", logger.Args("", err))
}
case GenerateKeysCMD:
err := GenerateKey(mod)
if err != nil {
logger.Error("Error generating key", logger.Args("", err))
}
case ImportKeysCMD:
err := ImportKey(mod)
if err != nil {
logger.Error("Error importing key", logger.Args("", err))
}
case ExitCMD:
ExitFunc()
}
// Pause CLI to let user read output of command. On keypress, clear screen and relist CLI options.
PressEnterToContinue()
}
}