From 60f33f14da3e04d9bc57cc3c52c817941931a13e Mon Sep 17 00:00:00 2001 From: Marcus Kok Date: Wed, 3 Apr 2024 16:10:47 -0400 Subject: [PATCH] make links a map in config --- cmd/add.go | 7 +++++-- cmd/link.go | 26 +++++++++++++------------- cmd/root.go | 4 +++- 3 files changed, 21 insertions(+), 16 deletions(-) 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 {