diff --git a/pkg/content/content.go b/pkg/content/content.go index 344fb59e..f192c738 100644 --- a/pkg/content/content.go +++ b/pkg/content/content.go @@ -76,6 +76,10 @@ func (c Manager) AfterPullFile(path string) error { } func (c Manager) CleanDir(root string) error { + if !pathx.Exists(root) { + return fmt.Errorf("cannot clean directory as it does not exist: %s", root) + } + log.Infof("cleaning directory %s", root) if err := c.cleanDotContents(root); err != nil { return err } @@ -88,13 +92,15 @@ func (c Manager) CleanDir(root string) error { if err := deleteEmptyDirs(root); err != nil { return err } + log.Infof("cleaned %s directory", root) return nil } func (c Manager) CleanFile(path string) error { if !pathx.Exists(path) { - return fmt.Errorf("file does not exist: %s", path) + return fmt.Errorf("cannot clean file as it does not exist: %s", path) } + log.Infof("cleaning file %s", path) if err := c.cleanDotContentFile(path); err != nil { return err } @@ -104,6 +110,7 @@ func (c Manager) CleanFile(path string) error { if err := deleteEmptyDirs(filepath.Dir(path)); err != nil { return err } + log.Infof("cleaned %s file", path) return nil } @@ -142,7 +149,7 @@ func (c Manager) cleanDotContentFile(path string) error { return nil } - log.Infof("cleaning file %s", path) + log.Debugf("cleaning dot content file %s", path) inputLines, err := readLines(path) if err != nil { return err @@ -241,9 +248,9 @@ func (c Manager) flattenFile(path string) error { dest := filepath.Dir(path) + ".xml" if pathx.Exists(dest) { - log.Infof("flattening file (override): %s", path) + log.Debugf("flattening file (override): %s", path) } else { - log.Infof("flattening file: %s", path) + log.Debugf("flattening file: %s", path) } return os.Rename(path, dest) } @@ -272,7 +279,7 @@ func deleteDir(dir string) error { if !pathx.Exists(dir) { return nil } - log.Infof("deleting dir %s", dir) + log.Debugf("deleting dir %s", dir) return os.RemoveAll(dir) } @@ -280,7 +287,7 @@ func deleteFile(path string, allowedFunc func() bool) error { if !pathx.Exists(path) || allowedFunc != nil && !allowedFunc() { return nil } - log.Infof("deleting file %s", path) + log.Debugf("deleting file %s", path) return os.Remove(path) } @@ -301,11 +308,10 @@ func deleteEmptyDirs(root string) error { return err } if len(entries) == 0 { + log.Debugf("deleting empty directory %s", root) if err = os.Remove(root); err != nil { return err } - log.Infof("deleting empty directory %s", root) - } return nil } @@ -314,7 +320,8 @@ func (c Manager) doParentsBackup(root string) error { return eachParentFiles(root, func(parent string) error { return eachFilesInDir(parent, func(path string) error { if !strings.HasSuffix(path, ParentsBackupSuffix) { - if err := c.backupFile(path, "doing backup of parent file: %s"); err != nil { + log.Debugf("doing backup of parent file: %s", path) + if err := c.backupFile(path); err != nil { return err } } @@ -327,7 +334,8 @@ func (c Manager) doSiblingsBackup(file string) error { dir := filepath.Dir(file) return eachFilesInDir(dir, func(path string) error { if path != file && !strings.HasSuffix(path, ParentsBackupSuffix) { - if err := c.backupFile(path, "doing backup of file: %s"); err != nil { + log.Debugf("doing backup of file: %s", path) + if err := c.backupFile(path); err != nil { return err } } @@ -359,7 +367,7 @@ func (c Manager) undoParentsBackup(root string) error { return eachFilesInDir(parent, func(path string) error { if strings.HasSuffix(path, ParentsBackupSuffix) { origin := strings.TrimSuffix(path, ParentsBackupSuffix) - log.Infof("undoing backup of parent file: %s", path) + log.Debugf("undoing backup of parent file: %s", path) return os.Rename(path, origin) } return nil @@ -372,7 +380,7 @@ func (c Manager) undoSiblingBackup(path string) error { return eachFilesInDir(dir, func(path string) error { if strings.HasSuffix(path, ParentsBackupSuffix) { origin := strings.TrimSuffix(path, ParentsBackupSuffix) - log.Infof("undoing backup of file: %s", path) + log.Debugf("undoing backup of file: %s", path) return os.Rename(path, origin) } else if strings.HasSuffix(path, ParentsBackupDirIndicator) { return deleteFile(path, nil) @@ -433,7 +441,7 @@ func writeLines(path string, lines []string) error { return err } -func (c Manager) backupFile(path string, format string) error { +func (c Manager) backupFile(path string) error { dir := filepath.Dir(path) indicator, err := os.Create(filepath.Join(dir, ParentsBackupDirIndicator)) if err != nil { @@ -457,7 +465,6 @@ func (c Manager) backupFile(path string, format string) error { if err != nil { return err } - log.Infof(format, path) return nil }