github.com/Kindred87/Obsidian@v0.0.0-20210809203756-86936424b848/directory/directory.go (about)

     1  package directory
     2  
     3  import (
     4  	"errors"
     5  	"os"
     6  	"path/filepath"
     7  )
     8  
     9  // Exe returns the path of the directory the executable is within.
    10  // Returned path does not include a trailing separator.
    11  func Exe() (string, error) {
    12  	path, err := os.Executable()
    13  	if err != nil {
    14  		return "", err
    15  	}
    16  
    17  	return filepath.Dir(path), nil
    18  }
    19  
    20  // ExeWithSep returns the path of the directory the executable is within.
    21  // Returned path includes a trailing separator.
    22  func ExeWithSep() (string, error) {
    23  	path, err := Exe()
    24  
    25  	return path + string(os.PathSeparator), err
    26  }
    27  
    28  // Spec returns the path of the specification directory.
    29  // Returned path does not include a trailing separator.
    30  func Spec() (string, error) {
    31  	path, err := Exe()
    32  	if err != nil {
    33  		return "", err
    34  	}
    35  
    36  	return path + string(os.PathSeparator) + "specification", nil
    37  }
    38  
    39  // SpecWithSep returns the path of the specification directory.
    40  // Returned path includes a trailing separator.
    41  func SpecWithSep() (string, error) {
    42  	path, err := Spec()
    43  
    44  	return path + string(os.PathSeparator), err
    45  }
    46  
    47  // EnsureSpecExists creates the specification directory if it does not exist.
    48  func EnsureSpecExists() error {
    49  	cfgDir, err := Spec()
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	if _, err := os.Stat(cfgDir); errors.Is(err, os.ErrNotExist) {
    55  		err = os.Mkdir(cfgDir, os.ModeDir)
    56  		if err != nil {
    57  			return err
    58  		}
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  // RemoveSpec removes the specification directory.
    65  func RemoveSpec() error {
    66  	dir, err := Spec()
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	return os.RemoveAll(dir)
    72  }
    73  
    74  // Datasource returns the path of the datasource specification subdirectory.
    75  // Returned path does not include a trailing separator.
    76  func Datasource() (string, error) {
    77  	path, err := Spec()
    78  	if err != nil {
    79  		return "", err
    80  	}
    81  
    82  	return path + string(os.PathSeparator) + "datasource", nil
    83  }
    84  
    85  // DatasourceWithSep returns the path of the datasource specification directory.
    86  // Returned path includes a trailing separator.
    87  func DatasourceWithSep() (string, error) {
    88  	path, err := Datasource()
    89  
    90  	return path + string(os.PathSeparator), err
    91  }
    92  
    93  // EnsureDatasourceExists creates the datasource directory, and parent specification directory,
    94  // if they do not exist.
    95  func EnsureDatasourceExists() error {
    96  	if err := EnsureSpecExists(); err != nil {
    97  		return err
    98  	}
    99  
   100  	dsDir, err := Datasource()
   101  	if err != nil {
   102  		return err
   103  	}
   104  
   105  	_, err = os.Stat(dsDir)
   106  	if errors.Is(err, os.ErrNotExist) {
   107  		err = os.Mkdir(dsDir, os.ModeDir)
   108  		if err != nil {
   109  			return err
   110  		}
   111  	}
   112  	return nil
   113  }