From 7ea68f71e317f63e96dd84f6190c32d6cb571fb6 Mon Sep 17 00:00:00 2001 From: Marcus Kok Date: Tue, 19 Mar 2024 16:57:25 -0400 Subject: [PATCH] add dry run option for link --- cmd/link.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/cmd/link.go b/cmd/link.go index 20cb3d9..6f1bb66 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -9,15 +9,18 @@ import ( "github.com/spf13/cobra" ) -func init() { - RootCmd.AddCommand(linkCommand) -} - var linkCommand = &cobra.Command { Use: "link", Run: runLinkCommand, } +var DryRun bool + +func init() { + RootCmd.AddCommand(linkCommand) + linkCommand.Flags().BoolVarP(&DryRun, "dry-run", "d", false, "Only output which symlinks will be created") +} + func runLinkCommand(cmd *cobra.Command, args []string) { fs := UseFilesystem() fmt.Println("Symlinking dotfiles...") @@ -33,9 +36,18 @@ func runLinkCommand(cmd *cobra.Command, args []string) { configPath := filepath.Join(ConfigPath, entry.Name()) // destination needs to be removed before symlink - fs.RemoveAll(configPath) + if(DryRun) { + log.Printf("Existing directory %s will be removed\n", configPath) - err = afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath) + } else { + fs.RemoveAll(configPath) + } + + if(DryRun) { + log.Printf("Will link %s -> %s\n", dotPath, configPath) + } else { + err = afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath) + } if err != nil { log.Fatalf("Cannot symlink %s: %s", entry.Name(), err.Error()) }