github.com/hoffie/larasync@v0.0.0-20151025221940-0384d2bddcef/repository/util.go (about)

     1  package repository
     2  
     3  import (
     4  	"encoding/hex"
     5  	"errors"
     6  	"os"
     7  	"path/filepath"
     8  )
     9  
    10  // GetRoot returns the repository root of the given path.
    11  func GetRoot(path string) (string, error) {
    12  	prevPath := path
    13  	for {
    14  		if isRoot(path) {
    15  			return path, nil
    16  		}
    17  		prevPath = path
    18  		path = filepath.Dir(path)
    19  		if path == prevPath {
    20  			break
    21  		}
    22  	}
    23  	return "", errors.New("unable to find repository root")
    24  }
    25  
    26  // isRoot checks whether the given path is the root of a repository.
    27  func isRoot(path string) bool {
    28  	mgmtPath := filepath.Join(path, managementDirName)
    29  	s, err := os.Stat(mgmtPath)
    30  	if err != nil {
    31  		return false
    32  	}
    33  	return s.IsDir()
    34  }
    35  
    36  // formatUUID converts a binary UUID to the readable string representation
    37  func formatUUID(uuid []byte) string {
    38  	return hex.EncodeToString(uuid)
    39  }