package main import ( "fmt" "io/ioutil" "os" "path/filepath" "strconv" "github.com/spf13/cobra" ) func main() { var rootCmd = &cobra.Command{ Use: "traefik-certs-dumper", Short: "Dump Let's Encrypt certificates from Traefik", Long: `Dump the content of the "acme.json" file from Traefik to certificates.`, Version: version, } var dumpCmd = &cobra.Command{ Use: "dump", Short: "Dump Let's Encrypt certificates from Traefik", Long: `Dump the content of the "acme.json" file from Traefik to certificates.`, PreRunE: func(cmd *cobra.Command, args []string) error { crtExt := cmd.Flag("crt-ext").Value.String() keyExt := cmd.Flag("key-ext").Value.String() subDir, _ := strconv.ParseBool(cmd.Flag("domain-subdir").Value.String()) if !subDir { if crtExt == keyExt { return fmt.Errorf("--crt-ext (%q) and --key-ext (%q) are identical, in this case --domain-subdir is required", crtExt, keyExt) } } return nil }, RunE: func(cmd *cobra.Command, _ []string) error { acmeFile := cmd.Flag("source").Value.String() dumpPath := cmd.Flag("dest").Value.String() crtInfo := fileInfo{ Name: cmd.Flag("crt-name").Value.String(), Ext: cmd.Flag("crt-ext").Value.String(), } keyInfo := fileInfo{ Name: cmd.Flag("key-name").Value.String(), Ext: cmd.Flag("key-ext").Value.String(), } subDir, _ := strconv.ParseBool(cmd.Flag("domain-subdir").Value.String()) err := dump(acmeFile, dumpPath, crtInfo, keyInfo, subDir) if err != nil { return err } return tree(dumpPath, "") }, } dumpCmd.Flags().String("source", "./acme.json", "Path to 'acme.json' file.") 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 tree(root, indent string) error { fi, err := os.Stat(root) if err != nil { return fmt.Errorf("could not stat %s: %v", root, err) } fmt.Println(fi.Name()) if !fi.IsDir() { return nil } fis, err := ioutil.ReadDir(root) if err != nil { return fmt.Errorf("could not read dir %s: %v", root, err) } var names []string for _, fi := range fis { if fi.Name()[0] != '.' { names = append(names, fi.Name()) } } for i, name := range names { add := "│ " if i == len(names)-1 { fmt.Printf(indent + "└──") add = " " } else { fmt.Printf(indent + "├──") } if err := tree(filepath.Join(root, name), indent+add); err != nil { return err } } return nil }