bitbucket.org/ai69/amoy@v0.2.3/dir.go (about)

     1  package amoy
     2  
     3  import (
     4  	"github.com/1set/gut/yos"
     5  	homedir "github.com/mitchellh/go-homedir"
     6  )
     7  
     8  // GetHomeDir returns the home directory of the current user, or the nested directory inside home directory if passed in as arguments.
     9  func GetHomeDir(dirs ...string) (string, error) {
    10  	base, err := homedir.Dir()
    11  	if err != nil {
    12  		return EmptyStr, err
    13  	}
    14  	if len(dirs) == 0 {
    15  		return base, nil
    16  	}
    17  	return yos.JoinPath(base, yos.JoinPath(dirs...)), nil
    18  }
    19  
    20  // CreateHomeDir creates the nested directory inside home directory if passed in as arguments.
    21  func CreateHomeDir(dirs ...string) (string, error) {
    22  	path, err := GetHomeDir(dirs...)
    23  	if err != nil {
    24  		return EmptyStr, err
    25  	}
    26  	if err = yos.MakeDir(path); err != nil {
    27  		return EmptyStr, err
    28  	}
    29  	return path, nil
    30  }