timefiles/lib/common.go

39 lines
1,013 B
Go

// Package lib provides routines for creating and reading data files as defined by the specification.
package lib
import (
"os"
"os/exec"
"path/filepath"
)
// dataDir holds the configured directory to read/write data from/to.
var dataDir string
// SetDataDir sets the global directory to read/write data from/to. It is typically called once at application start.
func SetDataDir(directory string) {
dataDir = directory
}
// getDataDir gets the global directory to read/write data from/to for a schema entity.
func getDataDir(entity string) (string, error) {
if dataDir != "" {
return filepath.Join(dataDir, entity), nil
}
home, homeErr := os.UserHomeDir()
if homeErr != nil {
return "", homeErr
}
return filepath.Join(home, ".protime", entity), nil
}
// newUUID creates a new UUID for an entity instance using the OS command `uuidgen`.
func newUUID() (string, error) {
out, err := exec.Command("uuidgen").Output()
if err != nil {
return "", err
}
return string(out[:len(out)-1]), nil
}