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.
dotctl/cmd/link.go

57 lines
1.2 KiB
Go

package cmd
import (
"fmt"
"log"
"path/filepath"
"github.com/spf13/afero"
"github.com/spf13/cobra"
)
var linkCommand = &cobra.Command {
Use: "link",
Run: runLinkCommand,
}
var DryRun bool
func init() {
RootCmd.AddCommand(linkCommand)
linkCommand.Flags().BoolVarP(&DryRun, "dry-run", "d", false, "Only output which symlinks will be created")
}
func runLinkCommand(cmd *cobra.Command, args []string) {
fs := UseFilesystem()
fmt.Println("Symlinking dotfiles...")
entries, err := afero.ReadDir(fs, DotfilePath)
if err != nil {
log.Fatal(err)
}
for _, entry := range(entries) {
if entry.Name() == ".git" {
continue
}
dotPath := filepath.Join(DotfilePath, entry.Name())
configPath := filepath.Join(ConfigPath, entry.Name())
// destination needs to be removed before symlink
if(DryRun) {
log.Printf("Existing directory %s will be removed\n", configPath)
} else {
fs.RemoveAll(configPath)
}
if(DryRun) {
log.Printf("Will link %s -> %s\n", dotPath, configPath)
} else {
err = afero.OsFs.SymlinkIfPossible(afero.OsFs{}, dotPath, configPath)
}
if err != nil {
log.Fatalf("Cannot symlink %s: %s", entry.Name(), err.Error())
}
}
}