-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
executable file
·196 lines (163 loc) · 5 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//go:build go1.7
// +build go1.7
package main
import (
"fmt"
"io"
"log"
"os"
"runtime"
"github.com/fatih/color"
cli "github.com/jawher/mow.cli"
"github.com/k-takata/go-iscygpty"
"github.com/mattn/go-isatty"
"github.com/raoulh/go-progress"
)
const (
CharStar = "\u2737"
CharAbort = "\u2718"
CharCheck = "\u2714"
CharWarning = "\u26A0"
CharArrow = "\u2012\u25b6"
CharVertLine = "\u2502"
)
var (
blue = color.New(color.FgBlue).SprintFunc()
errorRed = color.New(color.FgRed).SprintFunc()
errorBgRed = color.New(color.BgRed, color.FgBlack).SprintFunc()
green = color.New(color.FgGreen).SprintFunc()
cyan = color.New(color.FgCyan).SprintFunc()
bgCyan = color.New(color.FgWhite).SprintFunc()
)
var (
mcUrl *string
debugOpt *bool
optContext *string
optLogin *string
optPass *string
optDesc *string
optPrintDesc *bool
optFilename *string
optParameter *string
optValue *string
isTerminal bool
progressBar *progress.ProgressBar
)
func exit(err error, exit int) {
fmt.Fprintln(os.Stderr, errorRed(CharAbort), err)
cli.Exit(exit)
}
func checkLog() {
if !*debugOpt {
//completely disable debug output
log.SetFlags(0)
log.SetOutput(io.Discard)
}
}
func addDefaultArgs(cmd *cli.Cmd) {
mcUrl = cmd.StringOpt("m moolticute_url", MOOLTICUTE_DAEMON_URL, "Use a different url for connecting to moolticute")
debugOpt = cmd.BoolOpt("debug", false, "Add debug log to stdout")
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
// fix for cygwin terminal
if iscygpty.IsCygwinPty(os.Stdout.Fd()) || isatty.IsTerminal(os.Stdout.Fd()) {
color.NoColor = false
isTerminal = true
}
app := cli.App("mc-cli", "Command line tool to interact with a mooltipass device through a moolticute daemon")
app.Command("login", "Manage credentials stored in the device", func(cmd *cli.Cmd) {
cmd.Command("get", "Get a password for given context", func(cmd *cli.Cmd) {
optContext = cmd.StringArg("CONTEXT", "", "Context to work on")
optLogin = cmd.StringArg("LOGIN", "", "Login to use")
addDefaultArgs(cmd)
optPrintDesc = cmd.BoolOpt("d description", false, "Output service description instead of password")
cmd.Spec = "CONTEXT LOGIN [OPTIONS]"
cmd.Action = func() {
checkLog()
if err := processLoginCmd("get", *optContext, *optLogin, "", "", *optPrintDesc); err != nil {
exit(err, 1)
}
}
})
cmd.Command("set", "Add or update a context", func(cmd *cli.Cmd) {
optContext = cmd.StringArg("CONTEXT", "", "Context to work on")
optLogin = cmd.StringArg("LOGIN", "", "Login to set")
optPass = cmd.StringArg("PASS", "", "Password to set (would be asked if not set in command line)")
optDesc = cmd.StringArg("DESC", "", "Description to set")
addDefaultArgs(cmd)
cmd.Spec = "CONTEXT LOGIN [PASS] [DESC] [OPTIONS]"
cmd.Action = func() {
checkLog()
if err := processLoginCmd("set", *optContext, *optLogin, *optPass, *optDesc, false); err != nil {
exit(err, 1)
}
}
})
})
app.Command("data", "Import & export small files stored in the device", func(cmd *cli.Cmd) {
cmd.Command("get", "Retrieve data for given context", func(cmd *cli.Cmd) {
optContext = cmd.StringArg("CONTEXT", "", "Context to work on")
addDefaultArgs(cmd)
cmd.Spec = "CONTEXT [OPTIONS]"
cmd.Action = func() {
checkLog()
if err := processDataCmd("get", *optContext, "", progressFunc); err != nil {
exit(err, 1)
}
}
})
cmd.Command("set", "Add or update data for given context", func(cmd *cli.Cmd) {
optContext = cmd.StringArg("CONTEXT", "", "Context to work on")
optFilename = cmd.StringArg("FILENAME", "", "File to save in the device")
addDefaultArgs(cmd)
cmd.Spec = "CONTEXT FILENAME [OPTIONS]"
cmd.Action = func() {
checkLog()
if err := processDataCmd("set", *optContext, *optFilename, progressFunc); err != nil {
exit(err, 1)
}
}
})
})
app.Command("parameters", "Get/Set device parameters", func(cmd *cli.Cmd) {
cmd.Command("get", "Retrieve device parameter", func(cmd *cli.Cmd) {
optParameter = cmd.StringArg("PARAMETER", "", "Selected device parameter")
addDefaultArgs(cmd)
cmd.Spec = "PARAMETER [OPTIONS]"
cmd.Action = func() {
checkLog()
err := processParameterCmd("get", *optParameter, "")
if err != nil {
exit(err, 1)
}
}
})
cmd.Command("set", "Set device parameter", func(cmd *cli.Cmd) {
optParameter = cmd.StringArg("PARAMETER", "", "Selected device parameter")
optValue = cmd.StringArg("VALUE", "", "Value to set the parameter to")
addDefaultArgs(cmd)
cmd.Spec = "PARAMETER VALUE [OPTIONS]"
cmd.Action = func() {
checkLog()
err := processParameterCmd("set", *optParameter, *optValue)
if err != nil {
exit(err, 1)
}
}
})
})
if err := app.Run(os.Args); err != nil {
exit(err, 1)
}
}
func progressFunc(total, current int) {
if !isTerminal {
return
}
if progressBar == nil {
progressBar = progress.New(total)
progressBar.Format = progress.ProgressFormats[2]
}
progressBar.Set(current)
}