-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmain.go
71 lines (62 loc) · 1.51 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
package main
import (
"errors"
"fmt"
"os"
"github.com/miquella/vaulted/lib"
"github.com/miquella/vaulted/lib/legacy"
)
const (
EX_USAGE_ERROR = 64
EX_DATA_ERROR = 65
EX_UNAVAILABLE = 69
EX_TEMPORARY_ERROR = 79
)
type ErrorWithExitCode struct {
error
ExitCode int
}
var (
ErrNoError = errors.New("")
ErrFileNotExist = ErrorWithExitCode{os.ErrNotExist, EX_USAGE_ERROR}
ErrNoPasswordEntered = ErrorWithExitCode{errors.New("Could not get password"), EX_UNAVAILABLE}
ErrNoMFATokenEntered = ErrorWithExitCode{errors.New("Could not get MFA token"), EX_UNAVAILABLE}
)
func main() {
command, err := ParseArgs(os.Args[1:])
if err == nil {
steward := NewSteward()
store := struct {
vaulted.Store
legacy.LegacyStore
}{
Store: vaulted.New(steward),
LegacyStore: legacy.New(steward),
}
err = command.Run(store)
}
if err != nil {
err = mapErrorWithExitCode(err)
exiterr, ok := err.(ErrorWithExitCode)
if !ok || exiterr.error != ErrNoError {
fmt.Fprintln(os.Stderr, err)
}
if ok {
os.Exit(exiterr.ExitCode)
} else {
os.Exit(1)
}
}
}
func mapErrorWithExitCode(err error) error {
switch err {
case vaulted.ErrIncorrectPassword:
return ErrorWithExitCode{vaulted.ErrIncorrectPassword, EX_TEMPORARY_ERROR}
case vaulted.ErrInvalidKeyConfig:
return ErrorWithExitCode{vaulted.ErrInvalidKeyConfig, EX_DATA_ERROR}
case vaulted.ErrInvalidEncryptionConfig:
return ErrorWithExitCode{vaulted.ErrInvalidEncryptionConfig, EX_DATA_ERROR}
default:
return err
}
}