Skip to content

Commit

Permalink
Add Flush feature and deprecate Recreate
Browse files Browse the repository at this point in the history
  • Loading branch information
jotadrilo committed Jun 25, 2019
1 parent ebc6cca commit 1cd7475
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## Release 0.0.14 (2019-06-25)

- Add Flush feature to remove entries in the configuration for absent folders

## Release 0.0.13 (2019-06-24)

- Fix alias not to depend on others
Expand Down
17 changes: 6 additions & 11 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"sort"

"github.com/juju/errors"
"github.com/mmikulicic/multierror"
)

var (
Expand Down Expand Up @@ -210,20 +209,16 @@ func (config *Config) RemoveFolder(fn int) error {
return nil
}

// Iterate over the folder entries and create them again.
func (config *Config) Recreate() (errs error) {
// Flush iterates over the folder entries and remove them from the configuration if they are not present anymore.
func (config *Config) Flush() (errs error) {
var folders = []Folder{}
for _, fol := range config.Folders {
if _, err := os.Stat(fol.Path); err != nil {
if os.IsNotExist(err) {
if err := createFolder(fol); err != nil {
errs = multierror.Append(errs, errors.Errorf("failed to create folder %s: %+v", fol.Name, err))
}
}
} else {
errs = multierror.Append(errs, errors.Errorf("failed to recreate folder %s: %+v", fol.Name, err))
if _, err := os.Stat(fol.Path); err == nil {
folders = append(folders, fol)
}
}

config.Folders = folders
return errors.Trace(errs)
}

Expand Down
13 changes: 6 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,14 @@ func run() error {
return nil
}

// There possibily was a restart and the `tmp` folder was removed.
if err := config.Flush(); err != nil {
return err
}

// Try to change to the target directory
if err := os.Chdir(config.Latest.Path); err != nil {
// There possibily was a restart that removed the `tmp` folder.
if err := config.Recreate(); err != nil {
return err
}
if err := os.Chdir(config.Latest.Path); err != nil {
return errors.Errorf("failed to change to directory: %+v", err)
}
return errors.Errorf("failed to change to directory: %+v", err)
}

// Dump all the changes in the done configuration
Expand Down

0 comments on commit 1cd7475

Please sign in to comment.