outsource preRun and run func

This commit is contained in:
Stephan Müller 2019-04-19 19:28:50 +02:00
parent cf0a60d26c
commit 7d008c14a3
No known key found for this signature in database
GPG Key ID: 4650F39E5B5E1894

104
main.go
View File

@ -23,13 +23,59 @@ func main() {
Version: version, Version: version,
} }
config := &Config{}
var dumpCmd = &cobra.Command{ var dumpCmd = &cobra.Command{
Use: "dump", Use: "dump",
Short: "Dump Let's Encrypt certificates from Traefik", Short: "Dump Let's Encrypt certificates from Traefik",
Long: `Dump ACME data from Traefik of different storage backends to certificates.`, Long: `Dump ACME data from Traefik of different storage backends to certificates.`,
PreRunE: func(cmd *cobra.Command, args []string) error { PreRunE: commandPreRun,
RunE: commandRun,
}
dumpCmd.Flags().String("source", "file", "Source type, one of 'file', 'consul', 'etcd', 'zookeeper', 'boltdb'. Options for each source type are prefixed with `source.<type>.`")
dumpCmd.Flags().String("source.file", "./acme.json", "Path to 'acme.json' for file source.")
// Generic parameters for Key/Value backends
dumpCmd.Flags().String("source.kv.endpoints", "localhost:8500", "Comma seperated list of endpoints.")
dumpCmd.Flags().Int("source.kv.connection-timeout", 0, "Connection timeout in seconds.")
dumpCmd.Flags().String("source.kv.password", "", "Password for connection.")
dumpCmd.Flags().String("source.kv.username", "", "Username for connection.")
dumpCmd.Flags().Bool("source.kv.tls.enable", false, "Enable TLS encryption.")
dumpCmd.Flags().Bool("source.kv.tls.insecureskipverify", false, "Trust unverified certificates if TLS is enabled.")
dumpCmd.Flags().String("source.kv.tls.ca-cert-file", "", "Root CA file for certificate verification if TLS is enabled.")
// Special parameters for etcd
dumpCmd.Flags().Int("source.kv.etcd.sync-period", 0, "Sync period for etcd in seconds.")
// Special parameters for boltdb
dumpCmd.Flags().Bool("source.kv.boltdb.persist-connection", false, "Persist connection for boltdb.")
dumpCmd.Flags().String("source.kv.boltdb.bucket", "traefik", "Bucket for boltdb.")
// Special parameters for consul
dumpCmd.Flags().String("source.kv.consul.token", "", "Token for consul.")
dumpCmd.Flags().Bool("watch", false, "Enable watching changes.")
dumpCmd.Flags().String("dest", "./dump", "Path to store the dump content.")
dumpCmd.Flags().String("crt-ext", ".crt", "The file extension of the generated certificates.")
dumpCmd.Flags().String("crt-name", "certificate", "The file name (without extension) of the generated certificates.")
dumpCmd.Flags().String("key-ext", ".key", "The file extension of the generated private keys.")
dumpCmd.Flags().String("key-name", "privatekey", "The file name (without extension) of the generated private keys.")
dumpCmd.Flags().Bool("domain-subdir", false, "Use domain as sub-directory.")
rootCmd.AddCommand(dumpCmd)
var versionCmd = &cobra.Command{
Use: "version",
Short: "Display version",
Run: func(_ *cobra.Command, _ []string) {
displayVersion(rootCmd.Name())
},
}
rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func commandPreRun(cmd *cobra.Command, args []string) error {
source := cmd.Flag("source").Value.String() source := cmd.Flag("source").Value.String()
sourceFile := cmd.Flag("source.file").Value.String() sourceFile := cmd.Flag("source.file").Value.String()
watch, _ := strconv.ParseBool(cmd.Flag("watch").Value.String()) watch, _ := strconv.ParseBool(cmd.Flag("watch").Value.String())
@ -61,8 +107,11 @@ func main() {
} }
return nil return nil
}, }
RunE: func(cmd *cobra.Command, _ []string) error {
func commandRun(cmd *cobra.Command, _ []string) error {
config := &Config{}
source := cmd.Flag("source").Value.String() source := cmd.Flag("source").Value.String()
acmeFile := cmd.Flag("source.file").Value.String() acmeFile := cmd.Flag("source.file").Value.String()
@ -150,49 +199,4 @@ func main() {
} }
return nil return nil
},
}
dumpCmd.Flags().String("source", "file", "Source type, one of 'file', 'consul', 'etcd', 'zookeeper', 'boltdb'. Options for each source type are prefixed with `source.<type>.`")
dumpCmd.Flags().String("source.file", "./acme.json", "Path to 'acme.json' for file source.")
// Generic parameters for Key/Value backends
dumpCmd.Flags().String("source.kv.endpoints", "localhost:8500", "Comma seperated list of endpoints.")
dumpCmd.Flags().Int("source.kv.connection-timeout", 0, "Connection timeout in seconds.")
dumpCmd.Flags().String("source.kv.password", "", "Password for connection.")
dumpCmd.Flags().String("source.kv.username", "", "Username for connection.")
dumpCmd.Flags().Bool("source.kv.tls.enable", false, "Enable TLS encryption.")
dumpCmd.Flags().Bool("source.kv.tls.insecureskipverify", false, "Trust unverified certificates if TLS is enabled.")
dumpCmd.Flags().String("source.kv.tls.ca-cert-file", "", "Root CA file for certificate verification if TLS is enabled.")
// Special parameters for etcd
dumpCmd.Flags().Int("source.kv.etcd.sync-period", 0, "Sync period for etcd in seconds.")
// Special parameters for boltdb
dumpCmd.Flags().Bool("source.kv.boltdb.persist-connection", false, "Persist connection for boltdb.")
dumpCmd.Flags().String("source.kv.boltdb.bucket", "traefik", "Bucket for boltdb.")
// Special parameters for consul
dumpCmd.Flags().String("source.kv.consul.token", "", "Token for consul.")
dumpCmd.Flags().Bool("watch", false, "Enable watching changes.")
dumpCmd.Flags().String("dest", "./dump", "Path to store the dump content.")
dumpCmd.Flags().String("crt-ext", ".crt", "The file extension of the generated certificates.")
dumpCmd.Flags().String("crt-name", "certificate", "The file name (without extension) of the generated certificates.")
dumpCmd.Flags().String("key-ext", ".key", "The file extension of the generated private keys.")
dumpCmd.Flags().String("key-name", "privatekey", "The file name (without extension) of the generated private keys.")
dumpCmd.Flags().Bool("domain-subdir", false, "Use domain as sub-directory.")
rootCmd.AddCommand(dumpCmd)
var versionCmd = &cobra.Command{
Use: "version",
Short: "Display version",
Run: func(_ *cobra.Command, _ []string) {
displayVersion(rootCmd.Name())
},
}
rootCmd.AddCommand(versionCmd)
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
} }