/
root.go
107 lines (85 loc) · 3.36 KB
/
root.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
package cmd
import (
"fmt"
"log"
"os"
"strings"
"time"
"go.etcd.io/etcd/clientv3"
"google.golang.org/grpc/grpclog"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
const (
cliName = "traefik-etcd-coredns"
cliDescription = "Traefik custom acme.dnsChallenge provider for CoreDNS servers with Etcd backend."
defaultDialTimeout = 2 * time.Second
defaultCommandTimeOut = 5 * time.Second
)
var client *clientv3.Client
var (
rootCmd = &cobra.Command{
Use: cliName,
Short: cliDescription,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
initClient()
},
PersistentPostRun: func(cmd *cobra.Command, args []string) {
client.Close()
},
}
)
func init() {
log.SetPrefix("[" + cliName + "] ")
rootCmd.PersistentFlags().StringSliceVar(&globalFlags.Endpoints, "endpoints", []string{"127.0.0.1:2379"}, "gRPC endpoints")
rootCmd.PersistentFlags().BoolVar(&globalFlags.Debug, "debug", false, "enable client-side debug logging")
rootCmd.PersistentFlags().StringVar(&globalFlags.CoreDNSPrefix, "prefix", "/skydns", "etcd key prefix")
rootCmd.PersistentFlags().DurationVar(&globalFlags.DialTimeout, "dial-timeout", defaultDialTimeout, "dial timeout for client connections")
rootCmd.PersistentFlags().DurationVar(&globalFlags.CommandTimeOut, "command-timeout", defaultCommandTimeOut, "timeout for short running command (excluding dial timeout)")
rootCmd.PersistentFlags().BoolVar(&globalFlags.InsecureTransport, "insecure-transport", true, "disable transport security for client connections")
rootCmd.PersistentFlags().BoolVar(&globalFlags.TLS.InsecureSkipVerify, "insecure-skip-tls-verify", false, "skip server certificate verification")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.CertFile, "cert", "", "identify secure client using this TLS certificate file")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.KeyFile, "key", "", "identify secure client using this TLS key file")
rootCmd.PersistentFlags().StringVar(&globalFlags.TLS.TrustedCAFile, "cacert", "", "verify certificates of TLS-enabled secure servers using this CA bundle")
rootCmd.PersistentFlags().StringVar(&globalFlags.User, "user", "", "username[:password] for authentication")
rootCmd.PersistentFlags().StringVar(&globalFlags.Password, "password", "", "password for authentication (if this option is used, --user option shouldn't include password)")
rootCmd.PersistentFlags().VisitAll(func(flag *pflag.Flag) {
env := strings.ToUpper(strings.Replace(cliName+"_"+flag.Name, "-", "_", -1))
flag.Usage = fmt.Sprintf("[env %v] %v", env, flag.Usage)
if value := os.Getenv(env); value != "" {
flag.Value.Set(value)
}
})
rootCmd.Flags().SortFlags = false
rootCmd.PersistentFlags().SortFlags = false
cobra.EnableCommandSorting = false
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
//fmt.Println(err)
os.Exit(1)
}
os.Exit(0)
}
func initClient() {
tlsConfig, err := globalFlags.TLS.ClientConfig()
if err != nil {
log.Fatal(err)
}
var config = clientv3.Config{
Endpoints: globalFlags.Endpoints,
DialTimeout: globalFlags.DialTimeout,
Username: globalFlags.User,
Password: globalFlags.Password,
}
if !globalFlags.InsecureTransport {
config.TLS = tlsConfig
}
if globalFlags.Debug {
clientv3.SetLogger(grpclog.NewLoggerV2(os.Stderr, os.Stderr, os.Stderr))
}
client, err = clientv3.New(config)
if err != nil {
log.Fatal(err)
}
}