Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
BACKLOG/
logs/
*.log
coverage/
dist/
13 changes: 1 addition & 12 deletions backup/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,14 @@ package cmd
import (
"backup-rsync/backup/internal"
"fmt"
"log"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

func buildConfigCommand() *cobra.Command {
var configCmd = &cobra.Command{
Use: "config",
Short: "Manage configuration",
Run: func(cmd *cobra.Command, args []string) {
// Implementation for the config command
fmt.Println("Config command executed")
},
}

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

out, err := yaml.Marshal(cfg)
if err != nil {
log.Fatalf("Failed to marshal resolved configuration: %v", err)
}

fmt.Printf("Resolved Configuration:\n%s\n", string(out))
fmt.Printf("Resolved Configuration:\n%s\n", cfg)
},
}

Expand Down
27 changes: 11 additions & 16 deletions backup/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
// Package cmd contains the commands for the backup-tool CLI application.
package cmd

import (
"os"

"github.com/spf13/cobra"
)

// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
func BuildRootCommand() *cobra.Command {
rootCmd := &cobra.Command{
Use: "backup-tool",
Use: "backup",
Short: "A tool for managing backups",
Long: `backup-tool is a CLI tool for managing backups and configurations.`,
Long: `backup is a CLI tool for managing backups and configurations.`,
}

rootCmd.PersistentFlags().String("config", "config.yaml", "Path to the configuration file")

rootCmd.AddCommand(buildListCommand())
rootCmd.AddCommand(buildRunCommand())
rootCmd.AddCommand(buildSimulateCommand())
rootCmd.AddCommand(buildConfigCommand())
rootCmd.AddCommand(buildCheckCoverageCommand())
rootCmd.AddCommand(
buildListCommand(),
buildRunCommand(),
buildSimulateCommand(),
buildConfigCommand(),
buildCheckCoverageCommand(),
)

err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
return rootCmd
}
37 changes: 37 additions & 0 deletions backup/cmd/test/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cmd_test

import (
"bytes"
"testing"

"backup-rsync/backup/cmd"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// integration test to verify the root command and its sub-commands are set up correctly.
func TestBuildRootCommand_HelpOutput(t *testing.T) {
rootCmd := cmd.BuildRootCommand()

// Capture the help output
buf := new(bytes.Buffer)
rootCmd.SetOut(buf)
rootCmd.SetArgs([]string{"--help"})
err := rootCmd.Execute()
require.NoError(t, err)

helpOutput := buf.String()
// Verify the help output contains expected content
assert.Contains(t, helpOutput, "backup is a CLI tool for managing backups and configurations.",
"Help output should contain the long description")
assert.Contains(t, helpOutput, "backup [command]", "Help output should contain usage")
assert.Contains(t, helpOutput, "--config string Path to the configuration file (default \"config.yaml\")",
"Help output should contain the persistent flag description")

// check each sub-command is listed
subCommands := []string{"list", "run", "simulate", "config", "check-coverage"}
for _, cmdName := range subCommands {
assert.Regexp(t, "(?m)^ "+cmdName, helpOutput, "Help output should list the sub-command: "+cmdName)
}
}
6 changes: 6 additions & 0 deletions backup/internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ var (
ErrOverlappingPath = errors.New("overlapping path detected")
)

func (cfg Config) String() string {
out, _ := yaml.Marshal(cfg)

return string(out)
}

func LoadConfig(reader io.Reader) (Config, error) {
var cfg Config

Expand Down
14 changes: 14 additions & 0 deletions backup/internal/test/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,3 +318,17 @@ func TestValidatePaths_InvalidPaths(t *testing.T) {
assert.EqualError(t, err, test.errorMessage)
})
}

func TestConfigString_ValidConfig(t *testing.T) {
cfg := internal.Config{
Sources: []internal.Path{},
Targets: []internal.Path{},
Variables: map[string]string{},
Jobs: []internal.Job{},
}

expectedOutput := "sources: []\ntargets: []\nvariables: {}\njobs: []\n"
actualOutput := cfg.String()

assert.Equal(t, expectedOutput, actualOutput)
}
8 changes: 7 additions & 1 deletion backup/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package main

import (
"backup-rsync/backup/cmd"
"os"
)

func main() {
cmd.Execute()
rootCmd := cmd.BuildRootCommand()

err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}