update add command to link files automatically and deal with relative paths

pull/42/head
Marcus 1 year ago
parent a135494bbc
commit 5105cdf5cb

@ -3,6 +3,7 @@ package cmd
import ( import (
"fmt" "fmt"
"log" "log"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -14,16 +15,19 @@ import (
) )
func init() { func init() {
addCommand.Flags().BoolVar(&absolutePath, "absolute", false, "absolute path of config")
RootCmd.AddCommand(addCommand) RootCmd.AddCommand(addCommand)
} }
var addCommand = &cobra.Command{ var addCommand = &cobra.Command{
Use: "add", Use: "add",
Short: "Adds config to be tracked by dotctl", 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, Run: runAddCommand,
} }
var absolutePath bool
func runAddCommand(cmd *cobra.Command, args []string) { func runAddCommand(cmd *cobra.Command, args []string) {
fs := FileSystem fs := FileSystem
@ -35,6 +39,12 @@ func runAddCommand(cmd *cobra.Command, args []string) {
} }
configSrc := args[0] configSrc := args[0]
if !absolutePath {
cwd, _ := os.Getwd()
configSrc = cwd + "/" + configSrc
}
dirs := strings.Split(configSrc, "/") dirs := strings.Split(configSrc, "/")
name := dirs[len(dirs)-1] // take the last section of the path, this should be the name name := dirs[len(dirs)-1] // take the last section of the path, this should be the name
if name[0] == '.' { if name[0] == '.' {
@ -57,7 +67,7 @@ func runAddCommand(cmd *cobra.Command, args []string) {
_, err := fs.Stat(dotfileDest) _, err := fs.Stat(dotfileDest)
if err == nil { if err == nil {
fmt.Printf("Looks like %s exists in current dotfile directory\n", dotfileDest) 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{ confirm := promptui.Prompt{
Label: "overwrite config", Label: "overwrite config",
IsConfirm: true, IsConfirm: true,
@ -66,12 +76,20 @@ func runAddCommand(cmd *cobra.Command, args []string) {
if strings.ToUpper(overwrite) == "Y" { if strings.ToUpper(overwrite) == "Y" {
addConfigToDir(fs, configSrc, dotfileDest) addConfigToDir(fs, configSrc, dotfileDest)
} }
fmt.Printf("Just set up %s to link to %s\n", configSrc, dotfileDest)
} else { } else {
addConfigToDir(fs, configSrc, dotfileDest) 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 { if !testing {
// write to the config to persist changes
err := viper.WriteConfig() err := viper.WriteConfig()
if err != nil { if err != nil {
fmt.Printf("Problem updating dotctl config %s", err) fmt.Printf("Problem updating dotctl config %s", err)

@ -12,14 +12,13 @@ import (
func init() { func init() {
RootCmd.AddCommand(linkCommand) RootCmd.AddCommand(linkCommand)
linkCommand.AddCommand(listCommand)
} }
var linkCommand = &cobra.Command{ var linkCommand = &cobra.Command{
Use: "link", Use: "link",
Run: runLinkCommand, Run: runLinkCommand,
Short: "generate symlinks according to config", 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) { func runLinkCommand(cmd *cobra.Command, args []string) {
@ -55,28 +54,17 @@ func runLinkCommand(cmd *cobra.Command, args []string) {
if testing == true { if testing == true {
fmt.Fprintf(cmd.OutOrStdout(), "%s,%s", configPath, dotPath) fmt.Fprintf(cmd.OutOrStdout(), "%s,%s", configPath, dotPath)
} else { } else {
err := afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath) linkPaths(dotPath, configPath)
if err != nil {
log.Fatalf("Cannot symlink %s: %s\n", configName, err.Error())
} else {
fmt.Printf("%s linked\n", configName)
}
} }
} }
} }
} }
var listCommand = &cobra.Command{ func linkPaths(dotPath, configPath string) {
Use: "list", err := afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath)
Run: runListCommand, if err != nil {
Short: "list configs that should be symlinked", log.Fatalf("Cannot symlink %s: %s\n", configPath, err.Error())
Long: "add longer description", // TODO add longer description here } else {
} fmt.Printf("%s linked to %s\n", configPath, dotPath)
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)
} }
} }

Loading…
Cancel
Save