add dry run option for link (#10)

pull/11/head
Marcus Kok 2 years ago committed by GitHub
parent 959e2ff2c9
commit d9a2b13470
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -9,15 +9,18 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func init() {
RootCmd.AddCommand(linkCommand)
}
var linkCommand = &cobra.Command { var linkCommand = &cobra.Command {
Use: "link", Use: "link",
Run: runLinkCommand, 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) { func runLinkCommand(cmd *cobra.Command, args []string) {
fs := UseFilesystem() fs := UseFilesystem()
fmt.Println("Symlinking dotfiles...") fmt.Println("Symlinking dotfiles...")
@ -33,9 +36,18 @@ func runLinkCommand(cmd *cobra.Command, args []string) {
configPath := filepath.Join(ConfigPath, entry.Name()) configPath := filepath.Join(ConfigPath, entry.Name())
// destination needs to be removed before symlink // 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 { if err != nil {
log.Fatalf("Cannot symlink %s: %s", entry.Name(), err.Error()) log.Fatalf("Cannot symlink %s: %s", entry.Name(), err.Error())
} }

Loading…
Cancel
Save