skip run empty config
This commit is contained in:
parent
47456f58ed
commit
b16ef2618a
|
@ -7,6 +7,7 @@ import (
|
|||
"RedisShake/internal/writer"
|
||||
"bytes"
|
||||
"container/heap"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/google/uuid"
|
||||
"github.com/spf13/cobra"
|
||||
|
@ -102,7 +103,10 @@ command_writer: redis_writer, redis_writer.sentinel`,
|
|||
}
|
||||
|
||||
description := "#this config file is generated by command: " + strings.Join(os.Args, " ")
|
||||
toml := viperMap2Toml(viperMap, description)
|
||||
toml ,err:= viperMap2Toml(viperMap, description)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if dryRun {
|
||||
fmt.Println(string(toml))
|
||||
return "", nil
|
||||
|
@ -119,21 +123,28 @@ command_writer: redis_writer, redis_writer.sentinel`,
|
|||
return filePath, nil
|
||||
}
|
||||
|
||||
func viperMap2Toml(viperMap map[string]*viper.Viper, description string) []byte {
|
||||
func viperMap2Toml(viperMap map[string]*viper.Viper, description string) ([]byte,error) {
|
||||
var buffer bytes.Buffer
|
||||
buffer.WriteString(description + "\n")
|
||||
isAllConfigEmpty := true
|
||||
for key, vp := range viperMap {
|
||||
settings := vp.AllSettings()
|
||||
if len(settings) == 0 {
|
||||
continue
|
||||
}
|
||||
if isAllConfigEmpty {
|
||||
isAllConfigEmpty = false
|
||||
}
|
||||
buffer.WriteString("[" + key + "]\n")
|
||||
for k, v := range settings {
|
||||
buffer.WriteString(fmt.Sprintf("%s = %#v\n", k, v))
|
||||
}
|
||||
buffer.WriteString("\n")
|
||||
}
|
||||
return buffer.Bytes()
|
||||
if isAllConfigEmpty{
|
||||
return []byte{},errors.New("all config empty")
|
||||
}
|
||||
return buffer.Bytes(),nil
|
||||
}
|
||||
|
||||
func buildCommandRedisWriterSentinel() (*viper.Viper, *cobra.Command) {
|
||||
|
|
Loading…
Reference in New Issue