2019-10-30 02:54:45 +03:00
|
|
|
package mkdir
|
|
|
|
|
|
|
|
import(
|
2020-03-07 14:14:40 +03:00
|
|
|
"fmt"
|
2019-10-30 02:54:45 +03:00
|
|
|
"os"
|
|
|
|
"flag"
|
|
|
|
)
|
|
|
|
|
2022-06-16 09:31:23 +03:00
|
|
|
func Run(args []string) {
|
2020-03-07 14:14:40 +03:00
|
|
|
arg0 := args[0]
|
|
|
|
args = args[1:]
|
2019-10-30 02:54:45 +03:00
|
|
|
var (
|
2020-03-07 14:14:40 +03:00
|
|
|
parentFlag bool
|
2019-10-30 02:54:45 +03:00
|
|
|
modeArg int
|
|
|
|
)
|
2020-03-07 14:14:40 +03:00
|
|
|
flagSet := flag.NewFlagSet(arg0, flag.ExitOnError)
|
2019-10-30 02:54:45 +03:00
|
|
|
flagSet.BoolVar(&parentFlag, "p", false, "No error if existing, make parent as needed.")
|
|
|
|
flagSet.IntVar(&modeArg, "m", 0766, "Set file `mode`.")
|
2020-03-07 14:14:40 +03:00
|
|
|
flagSet.Usage = func() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage of %s: %s [options] [files]\n", arg0, arg0)
|
|
|
|
flagSet.PrintDefaults()
|
|
|
|
}
|
|
|
|
flagSet.Parse(args)
|
|
|
|
args = flagSet.Args()
|
|
|
|
if len(args) == 0 {
|
2019-10-30 02:54:45 +03:00
|
|
|
flagSet.Usage()
|
|
|
|
}
|
|
|
|
mode := os.FileMode(modeArg)
|
2020-03-07 14:14:40 +03:00
|
|
|
for _, path := range args {
|
|
|
|
var e error
|
2019-10-30 02:54:45 +03:00
|
|
|
if parentFlag {
|
2020-03-07 14:14:40 +03:00
|
|
|
e = os.MkdirAll(path, mode)
|
2019-10-30 02:54:45 +03:00
|
|
|
} else {
|
2020-03-07 14:14:40 +03:00
|
|
|
e = os.Mkdir(path, mode)
|
2019-10-30 02:54:45 +03:00
|
|
|
}
|
2020-03-07 14:14:40 +03:00
|
|
|
if e != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
|
2019-10-30 02:54:45 +03:00
|
|
|
}
|
|
|
|
}
|
2020-03-07 14:14:40 +03:00
|
|
|
}
|