-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathetcdenv.go
128 lines (103 loc) · 3.54 KB
/
etcdenv.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
/*
Copyright 2014 Upfluence, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"github.com/upfluence/etcdenv/etcdenv"
"github.com/upfluence/goutils/log"
)
const currentVersion = "0.4.1"
var (
flagset = flag.NewFlagSet("etcdenv", flag.ExitOnError)
flags = struct {
Version bool
ShutdownBehaviour string
Server string
Namespace string
WatchedKeys string
UserName string
Password string
}{}
)
func usage() {
fmt.Fprintf(os.Stderr, `
NAME
etcdenv - use your etcd keys as environment variables
USAGE
etcdenv [options] <command>
OPTIONS
`)
flagset.PrintDefaults()
}
func init() {
flagset.BoolVar(&flags.Version, "version", false, "Print the version and exit")
flagset.BoolVar(&flags.Version, "v", false, "Print the version and exit")
flagset.StringVar(&flags.ShutdownBehaviour, "b", "exit", "Behaviour when the process stop [exit|keepalive|restart]")
flagset.StringVar(&flags.ShutdownBehaviour, "shutdown-behaviour", "exit", "Behaviour when the process stop [exit|keepalive|restart]")
flagset.StringVar(&flags.Server, "server", "http://127.0.0.1:4001", "Location of the etcd server")
flagset.StringVar(&flags.Server, "s", "http://127.0.0.1:4001", "Location of the etcd server")
flagset.StringVar(&flags.Namespace, "namespace", "/environments/production", "etcd directory where the environment variables are fetched")
flagset.StringVar(&flags.Namespace, "n", "/environments/production", "etcd directory where the environment variables are fetched")
flagset.StringVar(&flags.WatchedKeys, "watched", "", "environment variables to watch, comma-separated")
flagset.StringVar(&flags.WatchedKeys, "w", "", "environment variables to watch, comma-separated")
flagset.StringVar(&flags.UserName, "user", "", "user to authenticate to etcd server")
flagset.StringVar(&flags.UserName, "u", "", "user to authenticate to etcd server")
flagset.StringVar(&flags.Password, "password", "", "password to authenticate to etcd server")
flagset.StringVar(&flags.Password, "p", "", "password to authenticate to etcd server")
}
func main() {
var watchedKeysList []string
flagset.Parse(os.Args[1:])
flagset.Usage = usage
if len(os.Args) < 2 {
flagset.Usage()
os.Exit(0)
}
if flags.Version {
fmt.Printf("etcdenv v%s", currentVersion)
os.Exit(0)
}
signalChan := make(chan os.Signal)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
if flags.WatchedKeys == "" {
watchedKeysList = []string{}
} else {
watchedKeysList = strings.Split(flags.WatchedKeys, ",")
}
ctx, err := etcdenv.NewContext(
strings.Split(flags.Namespace, ","),
[]string{flags.Server},
flagset.Args(),
flags.ShutdownBehaviour,
watchedKeysList,
flags.UserName,
flags.Password,
)
if err != nil {
log.Fatalf(err.Error())
os.Exit(1)
}
go ctx.Run()
select {
case sig := <-signalChan:
log.Noticef("Received signal %s", sig)
ctx.ExitChan <- true
case <-ctx.ExitChan:
log.Infof("Catching ExitChan, doing nothin'")
}
}