From f7141a44aac93a71764a573f5d2a9633506f7dc9 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Wed, 20 Feb 2019 20:23:12 +0100 Subject: [PATCH] feat: display dump tree. --- main.go | 49 +++++++++++++++++++++++++++++++++++++++++++++--- readme.md | 56 +++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 82 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index ce5bb28..eee6e78 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,9 @@ package main import ( "fmt" - "log" + "io/ioutil" "os" + "path/filepath" "strconv" "github.com/spf13/cobra" @@ -30,7 +31,7 @@ func main() { } return nil }, - Run: func(cmd *cobra.Command, _ []string) { + RunE: func(cmd *cobra.Command, _ []string) error { acmeFile := cmd.Flag("source").Value.String() dumpPath := cmd.Flag("dest").Value.String() crtExt := cmd.Flag("crt-ext").Value.String() @@ -39,8 +40,10 @@ func main() { err := dump(acmeFile, dumpPath, crtExt, keyExt, subDir) if err != nil { - log.Fatal(err) + return err } + + return tree(dumpPath, "") }, } @@ -66,3 +69,43 @@ func main() { 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 +} diff --git a/readme.md b/readme.md index b61a31b..785206b 100644 --- a/readme.md +++ b/readme.md @@ -4,8 +4,7 @@ [![Build Status](https://travis-ci.org/ldez/traefik-certs-dumper.svg?branch=master)](https://travis-ci.org/ldez/traefik-certs-dumper) [![Go Report Card](https://goreportcard.com/badge/github.com/ldez/traefik-certs-dumper)](https://goreportcard.com/report/github.com/ldez/traefik-certs-dumper) - -```yml +```yaml Dump the content of the "acme.json" file from Traefik to certificates. Usage: @@ -23,7 +22,7 @@ Flags: Use "traefik-certs-dumper [command] --help" for more information about a command. ``` -```yml +```yaml Dump the content of the "acme.json" file from Traefik to certificates. Usage: @@ -40,27 +39,44 @@ Flags: ## Examples -```bash -traefik-certs-dumper dump +```console +$ traefik-certs-dumper dump +dump +├──certs +│ └──my.domain.com.key +└──private + ├──my.domain.com.crt + └──letsencrypt.key + ``` -```bash -traefik-certs-dumper dump --source ./acme.json --dest ./dump +```console +$ traefik-certs-dumper dump --domain-subdir=true +dump +├──my.domain.com +│ ├──certificate.crt +│ └──privatekey.key +└──private + └──letsencrypt.key ``` -```bash -traefik-certs-dumper dump --crt-ext=.pem --key-ext=.pem +```console +$ traefik-certs-dumper dump --domain-subdir=true --crt-ext=.pem --key-ext=.pem +dump +├──my.domain.com +│ ├──certificate.pem +│ └──privatekey.pem +└──private + └──letsencrypt.key ``` -```bash -traefik-certs-dumper dump --domain-subdir=true +```console +$ traefik-certs-dumper dump --source ./acme.json --dest ./dump/test +test +├──certs +│ └──my.domain.com.key +└──private + ├──my.domain.com.crt + └──letsencrypt.key + ``` - -- https://github.com/containous/traefik/issues/4381 -- https://github.com/containous/traefik/issues/2418 -- https://github.com/containous/traefik/issues/3847 -- https://github.com/SvenDowideit/traefik-certdumper - -```bash -traefik-certs-dumper dump --use-subdir=false --crt-ext=.pem --key-ext=.pem --dest="/home/your_user/.homeassistant/" -``` \ No newline at end of file