-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
59 lines (47 loc) · 1.31 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
package main
import (
"log"
"os"
"strings"
kingpin "gopkg.in/alecthomas/kingpin.v2"
"github.com/alaa/vaultflow/cache"
"github.com/alaa/vaultflow/consul"
"github.com/alaa/vaultflow/sync"
"github.com/alaa/vaultflow/vault"
)
var (
pullCommand = kingpin.Command("pull", "pull latest secrets from vault.")
pullForceCommand = pullCommand.Flag("force", "force pull secrets").Bool()
pushCommand = kingpin.Command("push", "Push new secret to vault.")
pushSecretCommand = pushCommand.Flag("secret", "secret file name stored under vault-data/ directory").Required().String()
cachedir = kingpin.Flag("cache-dir", "cache directory").Default("vault-data").String()
)
func main() {
vaultToken := os.Getenv("VAULT_TOKEN")
vault, err := vault.New(vaultToken)
if err != nil {
log.Fatal(err)
}
consul, err := consul.New()
if err != nil {
log.Fatal(err)
}
opts := kingpin.Parse()
cache, err := cache.New(*cachedir)
if err != nil {
log.Fatal(err)
}
log.Printf("Using cache-dir=%s\n", *cachedir)
sync := sync.New(cache, consul, vault)
switch opts {
case "pull":
_ = *pullCommand
if err := sync.Pull(*pullForceCommand); err != nil {
log.Println(err)
}
case "push":
key := strings.Replace(*pushSecretCommand, *cachedir + "/", "", 1)
log.Printf("Pushing %s to vault\n", key)
sync.Push(key)
}
}