mkdir.go 667 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package mkdir
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "surdeus.su/core/cli/mtool"
  7. )
  8. func Run(flagSet *mtool.Flags) {
  9. var (
  10. parentFlag bool
  11. modeArg int
  12. )
  13. flagSet.BoolVar(&parentFlag, "p", false, "No error if existing, make parent as needed.")
  14. flagSet.IntVar(&modeArg, "m", 0766, "Set file `mode`.")
  15. flagSet.Parse()
  16. args := flagSet.Args()
  17. if len(args) == 0 {
  18. flagSet.Usage()
  19. os.Exit(1)
  20. }
  21. mode := os.FileMode(modeArg)
  22. for _, path := range args {
  23. var e error
  24. pth := filepath.FromSlash(path)
  25. if parentFlag {
  26. e = os.MkdirAll(pth, mode)
  27. } else {
  28. e = os.Mkdir(pth, mode)
  29. }
  30. if e != nil {
  31. fmt.Fprintf(os.Stderr, "%s\n", e)
  32. }
  33. }
  34. }