diff --git a/actions/apply.go b/actions/apply.go new file mode 100644 index 0000000..b8a2641 --- /dev/null +++ b/actions/apply.go @@ -0,0 +1,31 @@ +package actions + +import ( + "github.com/eankeen/dotty/fs" +) + +// Apply (symlink) dotfiles +func Apply(dotDir string, srcDir string, destDir string) { + onFile := func(src string, dest string, rel string) { + fs.ApplyFile(src, dest, rel) + } + + onFolder := func(src string, dest string, rel string) { + fs.ApplyFolder(src, dest, rel) + } + + fs.Walk(dotDir, srcDir, destDir, onFile, onFolder) +} + +// Unapply (un-symlink) dotfiles +func Unapply(dotDir string, srcDir string, destDir string) { + onFile := func(src string, dest string, rel string) { + fs.UnapplyFile(src, dest, rel) + } + + onFolder := func(src string, dest string, rel string) { + fs.UnapplyFolder(src, dest, rel) + } + + fs.Walk(dotDir, srcDir, destDir, onFile, onFolder) +} diff --git a/actions/open.go b/actions/open.go new file mode 100644 index 0000000..667c239 --- /dev/null +++ b/actions/open.go @@ -0,0 +1,25 @@ +package actions + +import ( + "os" + "os/exec" + + "github.com/eankeen/dotty/internal/util" +) + +// OpenEditor opens a file for editing +func OpenEditor(file string) { + editor := os.Getenv("EDITOR") + program := "vim" + if editor != "" { + program = editor + } + + cmd := exec.Command(program, file) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + err := cmd.Run() + util.HandleError(err) +} diff --git a/cmd/local_edit.go b/cmd/local_edit.go index 31d96b7..b53d6d8 100644 --- a/cmd/local_edit.go +++ b/cmd/local_edit.go @@ -1,8 +1,8 @@ package cmd import ( + "github.com/eankeen/dotty/actions" "github.com/eankeen/dotty/config" - "github.com/eankeen/dotty/internal/util" "github.com/spf13/cobra" ) @@ -12,7 +12,7 @@ var localEditCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { dotDir := cmd.Flag("dot-dir").Value.String() - util.OpenEditor(config.GetLocalTomlPath(dotDir)) + actions.OpenEditor(config.GetLocalTomlPath(dotDir)) }, } diff --git a/cmd/system_apply.go b/cmd/system_apply.go index 8c63f6d..e71e8a9 100644 --- a/cmd/system_apply.go +++ b/cmd/system_apply.go @@ -4,7 +4,7 @@ import ( "os" "path/filepath" - "github.com/eankeen/dotty/fs" + "github.com/eankeen/dotty/actions" "github.com/eankeen/go-logger" "github.com/spf13/cobra" ) @@ -23,15 +23,7 @@ var systemApplyCmd = &cobra.Command{ srcDir := filepath.Join(dotDir, "system") destDir := cmd.Flag("system-dir").Value.String() - onFile := func(src string, dest string, rel string) { - fs.ApplyFile(src, dest, rel) - } - - onFolder := func(src string, dest string, rel string) { - fs.ApplyFolder(src, dest, rel) - } - - fs.Walk(dotDir, srcDir, destDir, onFile, onFolder) + actions.Apply(dotDir, srcDir, destDir) }, } diff --git a/cmd/system_edit.go b/cmd/system_edit.go index 8c32a11..235d2b4 100644 --- a/cmd/system_edit.go +++ b/cmd/system_edit.go @@ -1,8 +1,8 @@ package cmd import ( + "github.com/eankeen/dotty/actions" "github.com/eankeen/dotty/config" - "github.com/eankeen/dotty/internal/util" "github.com/spf13/cobra" ) @@ -12,7 +12,7 @@ var systemEditCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { dotDir := cmd.Flag("dot-dir").Value.String() - util.OpenEditor(config.GetSystemTomlPath(dotDir)) + actions.OpenEditor(config.GetSystemTomlPath(dotDir)) }, } diff --git a/cmd/user_apply.go b/cmd/user_apply.go index 0a9298f..514a382 100644 --- a/cmd/user_apply.go +++ b/cmd/user_apply.go @@ -3,7 +3,7 @@ package cmd import ( "path/filepath" - "github.com/eankeen/dotty/fs" + "github.com/eankeen/dotty/actions" "github.com/spf13/cobra" ) @@ -16,15 +16,7 @@ var userApplyCmd = &cobra.Command{ srcDir := filepath.Join(dotDir, "user") destDir := cmd.Flag("user-dir").Value.String() - onFile := func(src string, dest string, rel string) { - fs.ApplyFile(src, dest, rel) - } - - onFolder := func(src string, dest string, rel string) { - fs.ApplyFolder(src, dest, rel) - } - - fs.Walk(dotDir, srcDir, destDir, onFile, onFolder) + actions.Apply(dotDir, srcDir, destDir) }, } diff --git a/cmd/user_edit.go b/cmd/user_edit.go index 604dd1b..36f0654 100644 --- a/cmd/user_edit.go +++ b/cmd/user_edit.go @@ -1,8 +1,8 @@ package cmd import ( + "github.com/eankeen/dotty/actions" "github.com/eankeen/dotty/config" - "github.com/eankeen/dotty/internal/util" "github.com/spf13/cobra" ) @@ -12,7 +12,7 @@ var userEditCmd = &cobra.Command{ Run: func(cmd *cobra.Command, args []string) { dotDir := cmd.Flag("dot-dir").Value.String() - util.OpenEditor(config.GetUserTomlPath(dotDir)) + actions.OpenEditor(config.GetUserTomlPath(dotDir)) }, } diff --git a/cmd/user_unapply.go b/cmd/user_unapply.go index e31d741..ee1cdbb 100644 --- a/cmd/user_unapply.go +++ b/cmd/user_unapply.go @@ -3,7 +3,7 @@ package cmd import ( "path/filepath" - "github.com/eankeen/dotty/fs" + "github.com/eankeen/dotty/actions" "github.com/spf13/cobra" ) @@ -16,15 +16,7 @@ var userUnapplyCmd = &cobra.Command{ srcDir := filepath.Join(dotDir, "user") destDir := cmd.Flag("user-dir").Value.String() - onFile := func(src string, dest string, rel string) { - fs.UnapplyFile(src, dest, rel) - } - - onFolder := func(src string, dest string, rel string) { - fs.UnapplyFolder(src, dest, rel) - } - - fs.Walk(dotDir, srcDir, destDir, onFile, onFolder) + actions.Unapply(dotDir, srcDir, destDir) }, } diff --git a/e2e/full_test.go b/e2e/full_test.go index 2300c57..9974b08 100644 --- a/e2e/full_test.go +++ b/e2e/full_test.go @@ -7,7 +7,7 @@ import ( "testing" "time" - "github.com/eankeen/dotty/fs" + "github.com/eankeen/dotty/actions" ) func run() { @@ -28,28 +28,11 @@ func _dirname() string { } func do(dotDir string, srcDir string, destDir string) { - onFile := func(src string, dest string, rel string) { - fs.ApplyFile(src, dest, rel) - } - - onFolder := func(src string, dest string, rel string) { - fs.ApplyFolder(src, dest, rel) - } - - fs.Walk(dotDir, srcDir, destDir, onFile, onFolder) + actions.Apply(dotDir, srcDir, destDir) time.Sleep(time.Millisecond * 500) - // unlink - onFile2 := func(src string, dest string, rel string) { - fs.UnapplyFile(src, dest, rel) - } - - onFolder2 := func(src string, dest string, rel string) { - fs.UnapplyFolder(src, dest, rel) - } - - fs.Walk(dotDir, srcDir, destDir, onFile2, onFolder2) + actions.Unapply(dotDir, srcDir, destDir) } func TestFull(t *testing.T) { diff --git a/internal/util/util.go b/internal/util/util.go index 788b05d..e232c5d 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -63,39 +63,6 @@ func Contains(arr []string, str string) bool { return false } -// OpenEditor opens a file for editing -func OpenEditor(file string) { - editor := os.Getenv("EDITOR") - program := "vim" - if editor != "" { - program = editor - } - - cmd := exec.Command(program, file) - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - err := cmd.Run() - HandleError(err) -} - -func OpenPager(file string) { - pager := os.Getenv("PAGER") - program := "less" - if pager != "" { - program = pager - } - - cmd := exec.Command(program, file) - cmd.Stdin = os.Stdin - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - - err := cmd.Run() - HandleError(err) -} - // Prompt ensures that we get a valid response func Prompt(options []string, printText string, printArgs ...interface{}) string { logger.Notice(printText, printArgs...) @@ -134,3 +101,19 @@ func HandleFsError(err error) { logger.Critical("An unknown error occurred\n") log.Panicln(err) } + +func OpenPager(file string) { + pager := os.Getenv("PAGER") + program := "less" + if pager != "" { + program = pager + } + + cmd := exec.Command(program, file) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + err := cmd.Run() + HandleError(err) +}