feat: allow to customize file name.
This commit is contained in:
parent
368d2ec120
commit
3c0aceaca3
25
dumper.go
25
dumper.go
@ -45,7 +45,12 @@ type Account struct {
|
||||
KeyType certcrypto.KeyType
|
||||
}
|
||||
|
||||
func dump(acmeFile, dumpPath string, crtExt, keyExt string, domainSubDir bool) error {
|
||||
type fileInfo struct {
|
||||
Name string
|
||||
Ext string
|
||||
}
|
||||
|
||||
func dump(acmeFile, dumpPath string, crtInfo, keyInfo fileInfo, domainSubDir bool) error {
|
||||
f, err := os.Open(acmeFile)
|
||||
if err != nil {
|
||||
return err
|
||||
@ -71,18 +76,18 @@ func dump(acmeFile, dumpPath string, crtExt, keyExt string, domainSubDir bool) e
|
||||
}
|
||||
|
||||
privateKeyPem := extractPEMPrivateKey(data.Account)
|
||||
err = ioutil.WriteFile(filepath.Join(dumpPath, keysSubDir, "letsencrypt"+keyExt), privateKeyPem, 0666)
|
||||
err = ioutil.WriteFile(filepath.Join(dumpPath, keysSubDir, "letsencrypt"+keyInfo.Ext), privateKeyPem, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, cert := range data.Certificates {
|
||||
err := writeCert(dumpPath, cert, crtExt, domainSubDir)
|
||||
err := writeCert(dumpPath, cert, crtInfo, domainSubDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = writeKey(dumpPath, cert, keyExt, domainSubDir)
|
||||
err = writeKey(dumpPath, cert, keyInfo, domainSubDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -91,10 +96,10 @@ func dump(acmeFile, dumpPath string, crtExt, keyExt string, domainSubDir bool) e
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeCert(dumpPath string, cert *Certificate, ext string, domainSubDir bool) error {
|
||||
certPath := filepath.Join(dumpPath, keysSubDir, cert.Domain.Main+ext)
|
||||
func writeCert(dumpPath string, cert *Certificate, info fileInfo, domainSubDir bool) error {
|
||||
certPath := filepath.Join(dumpPath, keysSubDir, cert.Domain.Main+info.Ext)
|
||||
if domainSubDir {
|
||||
certPath = filepath.Join(dumpPath, cert.Domain.Main, "certificate"+ext)
|
||||
certPath = filepath.Join(dumpPath, cert.Domain.Main, info.Name+info.Ext)
|
||||
if err := os.MkdirAll(filepath.Join(dumpPath, cert.Domain.Main), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -103,10 +108,10 @@ func writeCert(dumpPath string, cert *Certificate, ext string, domainSubDir bool
|
||||
return ioutil.WriteFile(certPath, cert.Certificate, 0666)
|
||||
}
|
||||
|
||||
func writeKey(dumpPath string, cert *Certificate, ext string, domainSubDir bool) error {
|
||||
keyPath := filepath.Join(dumpPath, certsSubDir, cert.Domain.Main+ext)
|
||||
func writeKey(dumpPath string, cert *Certificate, info fileInfo, domainSubDir bool) error {
|
||||
keyPath := filepath.Join(dumpPath, certsSubDir, cert.Domain.Main+info.Ext)
|
||||
if domainSubDir {
|
||||
keyPath = filepath.Join(dumpPath, cert.Domain.Main, "privatekey"+ext)
|
||||
keyPath = filepath.Join(dumpPath, cert.Domain.Main, info.Name+info.Ext)
|
||||
if err := os.MkdirAll(filepath.Join(dumpPath, cert.Domain.Main), 0755); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
23
main.go
23
main.go
@ -25,20 +25,33 @@ func main() {
|
||||
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 crtExt == keyExt && !subDir {
|
||||
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()
|
||||
crtExt := cmd.Flag("crt-ext").Value.String()
|
||||
keyExt := cmd.Flag("key-ext").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, crtExt, keyExt, subDir)
|
||||
err := dump(acmeFile, dumpPath, crtInfo, keyInfo, subDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -50,7 +63,9 @@ func main() {
|
||||
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)
|
||||
|
||||
|
||||
60
readme.md
60
readme.md
@ -31,15 +31,19 @@ Usage:
|
||||
|
||||
Flags:
|
||||
--crt-ext string The file extension of the generated certificates. (default ".crt")
|
||||
--crt-name string The file name (without extension) of the generated certificates. (default "certificate")
|
||||
--dest string Path to store the dump content. (default "./dump")
|
||||
--domain-subdir Use domain as sub-directory.
|
||||
-h, --help help for dump
|
||||
--key-ext string The file extension of the generated private keys. (default ".key")
|
||||
--key-name string The file name (without extension) of the generated private keys. (default "privatekey")
|
||||
--source string Path to 'acme.json' file. (default "./acme.json")
|
||||
```
|
||||
|
||||
## Examples
|
||||
|
||||
### Simple Dump
|
||||
|
||||
```console
|
||||
$ traefik-certs-dumper dump
|
||||
dump
|
||||
@ -51,25 +55,7 @@ dump
|
||||
|
||||
```
|
||||
|
||||
```console
|
||||
$ traefik-certs-dumper dump --domain-subdir=true
|
||||
dump
|
||||
├──my.domain.com
|
||||
│ ├──certificate.crt
|
||||
│ └──privatekey.key
|
||||
└──private
|
||||
└──letsencrypt.key
|
||||
```
|
||||
|
||||
```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
|
||||
```
|
||||
#### Change source and destination
|
||||
|
||||
```console
|
||||
$ traefik-certs-dumper dump --source ./acme.json --dest ./dump/test
|
||||
@ -81,3 +67,39 @@ test
|
||||
└──letsencrypt.key
|
||||
|
||||
```
|
||||
|
||||
### Use domain as sub-directory
|
||||
|
||||
```console
|
||||
$ traefik-certs-dumper dump --domain-subdir=true
|
||||
dump
|
||||
├──my.domain.com
|
||||
│ ├──certificate.crt
|
||||
│ └──privatekey.key
|
||||
└──private
|
||||
└──letsencrypt.key
|
||||
```
|
||||
|
||||
#### Change file extension
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
#### Change file name
|
||||
|
||||
```console
|
||||
$ traefik-certs-dumper dump --domain-subdir=true --crt-name=fullchain --key-name=privkey
|
||||
dump
|
||||
├──my.domain.com
|
||||
│ ├──fullchain.crt
|
||||
│ └──privkey.key
|
||||
└──private
|
||||
└──letsencrypt.key
|
||||
```
|
||||
|
||||
Loading…
Reference in New Issue
Block a user