From 5105cdf5cb1a2407ffe937e5977311c1fd54d8f6 Mon Sep 17 00:00:00 2001 From: Marcus Date: Sun, 15 Dec 2024 15:38:45 -0500 Subject: [PATCH] update add command to link files automatically and deal with relative paths --- cmd/add.go | 24 +++++++++++++++++++++--- cmd/link.go | 28 ++++++++-------------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/cmd/add.go b/cmd/add.go index 2e2c38f..aa0f642 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -3,6 +3,7 @@ package cmd import ( "fmt" "log" + "os" "path/filepath" "strings" @@ -14,16 +15,19 @@ import ( ) func init() { + addCommand.Flags().BoolVar(&absolutePath, "absolute", false, "absolute path of config") RootCmd.AddCommand(addCommand) } var addCommand = &cobra.Command{ Use: "add", Short: "Adds config to be tracked by dotctl", - Long: "TODO: add longer description", // TODO add more description + Long: "will copy files passed as argument to the dotfiles directory and symlink them", // TODO add more description Run: runAddCommand, } +var absolutePath bool + func runAddCommand(cmd *cobra.Command, args []string) { fs := FileSystem @@ -35,6 +39,12 @@ func runAddCommand(cmd *cobra.Command, args []string) { } configSrc := args[0] + + if !absolutePath { + cwd, _ := os.Getwd() + configSrc = cwd + "/" + configSrc + } + dirs := strings.Split(configSrc, "/") name := dirs[len(dirs)-1] // take the last section of the path, this should be the name if name[0] == '.' { @@ -57,7 +67,7 @@ func runAddCommand(cmd *cobra.Command, args []string) { _, err := fs.Stat(dotfileDest) if err == nil { fmt.Printf("Looks like %s exists in current dotfile directory\n", dotfileDest) - fmt.Println("Do you want to overwrite it?") + fmt.Printf("Do you want to overwrite it with what is in %s?\n", configSrc) confirm := promptui.Prompt{ Label: "overwrite config", IsConfirm: true, @@ -66,12 +76,20 @@ func runAddCommand(cmd *cobra.Command, args []string) { if strings.ToUpper(overwrite) == "Y" { addConfigToDir(fs, configSrc, dotfileDest) } - fmt.Printf("Just set up %s to link to %s\n", configSrc, dotfileDest) } else { addConfigToDir(fs, configSrc, dotfileDest) } + if !DryRun { + // symlink the copied dotfile destination back to the config src + fs.RemoveAll(configSrc) + linkPaths(dotfileDest, configSrc) + } else { + fmt.Println("Files were not symlinked") + } + if !testing { + // write to the config to persist changes 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 b93d6b1..25ed8b6 100644 --- a/cmd/link.go +++ b/cmd/link.go @@ -12,14 +12,13 @@ import ( func init() { RootCmd.AddCommand(linkCommand) - linkCommand.AddCommand(listCommand) } var linkCommand = &cobra.Command{ Use: "link", Run: runLinkCommand, Short: "generate symlinks according to config", - Long: "add longer description", // TODO add longer description here + Long: "runs through all configs in the dotctl config file and links them to configured symlinks", // TODO add longer description here } func runLinkCommand(cmd *cobra.Command, args []string) { @@ -55,28 +54,17 @@ 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) - if err != nil { - log.Fatalf("Cannot symlink %s: %s\n", configName, err.Error()) - } else { - fmt.Printf("%s linked\n", configName) - } + linkPaths(dotPath, configPath) } } } } -var listCommand = &cobra.Command{ - Use: "list", - Run: runListCommand, - Short: "list configs that should be symlinked", - Long: "add longer description", // TODO add longer description here -} - -func runListCommand(cmd *cobra.Command, args []string) { - links := viper.GetStringMapString("links") - fmt.Println("Configs added:") - for configName, configPath := range links { - fmt.Printf("%s: %s\n", configName, configPath) +func linkPaths(dotPath, configPath string) { + err := afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath) + if err != nil { + log.Fatalf("Cannot symlink %s: %s\n", configPath, err.Error()) + } else { + fmt.Printf("%s linked to %s\n", configPath, dotPath) } }