Skip to content
This repository has been archived by the owner on Jan 22, 2023. It is now read-only.

Commit

Permalink
feat: abstract the fs.Walk to an Apply/Unapply action
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperupcall committed Nov 7, 2020
1 parent c33383e commit 3288fe9
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 89 deletions.
31 changes: 31 additions & 0 deletions actions/apply.go
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions actions/open.go
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 2 additions & 2 deletions cmd/local_edit.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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))
},
}

Expand Down
12 changes: 2 additions & 10 deletions cmd/system_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/system_edit.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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))
},
}

Expand Down
12 changes: 2 additions & 10 deletions cmd/user_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"path/filepath"

"github.com/eankeen/dotty/fs"
"github.com/eankeen/dotty/actions"
"github.com/spf13/cobra"
)

Expand All @@ -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)
},
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/user_edit.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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))
},
}

Expand Down
12 changes: 2 additions & 10 deletions cmd/user_unapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"path/filepath"

"github.com/eankeen/dotty/fs"
"github.com/eankeen/dotty/actions"
"github.com/spf13/cobra"
)

Expand All @@ -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)
},
}

Expand Down
23 changes: 3 additions & 20 deletions e2e/full_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"
"time"

"github.com/eankeen/dotty/fs"
"github.com/eankeen/dotty/actions"
)

func run() {
Expand All @@ -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) {
Expand Down
49 changes: 16 additions & 33 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down Expand Up @@ -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)
}

0 comments on commit 3288fe9

Please sign in to comment.