From cd085a4b5450ec842e6fe5f9f7bb53618f60931b Mon Sep 17 00:00:00 2001 From: Nguyen Tran Hau Date: Thu, 22 Apr 2021 17:54:17 +0700 Subject: [PATCH] feat: dry run for install, update, clean (#9) * feat: implement dry run for config install, update * chore: better wording * feat: dry run for clean unused dirs Co-authored-by: Tran Hau --- pkg/config/config_demo.go | 23 +++++++++++++++ pkg/config/config_real.go | 61 ++++++++++++++++++++++----------------- 2 files changed, 58 insertions(+), 26 deletions(-) diff --git a/pkg/config/config_demo.go b/pkg/config/config_demo.go index b288aa8..74d11bd 100644 --- a/pkg/config/config_demo.go +++ b/pkg/config/config_demo.go @@ -1,5 +1,7 @@ package config +import "fmt" + type configDemo struct { configApps } @@ -7,13 +9,34 @@ type configDemo struct { var _ Config = (*configDemo)(nil) func (cd *configDemo) Install() error { + for _, app := range cd.Apps { + for _, p := range app.Paths { + fmt.Printf("Replace %s -> %s\n", p.Internal, p.External) + } + } + return nil } func (cd *configDemo) Update() error { + for _, app := range cd.Apps { + for _, p := range app.Paths { + fmt.Printf("Replace %s -> %s\n", p.External, p.Internal) + } + } + return nil } func (cd *configDemo) Clean() error { + unusedDirs, err := getUnusedDirs(cd.Apps) + if err != nil { + return err + } + + for dir := range unusedDirs { + fmt.Printf("Remove %s\n", dir) + } + return nil } diff --git a/pkg/config/config_real.go b/pkg/config/config_real.go index 6e855ae..80af36e 100644 --- a/pkg/config/config_real.go +++ b/pkg/config/config_real.go @@ -63,12 +63,12 @@ func LoadConfig(path string) (*config, *configDemo, error) { return &cfg, &cfgDemo, nil } -// Install replace src internal dst external +// Install internal -> external func (c *config) Install() error { for _, app := range c.Apps { - 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) + for _, p := range app.Paths { + if err := copy.Replace(p.Internal, p.External); err != nil { + return fmt.Errorf("failed to replace %s -> %s: %w", p.Internal, p.External, err) } } } @@ -76,12 +76,12 @@ func (c *config) Install() error { return nil } -// Update replace src external dst internal +// Update external -> internal func (c *config) Update() error { for _, app := range c.Apps { - 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) + for _, p := range app.Paths { + if err := copy.Replace(p.External, p.Internal); err != nil { + return fmt.Errorf("failed to replace %s -> %s: %w", p.External, p.Internal, err) } } } @@ -91,25 +91,9 @@ func (c *config) Update() error { // Clean remove unused config inside config dir func (c *config) Clean() error { - files, err := os.ReadDir(configDirPath) + unusedDirs, err := getUnusedDirs(c.Apps) if err != nil { - return fmt.Errorf("failed to read dir %s: %w", configDirPath, err) - } - - // Get all dirs inside config dir - unusedDirs := make(map[string]struct{}) - for _, file := range files { - // Ignore config file - if file.Name() == configFile { - continue - } - - unusedDirs[file.Name()] = struct{}{} - } - - // Removed used dirs - for name := range c.Apps { - delete(unusedDirs, name) + return err } // Delete ununsed dirs to save some space @@ -126,3 +110,28 @@ func (c *config) Clean() error { func getConfigPath(path string) string { return filepath.Join(path, configDirPath, configFile) } + +func getUnusedDirs(apps map[string]App) (map[string]struct{}, error) { + files, err := os.ReadDir(configDirPath) + if err != nil { + return nil, fmt.Errorf("failed to read dir %s: %w", configDirPath, err) + } + + // Get all dirs inside config dir + unusedDirs := make(map[string]struct{}) + for _, file := range files { + // Ignore config file + if file.Name() == configFile { + continue + } + + unusedDirs[file.Name()] = struct{}{} + } + + // Removed used dirs + for name := range apps { + delete(unusedDirs, name) + } + + return unusedDirs, nil +}