mirror of https://github.com/Marcusk19/dotctl
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"path/filepath"
|
|
|
|
"github.com/spf13/afero"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
var linkCommand = &cobra.Command {
|
|
Use: "link",
|
|
Run: runLinkCommand,
|
|
}
|
|
|
|
func init() {
|
|
RootCmd.AddCommand(linkCommand)
|
|
}
|
|
|
|
func runLinkCommand(cmd *cobra.Command, args []string) {
|
|
fs := UseFilesystem()
|
|
fmt.Println("Symlinking dotfiles...")
|
|
dotfileRoot := viper.Get("dotfile-path").(string)
|
|
entries, err := afero.ReadDir(fs, dotfileRoot)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
for _, entry := range(entries) {
|
|
configName := entry.Name()
|
|
if configName == ".git" || configName == "bender" {
|
|
continue
|
|
}
|
|
dotPath := filepath.Join(dotfileRoot, entry.Name())
|
|
|
|
configPath := viper.Get(configName).(string)
|
|
|
|
|
|
// destination needs to be removed before symlink
|
|
if(DryRun) {
|
|
log.Printf("Existing directory %s will be removed\n", configPath)
|
|
|
|
} else {
|
|
fs.RemoveAll(configPath)
|
|
}
|
|
|
|
testing := viper.Get("testing")
|
|
|
|
if(DryRun) {
|
|
log.Printf("Will link %s -> %s\n", configPath, dotPath)
|
|
} else {
|
|
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())
|
|
}
|
|
}
|
|
|
|
}
|