diff --git a/data/data.json b/data/data.json index 0702d58..b333175 100644 --- a/data/data.json +++ b/data/data.json @@ -1,7 +1,7 @@ { "apps": { "nvim": { - "files": [ + "paths": [ { "internal": "config/nvim/init.vim", "external": "~/.config/nvim/init.vim" diff --git a/pkg/config/config_real.go b/pkg/config/config_real.go index 16a6793..6e855ae 100644 --- a/pkg/config/config_real.go +++ b/pkg/config/config_real.go @@ -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 {