refactor: use replaceFile and replaceDir

main
Tran Hau 2021-01-20 14:17:47 +07:00
parent fc16a62de1
commit c343fcb0cb
2 changed files with 66 additions and 49 deletions

View File

@ -6,16 +6,11 @@ import (
"fmt"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"github.com/haunt98/copy-go"
)
const (
configFilePath = "config/config.json"
homeSymbol = '~'
)
type Config struct {
@ -33,8 +28,6 @@ type Path struct {
External string `json:"external"`
}
type copyFn func(from, to string) error
// Load config from file
func LoadConfig(path string) (result Config, err error) {
configPath := getConfigPath(path)
@ -66,13 +59,13 @@ func LoadConfig(path string) (result Config, err error) {
func (c *Config) Install() error {
for _, app := range c.Apps {
for _, file := range app.Files {
if err := removeAndCopy(file.Internal, file.External, copy.CopyFile); err != nil {
if err := replaceFile(file.Internal, file.External); err != nil {
return fmt.Errorf("failed to remove and copy from %s to %s: %w", file.Internal, file.External, err)
}
}
for _, dir := range app.Dirs {
if err := removeAndCopy(dir.Internal, dir.External, copy.CopyDir); err != nil {
if err := replaceDir(dir.Internal, dir.External); err != nil {
return fmt.Errorf("failed to remove and copy from %s to %s: %w", dir.Internal, dir.External, err)
}
}
@ -85,13 +78,13 @@ func (c *Config) Install() error {
func (c *Config) Update() error {
for _, app := range c.Apps {
for _, file := range app.Files {
if err := removeAndCopy(file.External, file.Internal, copy.CopyFile); err != nil {
if err := replaceFile(file.External, file.Internal); err != nil {
return fmt.Errorf("failed to remove and copy from %s to %s: %w", file.External, file.Internal, err)
}
}
for _, dir := range app.Dirs {
if err := removeAndCopy(dir.External, dir.Internal, copy.CopyDir); err != nil {
if err := replaceDir(dir.External, dir.Internal); err != nil {
return fmt.Errorf("failed to remove and copy from %s to %s: %w", dir.External, dir.Internal, err)
}
}
@ -107,41 +100,3 @@ func (c *Config) Clean() error {
func getConfigPath(path string) string {
return filepath.Join(path, configFilePath)
}
func removeAndCopy(from, to string, fn copyFn) error {
newFrom, err := replaceHomeSymbol(from)
if err != nil {
return fmt.Errorf("failed to replace home symbol %s: %w", from, err)
}
newTo, err := replaceHomeSymbol(to)
if err != nil {
return fmt.Errorf("failed to replace home symbol %s: %w", to, err)
}
if err := os.RemoveAll(newTo); err != nil {
return fmt.Errorf("failed to remove %s: %w", newTo, err)
}
if err := fn(newFrom, newTo); err != nil {
return fmt.Errorf("failed to copy from %s to %s: %w", newFrom, newTo, err)
}
return nil
}
// replace ~
// https://stackoverflow.com/a/17609894
func replaceHomeSymbol(path string) (string, error) {
if path == "" || path[0] != homeSymbol {
return path, nil
}
currentUser, err := user.Current()
if err != nil {
return "", err
}
newPath := filepath.Join(currentUser.HomeDir, path[1:])
return newPath, nil
}

62
file.go Normal file
View File

@ -0,0 +1,62 @@
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"github.com/haunt98/copy-go"
)
const (
homeSymbol = '~'
)
type copyFn func(from, to string) error
func replaceFile(from, to string) error {
return replace(from, to, copy.CopyFile)
}
func replaceDir(from, to string) error {
return replace(from, to, copy.CopyDir)
}
func replace(from, to string, fn copyFn) error {
newFrom, err := replaceHomeSymbol(from)
if err != nil {
return fmt.Errorf("failed to replace home symbol %s: %w", from, err)
}
newTo, err := replaceHomeSymbol(to)
if err != nil {
return fmt.Errorf("failed to replace home symbol %s: %w", to, err)
}
if err := os.RemoveAll(newTo); err != nil {
return fmt.Errorf("failed to remove %s: %w", newTo, err)
}
if err := fn(newFrom, newTo); err != nil {
return fmt.Errorf("failed to copy from %s to %s: %w", newFrom, newTo, err)
}
return nil
}
// replace ~
// https://stackoverflow.com/a/17609894
func replaceHomeSymbol(path string) (string, error) {
if path == "" || path[0] != homeSymbol {
return path, nil
}
currentUser, err := user.Current()
if err != nil {
return "", err
}
newPath := filepath.Join(currentUser.HomeDir, path[1:])
return newPath, nil
}