github.com/jlmeeker/kismatic@v1.10.1-0.20180612190640-57f9005a1f1a/pkg/util/file.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  // BackupDirectory checks for existence of the $sourceDir and backs it up to backupDir
     9  func BackupDirectory(sourceDir string, backupDir string) (bool, error) {
    10  	_, err := os.Stat(sourceDir)
    11  	var backedup bool
    12  	// Directory exists
    13  	if err == nil {
    14  		if err = os.Rename(sourceDir, backupDir); err != nil {
    15  			return false, fmt.Errorf("Could not back up %q directory: %v", sourceDir, err)
    16  		}
    17  		return true, nil
    18  	} else if !os.IsNotExist(err) { // Directory does not exist but got some other error
    19  		return backedup, fmt.Errorf("Could not determine if %q directory exists: %v", sourceDir, err)
    20  	}
    21  	// Directory does not already exist, nothing to do
    22  	return backedup, nil
    23  }