-
Notifications
You must be signed in to change notification settings - Fork 3
/
vault4summon.go
60 lines (48 loc) · 1.05 KB
/
vault4summon.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
package main
import (
_ "embed"
"fmt"
"os"
"vault4summon/vault"
)
// generate version from last tag created
//
//go:generate bash -c "printf %s $(git describe --tags --abbrev=0) > version.txt"
//go:embed version.txt
var version string
func main() {
checkArgument()
argument := os.Args[1]
// Get the secret and key name from the argument
switch argument {
case "-v", "--version":
printVersion()
default:
result, err := vault.GetSecret(argument)
exitIfError(err)
printSecret(result)
}
}
func checkArgument() {
if len(os.Args) != 2 { //nolint:gomnd
exitIfError(fmt.Errorf("%s", "ERROR: a variable ID or version flag(-v or --version) must be given as the only one argument!")) //nolint:lll
}
}
func exitIfError(err error) {
if err == nil {
return
}
_, _ = os.Stderr.Write([]byte(err.Error()))
os.Exit(1)
}
func printSecret(secret string) {
_, err := os.Stdout.Write([]byte(secret))
exitIfError(err)
}
func printVersion() {
if len(version) == 0 {
version = "dev"
}
_, err := os.Stdout.Write([]byte(version))
exitIfError(err)
}