Skip to content

Commit addee78

Browse files
committed
fix: Simplify config output
1 parent eb61f11 commit addee78

3 files changed

Lines changed: 20 additions & 12 deletions

File tree

backup/cmd/config.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,14 @@ package cmd
33
import (
44
"backup-rsync/backup/internal"
55
"fmt"
6-
"log"
76

87
"github.com/spf13/cobra"
9-
"gopkg.in/yaml.v3"
108
)
119

1210
func buildConfigCommand() *cobra.Command {
1311
var configCmd = &cobra.Command{
1412
Use: "config",
1513
Short: "Manage configuration",
16-
Run: func(cmd *cobra.Command, args []string) {
17-
// Implementation for the config command
18-
fmt.Println("Config command executed")
19-
},
2014
}
2115

2216
var showVerb = &cobra.Command{
@@ -26,12 +20,7 @@ func buildConfigCommand() *cobra.Command {
2620
configPath, _ := cmd.Flags().GetString("config")
2721
cfg := internal.LoadResolvedConfig(configPath)
2822

29-
out, err := yaml.Marshal(cfg)
30-
if err != nil {
31-
log.Fatalf("Failed to marshal resolved configuration: %v", err)
32-
}
33-
34-
fmt.Printf("Resolved Configuration:\n%s\n", string(out))
23+
fmt.Printf("Resolved Configuration:\n%s\n", cfg)
3524
},
3625
}
3726

backup/internal/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ var (
2020
ErrOverlappingPath = errors.New("overlapping path detected")
2121
)
2222

23+
func (cfg Config) String() string {
24+
out, _ := yaml.Marshal(cfg)
25+
return string(out)
26+
}
27+
2328
func LoadConfig(reader io.Reader) (Config, error) {
2429
var cfg Config
2530

backup/internal/test/config_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,3 +318,17 @@ func TestValidatePaths_InvalidPaths(t *testing.T) {
318318
assert.EqualError(t, err, test.errorMessage)
319319
})
320320
}
321+
322+
func TestConfigString_ValidConfig(t *testing.T) {
323+
cfg := internal.Config{
324+
Sources: []internal.Path{},
325+
Targets: []internal.Path{},
326+
Variables: map[string]string{},
327+
Jobs: []internal.Job{},
328+
}
329+
330+
expectedOutput := "sources: []\ntargets: []\nvariables: {}\njobs: []\n"
331+
actualOutput := cfg.String()
332+
333+
assert.Equal(t, expectedOutput, actualOutput)
334+
}

0 commit comments

Comments
 (0)