Add new dir structure util to ensure correct dir permissions in a structure

This commit is contained in:
Daniel 2019-07-31 22:30:57 +02:00
parent 46b151ddfe
commit c4b9c73f41
3 changed files with 214 additions and 1 deletions

72
utils/structure_test.go Normal file
View file

@ -0,0 +1,72 @@
// +build !windows
package utils
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func ExampleDirStructure() {
// output:
// / [755]
// /repo [777]
// /repo/b [755]
// /repo/b/c [750]
// /repo/b/d [755]
// /repo/b/d/e [755]
// /repo/b/d/f [755]
// /secret [700]
basePath, err := ioutil.TempDir("", "")
if err != nil {
fmt.Println(err)
return
}
ds := NewDirStructure(basePath, 0755)
secret := ds.ChildDir("secret", 0700)
repo := ds.ChildDir("repo", 0777)
_ = repo.ChildDir("a", 0700)
b := repo.ChildDir("b", 0755)
c := b.ChildDir("c", 0750)
err = ds.Ensure()
if err != nil {
fmt.Println(err)
}
err = c.Ensure()
if err != nil {
fmt.Println(err)
}
err = secret.Ensure()
if err != nil {
fmt.Println(err)
}
err = b.EnsureRelDir("d", "e")
if err != nil {
fmt.Println(err)
}
err = b.EnsureRelPath("d/f")
if err != nil {
fmt.Println(err)
}
filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error {
if err == nil {
dir := strings.TrimPrefix(path, basePath)
if dir == "" {
dir = "/"
}
fmt.Printf("%s [%o]\n", dir, info.Mode().Perm())
}
return nil
})
}