-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.go
54 lines (45 loc) · 1.05 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/spf13/cobra"
)
var options struct {
mode string
}
var rootCmd = &cobra.Command{
Use: "make_directories DIR [DIR ...]",
Short: "make directories",
Long: `make directories with given permission flags`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
return subMain(args)
},
}
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
func subMain(dirs []string) error {
modeBits, err := strconv.ParseInt(options.mode, 8, 64)
if err != nil {
return fmt.Errorf("invalid mode %s: %w", options.mode, err)
}
for _, d := range dirs {
if !filepath.IsAbs(d) {
return fmt.Errorf("non-absolute path: %s", d)
}
if err := os.MkdirAll(d, os.FileMode(modeBits)); err != nil {
return fmt.Errorf("failed to create %s: %w", d, err)
}
}
return nil
}
func init() {
rootCmd.Flags().StringVar(&options.mode, "mode", "755", "permission bits for directories")
}