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

     1  package repository
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  
     7  	"github.com/hoffie/larasync/repository/content"
     8  )
     9  
    10  // newManagementDirectory returns the struct which can be used
    11  // to interact with generic management directory functionality.
    12  func newManagementDirectory(r *Repository) *managementDirectory {
    13  	return &managementDirectory{
    14  		r: r,
    15  	}
    16  }
    17  
    18  type managementDirectory struct {
    19  	r *Repository
    20  }
    21  
    22  // subPathFor returns the full path for the given subdirectory.
    23  func (md *managementDirectory) subPathFor(name string) string {
    24  	return filepath.Join(md.getDir(), name)
    25  }
    26  
    27  // getDir returns the directory of this management directory.
    28  func (md *managementDirectory) getDir() string {
    29  	return filepath.Join(md.r.Path, managementDirName)
    30  }
    31  
    32  // create ensures that this repository's management
    33  // directory exists.
    34  func (md *managementDirectory) create() error {
    35  	err := os.Mkdir(md.getDir(), defaultDirPerms)
    36  	if err != nil && !os.IsExist(err) {
    37  		return err
    38  	}
    39  	err = md.afterRootInitialization()
    40  
    41  	storages := []*content.FileStorage{
    42  		content.NewFileStorage(md.subPathFor(authorizationsDirName)),
    43  		content.NewFileStorage(md.subPathFor(nibsDirName)),
    44  		content.NewFileStorage(md.subPathFor(transactionsDirName)),
    45  		content.NewFileStorage(md.subPathFor(objectsDirName)),
    46  		content.NewFileStorage(md.subPathFor(keysDirName)),
    47  	}
    48  
    49  	for _, fileStorage := range storages {
    50  		err = fileStorage.CreateDir()
    51  		if err != nil {
    52  			return err
    53  		}
    54  	}
    55  
    56  	return nil
    57  }