Skip to content

Instantly share code, notes, and snippets.

@niski84
Created November 16, 2018 01:32
Show Gist options
  • Save niski84/6de03e835819eb5273a0f5426e43257f to your computer and use it in GitHub Desktop.
Save niski84/6de03e835819eb5273a0f5426e43257f to your computer and use it in GitHub Desktop.
// create DB Dirs creates dirs for postgress tabelspace(s)
// replaces tokens contained in the configMap
func CreateDBDirs(dirs []string, configMap map[string]string) (err error) {
for _, dir := range dirs {
dir = TokenReplace(dir, configMap)
_, err := os.Stat(dir)
if err != nil {
if os.IsNotExist(err) {
log.Println("creating tablespace dir:", dir)
// for windows, force \ in path
if runtime.GOOS == "windows" {
dir = strings.Replace(dir,`/`, `\`, -1 )
}
err := os.MkdirAll(dir, os.FileMode(0775))
if err != nil {
return fmt.Errorf("error creating tabelspace folders: %v ", err.Error())
}
if runtime.GOOS != "windows" {
group, err := user.Lookup("postgres")
if err != nil {
return fmt.Errorf("error looking up postgres user user info")
}
uid,_ := strconv.Atoi(group.Uid)
gid,_ := strconv.Atoi(group.Gid)
log.Printf(`Setting uid to "%v" and gid to "%v" for dir %v`, uid, gid, dir)
err = os.Chown(dir, uid, gid)
if err != nil {
log.Println(err)
}
}
} else {
return err
}
}
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment