Skip to content

Commit

Permalink
Less noisy content clean
Browse files Browse the repository at this point in the history
  • Loading branch information
krystian-panek-vmltech committed Apr 10, 2024
1 parent dd6ab79 commit b1e43e1
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions pkg/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -272,15 +279,15 @@ 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)
}

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)
}

Expand All @@ -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
}
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -457,7 +465,6 @@ func (c Manager) backupFile(path string, format string) error {
if err != nil {
return err
}
log.Infof(format, path)
return nil
}

Expand Down

0 comments on commit b1e43e1

Please sign in to comment.