skip run empty config

This commit is contained in:
tingfeng 2024-12-26 12:31:50 +08:00
parent 47456f58ed
commit b16ef2618a
1 changed files with 14 additions and 3 deletions

View File

@ -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) {