bb/mkdir/main.go

44 lines
872 B
Go
Raw Normal View History

2019-09-28 15:11:32 +03:00
package main
import(
"os"
"flag"
"log"
)
func main() {
var (
parentFlag, verbFlag bool
2019-09-28 16:03:39 +03:00
modeArg int
2019-09-28 15:11:32 +03:00
)
2019-09-28 16:03:39 +03:00
status := 0
2019-09-28 15:11:32 +03:00
flag.BoolVar(&parentFlag, "p", false, "No error if existing, make parent as needed.")
flag.IntVar(&modeArg, "m", 0766, "Set file `mode`.")
flag.BoolVar(&verbFlag, "v", false, "Print a message for each created directory.")
if len(os.Args) < 2 {
flag.Usage()
}
flag.Parse()
mode := os.FileMode(modeArg)
var verb *log.Logger
if verbFlag {
verb = log.New(os.Stdout, os.Args[0]+": ", 0)
}
warn := log.New(os.Stderr, os.Args[0]+": ", 0)
for _, path := range flag.Args() {
var err error
if parentFlag {
err = os.MkdirAll(path, mode)
} else {
err = os.Mkdir(path, mode)
}
if err != nil {
warn.Println(err)
2019-09-28 16:03:39 +03:00
status = 1
2019-09-28 15:11:32 +03:00
} else if verbFlag {
verb.Printf("Created directory '%s'.", path)
}
}
2019-09-28 16:03:39 +03:00
os.Exit(status)
2019-09-28 15:11:32 +03:00
}