my website's cms.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142package main
import ( "errors" "fmt" "log" "os")
// ensure a directory exists at a given path. Output toggles whether or not it tells the user a new directory will be created.func EnsureDir(path string, output bool) []os.DirEntry { dir, err := os.ReadDir(path) if err != nil { if !errors.Is(err, os.ErrNotExist) { log.Panic(err) }
fmt.Printf("The '%s' directory does not exist yet. Creating it for you... ", path) err := os.Mkdir(path, os.ModePerm) if err != nil { log.Panic(err) } fmt.Print("Done!\n")
dir, err = os.ReadDir(path) if err != nil { log.Panic(err) // it's over } } return dir}
// removes all files and subdirectories in the given path.// if the directory does not exist, it creates a new one.func ClearDir(path string) { _, err := os.ReadDir(path) if err == nil { // error means "doesn't exist". Permissions shouldnt be an issue. os.RemoveAll(path) } EnsureDir(path, false)}