-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
101 lines (87 loc) · 3.72 KB
/
Copy pathmain.go
File metadata and controls
101 lines (87 loc) · 3.72 KB
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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.etcd.io/etcd/clientv3"
"google.golang.org/grpc/grpclog"
)
const (
defaultDialTimeout = 2 * time.Second
defaultKeepAliveTime = 2 * time.Second
defaultKeepAliveTimeOut = 6 * time.Second
)
var (
rootCmd = &cobra.Command{
Use: "confsync",
Short: "A simple tool to synchronize on-disk configuration files from etcd v3 cluster",
SuggestFor: []string{"confsync"},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if globals.debug {
clientv3.SetLogger(grpclog.NewLoggerV2WithVerbosity(os.Stderr, os.Stderr, os.Stderr, 4))
} else {
clientv3.SetLogger(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
}
},
}
globals = struct {
endpoints []string
dialTimeout time.Duration
keepAliveTime time.Duration
keepAliveTimeout time.Duration
insecure bool
insecureDiscovery bool
insecureSkipVerify bool
debug bool
tls struct {
certFile string
keyFile string
caFile string
}
serviceName string
user string
}{}
)
func init() {
rootCmd.PersistentFlags().StringSliceVarP(&globals.endpoints, "endpoints", "e", nil, "list of gRPC `endpoints`")
rootCmd.PersistentFlags().StringVarP(&globals.serviceName, "domain", "d", "", "domain `name` to query for SRV records or to verify TLS-enabled secure servers against")
rootCmd.PersistentFlags().StringVar(&globals.serviceName, "discovery-srv", "", "domain `name`, the same as --domain (to keep compatible with ETCDCTL_ variables)")
rootCmd.PersistentFlags().BoolVar(&globals.debug, "debug", false, "enable client-side debug logging")
rootCmd.PersistentFlags().DurationVar(&globals.dialTimeout, "dial-timeout", defaultDialTimeout, "dial `timeout` for client connections")
rootCmd.PersistentFlags().DurationVar(&globals.keepAliveTime, "keepalive-time", defaultKeepAliveTime, "keepalive `time` for client connections")
rootCmd.PersistentFlags().DurationVar(&globals.keepAliveTimeout, "keepalive-timeout", defaultKeepAliveTimeOut, "keepalive `timeout` for client connections")
rootCmd.PersistentFlags().BoolVar(&globals.insecure, "insecure-transport", true, "disable transport security for client connections")
rootCmd.PersistentFlags().BoolVar(&globals.insecureDiscovery, "insecure-discovery", true, "accept insecure SRV records describing cluster endpoints")
rootCmd.PersistentFlags().BoolVar(&globals.insecureSkipVerify, "insecure-skip-tls-verify", false, "skip server certificate verification")
rootCmd.PersistentFlags().StringVar(&globals.tls.certFile, "cert", "", "identify secure client using this TLS certificate `file`")
rootCmd.PersistentFlags().StringVar(&globals.tls.keyFile, "key", "", "identify secure client using this TLS key `file`")
rootCmd.PersistentFlags().StringVar(&globals.tls.caFile, "cacert", "", "verify certificates of TLS-enabled secure servers using this CA bundle `file`")
rootCmd.PersistentFlags().StringVar(&globals.user, "user", "", "`username[:password]` for authentication (will prompt if password is not supplied)")
rootCmd.AddCommand(
newPutCommand(),
newWatchCommand(),
newUpdateStateCommand(),
)
cobra.EnablePrefixMatching = true
}
func fail(err error) {
_, _ = fmt.Fprintln(os.Stderr, "Error:", err)
os.Exit(1)
}
func main() {
rootCmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
envName := "ETCDCTL_" + strings.ToUpper(strings.Replace(f.Name, "-", "_", -1))
if envValue := os.Getenv(envName); envValue != "" {
if err := f.Value.Set(envValue); err != nil {
fail(fmt.Errorf("error setting %s: %s", envName, err))
}
}
})
if err := rootCmd.Execute(); err != nil {
fail(err)
}
}