github.com/safing/portbase@v0.19.5/utils/structure_test.go (about) 1 //go:build !windows 2 3 package utils 4 5 import ( 6 "fmt" 7 "os" 8 "path/filepath" 9 "strings" 10 ) 11 12 func ExampleDirStructure() { 13 // output: 14 // / [755] 15 // /repo [777] 16 // /repo/b [707] 17 // /repo/b/c [750] 18 // /repo/b/d [707] 19 // /repo/b/d/e [707] 20 // /repo/b/d/f [707] 21 // /repo/b/d/f/g [707] 22 // /repo/b/d/f/g/h [707] 23 // /secret [700] 24 25 basePath, err := os.MkdirTemp("", "") 26 if err != nil { 27 fmt.Println(err) 28 return 29 } 30 31 ds := NewDirStructure(basePath, 0o0755) 32 secret := ds.ChildDir("secret", 0o0700) 33 repo := ds.ChildDir("repo", 0o0777) 34 _ = repo.ChildDir("a", 0o0700) 35 b := repo.ChildDir("b", 0o0707) 36 c := b.ChildDir("c", 0o0750) 37 38 err = ds.Ensure() 39 if err != nil { 40 fmt.Println(err) 41 } 42 43 err = c.Ensure() 44 if err != nil { 45 fmt.Println(err) 46 } 47 48 err = secret.Ensure() 49 if err != nil { 50 fmt.Println(err) 51 } 52 53 err = b.EnsureRelDir("d", "e") 54 if err != nil { 55 fmt.Println(err) 56 } 57 58 err = b.EnsureRelPath("d/f/g/h") 59 if err != nil { 60 fmt.Println(err) 61 } 62 63 _ = filepath.Walk(basePath, func(path string, info os.FileInfo, err error) error { 64 if err == nil { 65 dir := strings.TrimPrefix(path, basePath) 66 if dir == "" { 67 dir = "/" 68 } 69 fmt.Printf("%s [%o]\n", dir, info.Mode().Perm()) 70 } 71 return nil 72 }) 73 }