diff --git a/cmd/add.go b/cmd/add.go index cf2866b..1a62ef4 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -32,8 +32,11 @@ func runAddCommand(cmd *cobra.Command, args []string) { configSrc := args[0] dirs := strings.Split(configSrc, "/") - name := dirs[len(dirs) - 1] - viper.Set(name, configSrc) + name := dirs[len(dirs) - 1] // take the last section of the path, this should be the name + + links := viper.GetStringMap("links") + links[name] = configSrc + viper.Set("links", links) err := viper.WriteConfig() if err != nil { fmt.Printf("Problem updating dotctl config %s", err) diff --git a/cmd/link.go b/cmd/link.go index 3ac3a6b..7873001 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -25,20 +25,17 @@ func runLinkCommand(cmd *cobra.Command, args []string) { fs := FileSystem fmt.Println("Symlinking dotfiles...") dotfileRoot := viper.Get("dotfile-path").(string) - entries, err := afero.ReadDir(fs, dotfileRoot) - if err != nil { - log.Fatalf("Could not read dotfiles directory: %s\n",err) - } - for _, entry := range(entries) { - configName := entry.Name() + + links := viper.GetStringMapString("links") + + for configName, configPath := range links { if configName == ".git" || configName == "dotctl" { continue } - dotPath := filepath.Join(dotfileRoot, entry.Name()) + dotPath := filepath.Join(dotfileRoot, configName) - configPath := viper.GetString(configName) if configPath == ""{ - fmt.Fprintf(cmd.OutOrStdout(), "Warning: could not find config for %s\n", entry.Name()) + fmt.Fprintf(cmd.OutOrStdout(), "Warning: could not find config for %s\n", configName) } @@ -58,12 +55,15 @@ func runLinkCommand(cmd *cobra.Command, args []string) { if(testing == true) { fmt.Fprintf(cmd.OutOrStdout(), "%s,%s", configPath, dotPath) } else { - err = afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath) + err := afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath) + if err != nil { + log.Fatalf("Cannot symlink %s: %s\n", configName, err.Error()) + } else { + fmt.Printf("%s linked\n", configName) + } } } - if err != nil { - log.Fatalf("Cannot symlink %s: %s", entry.Name(), err.Error()) - } } + } diff --git a/cmd/root.go b/cmd/root.go index 98ae152..cb11d6c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -43,7 +43,7 @@ func init() { // Cobra also supports local flags, which will only run // when this action is called directly. - defaultDotPath := os.Getenv("HOME") + "/.dotfiles/" + defaultDotPath := os.Getenv("HOME") + "/dotfiles/" defaultConfPath := os.Getenv("HOME") + "/.config/" RootCmd.PersistentFlags().StringVar( &DotfilePath, @@ -69,6 +69,8 @@ func init() { viper.AddConfigPath("./tmp/dotfiles/dotctl") viper.AddConfigPath(filepath.Join(DotfilePath, "dotctl")) + viper.SetDefault("links", map[string]string{}) + err := viper.ReadInConfig() if err != nil { diff --git a/test/link_test.go b/test/link_test.go index 0fea3ac..b5a754c 100644 --- a/test/link_test.go +++ b/test/link_test.go @@ -26,7 +26,7 @@ func TestLinkCommand(t *testing.T) { homedir := os.Getenv("HOME") someconfig := filepath.Join(homedir, ".config/someconfig/") - somedot := filepath.Join(homedir, ".dotfiles/someconfig/") + somedot := filepath.Join(homedir, "dotfiles/someconfig/") expected := fmt.Sprintf("%s,%s", someconfig, somedot) @@ -36,16 +36,17 @@ func TestLinkCommand(t *testing.T) { } func setUpTesting() { + viper.Set("testing", true) + fs := cmd.FileSystem homedir := os.Getenv("HOME") - fs.MkdirAll(filepath.Join(homedir, ".dotfiles/dotctl"), 0755) - fs.Create(filepath.Join(homedir, ".dotfiles/dotctl/config")) - fs.MkdirAll(filepath.Join(homedir, ".dotfiles/someconfig/"), 0755) + fakeLinks := map[string]string {"someconfig": filepath.Join(homedir, ".config/someconfig")} + viper.Set("links", fakeLinks) + fs.MkdirAll(filepath.Join(homedir, "dotfiles/dotctl"), 0755) + fs.Create(filepath.Join(homedir, "dotfiles/dotctl/config")) - viper.Set("dotfile-path", filepath.Join(homedir, ".dotfiles")) + viper.Set("dotfile-path", filepath.Join(homedir, "dotfiles")) viper.Set("someconfig", filepath.Join(homedir, ".config/someconfig/")) - viper.Set("testing", true) - } func tearDownTesting() {