feat: treat files, dirs in config as the same (#8)

* feat: use paths for bpth files and dirs

* refactor: rename file to path when config install/update

Co-authored-by: Tran Hau <ngtranhau@gmail.com>
main
sudo pacman -Syu 2021-04-22 17:36:19 +07:00 committed by GitHub
parent 992bba6640
commit d655736596
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 26 deletions

View File

@ -1,7 +1,7 @@
{
"apps": {
"nvim": {
"files": [
"paths": [
{
"internal": "config/nvim/init.vim",
"external": "~/.config/nvim/init.vim"

View File

@ -27,8 +27,7 @@ type configApps struct {
// Read from file
type App struct {
Files []Path `json:"files"`
Dirs []Path `json:"dirs"`
Paths []Path `json:"paths"`
}
type Path struct {
@ -64,18 +63,12 @@ func LoadConfig(path string) (*config, *configDemo, error) {
return &cfg, &cfgDemo, nil
}
// internal -> external
// Install replace src internal dst external
func (c *config) Install() error {
for _, app := range c.Apps {
for _, file := range app.Files {
if err := copy.Replace(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 := copy.Replace(dir.Internal, dir.External); err != nil {
return fmt.Errorf("failed to remove and copy from %s to %s: %w", dir.Internal, dir.External, err)
for _, path := range app.Paths {
if err := copy.Replace(path.Internal, path.External); err != nil {
return fmt.Errorf("failed to replace src %s dst %s: %w", path.Internal, path.External, err)
}
}
}
@ -83,18 +76,12 @@ func (c *config) Install() error {
return nil
}
// external -> internal
// Update replace src external dst internal
func (c *config) Update() error {
for _, app := range c.Apps {
for _, file := range app.Files {
if err := copy.Replace(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 := copy.Replace(dir.External, dir.Internal); err != nil {
return fmt.Errorf("failed to remove and copy from %s to %s: %w", dir.External, dir.Internal, err)
for _, path := range app.Paths {
if err := copy.Replace(path.External, path.Internal); err != nil {
return fmt.Errorf("failed to replace src %s dst %s: %w", path.External, path.Internal, err)
}
}
}
@ -102,15 +89,17 @@ func (c *config) Update() error {
return nil
}
// Clean remove unused config inside config dir
func (c *config) Clean() error {
files, err := os.ReadDir(configDirPath)
if err != nil {
return fmt.Errorf("failed to read dir %s: %w", configDirPath, err)
}
// get all dirs inside config dir
// Get all dirs inside config dir
unusedDirs := make(map[string]struct{})
for _, file := range files {
// Ignore config file
if file.Name() == configFile {
continue
}
@ -118,12 +107,12 @@ func (c *config) Clean() error {
unusedDirs[file.Name()] = struct{}{}
}
// removed used dirs
// Removed used dirs
for name := range c.Apps {
delete(unusedDirs, name)
}
// delete ununsed dirs to save some space
// Delete ununsed dirs to save some space
for dir := range unusedDirs {
dirPath := filepath.Join(configDirPath, dir)
if err := os.RemoveAll(dirPath); err != nil {