bb/tool/mkdir/mkdir.go

38 lines
667 B
Go
Raw Permalink Normal View History

2019-10-30 02:54:45 +03:00
package mkdir
import (
"fmt"
2019-10-30 02:54:45 +03:00
"os"
2023-03-30 10:13:49 +03:00
"path/filepath"
2024-05-15 21:07:35 +03:00
"surdeus.su/core/cli/mtool"
2019-10-30 02:54:45 +03:00
)
2023-03-24 16:54:51 +03:00
func Run(flagSet *mtool.Flags) {
2019-10-30 02:54:45 +03:00
var (
parentFlag bool
modeArg int
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`.")
2023-03-24 16:54:51 +03:00
flagSet.Parse()
args := flagSet.Args()
if len(args) == 0 {
2019-10-30 02:54:45 +03:00
flagSet.Usage()
os.Exit(1)
2019-10-30 02:54:45 +03:00
}
mode := os.FileMode(modeArg)
for _, path := range args {
var e error
2023-03-30 10:13:49 +03:00
pth := filepath.FromSlash(path)
2019-10-30 02:54:45 +03:00
if parentFlag {
e = os.MkdirAll(pth, mode)
2019-10-30 02:54:45 +03:00
} else {
e = os.Mkdir(pth, mode)
2019-10-30 02:54:45 +03:00
}
if e != nil {
2023-03-24 16:54:51 +03:00
fmt.Fprintf(os.Stderr, "%s\n", e)
2019-10-30 02:54:45 +03:00
}
}
}