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
5 changes: 4 additions & 1 deletion backup/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,12 @@ func listCommands(cfg internal.Config) {
}
}

func AddBackupCommands(rootCmd *cobra.Command, configPath string) {
func AddBackupCommands(rootCmd *cobra.Command) {
var runCmd = &cobra.Command{
Use: "run",
Short: "Execute the sync jobs",
Run: func(cmd *cobra.Command, args []string) {
configPath, _ := cmd.Flags().GetString("config")
cfg := internal.LoadResolvedConfig(configPath)
executeSyncJobs(cfg, false)
},
Expand All @@ -69,6 +70,7 @@ func AddBackupCommands(rootCmd *cobra.Command, configPath string) {
Use: "simulate",
Short: "Simulate the sync jobs",
Run: func(cmd *cobra.Command, args []string) {
configPath, _ := cmd.Flags().GetString("config")
cfg := internal.LoadResolvedConfig(configPath)
executeSyncJobs(cfg, true)
},
Expand All @@ -78,6 +80,7 @@ func AddBackupCommands(rootCmd *cobra.Command, configPath string) {
Use: "list",
Short: "List the commands that will be executed",
Run: func(cmd *cobra.Command, args []string) {
configPath, _ := cmd.Flags().GetString("config")
cfg := internal.LoadResolvedConfig(configPath)
listCommands(cfg)
},
Expand Down
3 changes: 2 additions & 1 deletion backup/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"github.com/spf13/cobra"
)

func AddCheckCommands(rootCmd *cobra.Command, configPath string) {
func AddCheckCommands(rootCmd *cobra.Command) {
var fs = afero.NewOsFs()

var checkCmd = &cobra.Command{
Use: "check-coverage",
Short: "Check path coverage",
Run: func(cmd *cobra.Command, args []string) {
configPath, _ := cmd.Flags().GetString("config")
cfg := internal.LoadResolvedConfig(configPath)
uncoveredPaths := internal.ListUncoveredPaths(fs, cfg)

Expand Down
4 changes: 3 additions & 1 deletion backup/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// AddConfigCommands binds the config command and its subcommands to the root command.
func AddConfigCommands(rootCmd *cobra.Command, configPath string) {
func AddConfigCommands(rootCmd *cobra.Command) {
// configCmd represents the config command.
var configCmd = &cobra.Command{
Use: "config",
Expand All @@ -26,6 +26,7 @@ func AddConfigCommands(rootCmd *cobra.Command, configPath string) {
Use: "show",
Short: "Show resolved configuration",
Run: func(cmd *cobra.Command, args []string) {
configPath, _ := cmd.Flags().GetString("config")
cfg := internal.LoadResolvedConfig(configPath)

out, err := yaml.Marshal(cfg)
Expand All @@ -42,6 +43,7 @@ func AddConfigCommands(rootCmd *cobra.Command, configPath string) {
Use: "validate",
Short: "Validate configuration",
Run: func(cmd *cobra.Command, args []string) {
configPath, _ := cmd.Flags().GetString("config")
internal.LoadResolvedConfig(configPath)
fmt.Println("Configuration is valid.")
},
Expand Down
18 changes: 5 additions & 13 deletions backup/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,19 @@ import (

// Execute adds all child commands to the root command and sets flags appropriately.
func Execute() {
var configPath string

rootCmd := &cobra.Command{
Use: "backup-tool",
Short: "A tool for managing backups",
Long: `backup-tool is a CLI tool for managing backups and configurations.`,
}

rootCmd.PersistentFlags().StringVar(&configPath, "config", "config.yaml", "Path to the configuration file")

// Parse flags before adding commands to ensure configPath is available.
err := rootCmd.ParseFlags(os.Args[1:])
if err != nil {
os.Exit(1)
}
rootCmd.PersistentFlags().String("config", "config.yaml", "Path to the configuration file")

AddConfigCommands(rootCmd, configPath)
AddBackupCommands(rootCmd, configPath)
AddCheckCommands(rootCmd, configPath)
AddConfigCommands(rootCmd)
AddBackupCommands(rootCmd)
AddCheckCommands(rootCmd)

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