feat: introduce an option for keeping existing folder structure of dump folder (#12)

This commit is contained in:
Stephan Müller 2019-04-20 22:08:44 +02:00 committed by Ludovic Fernandez
parent e2b5cc7e60
commit 617007edd1
3 changed files with 13 additions and 2 deletions

View File

@ -59,6 +59,7 @@ func init() {
rootCmd.PersistentFlags().String("key-ext", ".key", "The file extension of the generated private keys.")
rootCmd.PersistentFlags().String("key-name", "privatekey", "The file name (without extension) of the generated private keys.")
rootCmd.PersistentFlags().Bool("domain-subdir", false, "Use domain as sub-directory.")
rootCmd.PersistentFlags().Bool("clean", true, "Clean destination folder before dumping content.")
}
// initConfig reads in config file and ENV variables if set.
@ -93,6 +94,11 @@ func getBaseConfig(cmd *cobra.Command) (*dumper.BaseConfig, error) {
return nil, err
}
clean, err := strconv.ParseBool(cmd.Flag("clean").Value.String())
if err != nil {
return nil, err
}
return &dumper.BaseConfig{
DumpPath: cmd.Flag("dest").Value.String(),
CrtInfo: dumper.FileInfo{
@ -104,6 +110,7 @@ func getBaseConfig(cmd *cobra.Command) (*dumper.BaseConfig, error) {
Ext: cmd.Flag("key-ext").Value.String(),
},
DomainSubDir: subDir,
Clean: clean,
}, nil
}

View File

@ -6,4 +6,5 @@ type BaseConfig struct {
CrtInfo FileInfo
KeyInfo FileInfo
DomainSubDir bool
Clean bool
}

View File

@ -22,8 +22,11 @@ type FileInfo struct {
// Dump Dumps data to certificates.
func Dump(data *StoredData, baseConfig *BaseConfig) error {
if err := os.RemoveAll(baseConfig.DumpPath); err != nil {
return err
if baseConfig.Clean {
if err := os.RemoveAll(baseConfig.DumpPath); err != nil {
return err
}
}
if !baseConfig.DomainSubDir {