diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3a5a6f9 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +clean: + rm -r test/bender_test + rm -r tmp + +sandbox: + mkdir -p ./tmp/ + cp -r ~/.config/ ./tmp/config diff --git a/cmd/add.go b/cmd/add.go index 13546d1..7646376 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -34,16 +34,21 @@ func runAddCommand(cmd *cobra.Command, args []string) { dirs := strings.Split(configSrc, "/") name := dirs[len(dirs) - 1] viper.Set(name, configSrc) - viper.WriteConfig() + err := viper.WriteConfig() + if err != nil { + fmt.Printf("Problem updating bender config %s", err) + } + + dotfilePath := viper.Get("dotfile-path").(string) - dotfileDest := filepath.Join(DotfilePath, name) + dotfileDest := filepath.Join(dotfilePath, name) if DryRun { fmt.Printf("Will copy %s -> %s \n", configSrc, dotfileDest) return } - err := tools.CopyDir(fs, configSrc, dotfileDest) + err = tools.CopyDir(fs, configSrc, dotfileDest) if err != nil { log.Fatal(err) } diff --git a/cmd/init.go b/cmd/init.go index e95cdb9..5506815 100644 --- a/cmd/init.go +++ b/cmd/init.go @@ -68,7 +68,10 @@ func runInitCommand(cmd *cobra.Command, args []string) { panic(fmt.Errorf("Unable to create config file %w", err)) } - viper.WriteConfig() + err = viper.WriteConfig() + if err != nil { + fmt.Printf("Unable to write config on init: %s\n", err) + } if (viper.Get("testing") != "true"){ _, err = git.PlainInit(DotfilePath, false) @@ -77,5 +80,5 @@ func runInitCommand(cmd *cobra.Command, args []string) { } } - fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository\n") + fmt.Fprintf(cmd.OutOrStdout(), "Successfully created dotfiles repository at %s\n", DotfilePath) } diff --git a/cmd/link.go b/cmd/link.go index 8f5a5db..2ccc513 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -7,6 +7,7 @@ import ( "github.com/spf13/afero" "github.com/spf13/cobra" + "github.com/spf13/viper" ) var linkCommand = &cobra.Command { @@ -21,16 +22,20 @@ func init() { func runLinkCommand(cmd *cobra.Command, args []string) { fs := UseFilesystem() fmt.Println("Symlinking dotfiles...") - entries, err := afero.ReadDir(fs, DotfilePath) + dotfileRoot := viper.Get("dotfile-path").(string) + entries, err := afero.ReadDir(fs, dotfileRoot) if err != nil { log.Fatal(err) } for _, entry := range(entries) { - if entry.Name() == ".git" { + configName := entry.Name() + if configName == ".git" || configName == "bender" { continue } - dotPath := filepath.Join(DotfilePath, entry.Name()) - configPath := filepath.Join(ConfigPath, entry.Name()) + dotPath := filepath.Join(dotfileRoot, entry.Name()) + + configPath := viper.Get(configName).(string) + // destination needs to be removed before symlink if(DryRun) { @@ -40,10 +45,16 @@ func runLinkCommand(cmd *cobra.Command, args []string) { fs.RemoveAll(configPath) } + testing := viper.Get("testing") + if(DryRun) { - log.Printf("Will link %s -> %s\n", dotPath, configPath) + log.Printf("Will link %s -> %s\n", configPath, dotPath) } else { - err = afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath) + if(testing == "true") { + fmt.Fprintf(cmd.OutOrStdout(), "%s,%s", configPath, dotPath) + } else { + err = afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath) + } } if err != nil { log.Fatalf("Cannot symlink %s: %s", entry.Name(), err.Error()) diff --git a/cmd/root.go b/cmd/root.go index ec99e4d..9564e06 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -66,10 +66,11 @@ func init() { viper.SetConfigName("config") viper.SetConfigType("yaml") - viper.AddConfigPath(filepath.Join(defaultDotPath, "bender")) - viper.AddConfigPath("./bender") + viper.AddConfigPath("./tmp/dotfiles/bender") + viper.AddConfigPath(filepath.Join(DotfilePath, "bender")) err := viper.ReadInConfig() + if err != nil { fmt.Println("No config detected. You can generate one by using 'bender init'") } diff --git a/test/init_test.go b/test/init_test.go index e8b0c51..736fe07 100644 --- a/test/init_test.go +++ b/test/init_test.go @@ -20,13 +20,13 @@ func TestInitCommand(t *testing.T) { bender.SetOut(actual) bender.SetErr(actual) - bender.SetArgs([]string{"init", "--dotfile-path=bender_test/.dotfiles"}) + bender.SetArgs([]string{"init", "--dotfile-path=bender_test/dotfiles"}) bender.Execute() homedir := "bender_test/" - _, err := afero.ReadFile(fs, filepath.Join(homedir, ".dotfiles/bender/config")) + _, err := afero.ReadFile(fs, filepath.Join(homedir, "dotfiles/bender/config")) if err != nil { t.Error(err.Error()) }