2019-10-30 02:54:45 +03:00
|
|
|
package mkdir
|
|
|
|
|
2023-03-08 22:26:23 +03:00
|
|
|
import (
|
2020-03-07 14:14:40 +03:00
|
|
|
"fmt"
|
2019-10-30 02:54:45 +03:00
|
|
|
"os"
|
2023-03-08 22:26:23 +03:00
|
|
|
|
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 (
|
2020-03-07 14:14:40 +03:00
|
|
|
parentFlag bool
|
2023-03-08 22:26:23 +03:00
|
|
|
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()
|
2020-03-07 14:14:40 +03:00
|
|
|
if len(args) == 0 {
|
2019-10-30 02:54:45 +03:00
|
|
|
flagSet.Usage()
|
2023-03-08 22:26:23 +03:00
|
|
|
os.Exit(1)
|
2019-10-30 02:54:45 +03:00
|
|
|
}
|
|
|
|
mode := os.FileMode(modeArg)
|
2023-03-08 22:26:23 +03:00
|
|
|
for _, path := range args {
|
2020-03-07 14:14:40 +03:00
|
|
|
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 {
|
2023-03-08 22:26:23 +03:00
|
|
|
e = os.MkdirAll(pth, mode)
|
2019-10-30 02:54:45 +03:00
|
|
|
} else {
|
2023-03-08 22:26:23 +03:00
|
|
|
e = os.Mkdir(pth, mode)
|
2019-10-30 02:54:45 +03:00
|
|
|
}
|
2020-03-07 14:14:40 +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
|
|
|
}
|
|
|
|
}
|
2020-03-07 14:14:40 +03:00
|
|
|
}
|