-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (110 loc) · 3.64 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/devkcud/mk/internal/help"
"github.com/devkcud/mk/internal/mklog"
"github.com/devkcud/mk/internal/utils"
"github.com/fatih/color"
flag "github.com/spf13/pflag"
)
var Version string
func main() {
flag.Usage = func() { help.ShowHelp(Version) }
flagHelp := flag.BoolP("help", "h", false, "Show help menu")
flagVersion := flag.BoolP("version", "V", false, "Show version")
flagQuiet := flag.BoolP("quiet", "q", false, "Disable output")
flagPrompt := flag.Bool("prompt", false, "Ask to create file/directory")
flagNoColor := flag.Bool("nocolor", false, "Disable colors in output (logging)")
flagLogLevel := flag.IntP("loglevel", "l", 2, "Set log level (0: none, 1: error, 2: warn, 3: info, 4: all)")
flagPreview := flag.BoolP("preview", "p", false, "Preview mode (show what would be created)")
flag.Parse()
mklog.LogLevel = *flagLogLevel
previewFolder := filepath.Join(os.TempDir(), fmt.Sprintf("mk-preview-%d", os.Getpid()))
previousWd, _ := os.Getwd()
if *flagPreview {
if _, err := os.Stat(previewFolder); os.IsNotExist(err) {
utils.CreateDir(previewFolder)
} else {
os.RemoveAll(previewFolder)
}
os.Chdir(previewFolder)
}
if *flagNoColor {
os.Setenv("NO_COLOR", "1")
}
if *flagQuiet {
if *flagPrompt {
mklog.Warn("--prompt and --quiet are incompatible; skipping --prompt")
}
os.Stdin = nil
os.Stdout = nil
os.Stderr = nil
}
if *flagVersion {
mklog.Print("mk", Version)
return
}
if *flagHelp || flag.NArg() == 0 {
flag.Usage()
return
}
var dirstack []string
for _, name := range flag.Args() {
if strings.Trim(name, ".") == "" {
dots := strings.Count(name, ".") - 1
if dots > len(dirstack) {
dirstack = nil
} else {
dirstack = dirstack[:len(dirstack)-dots]
}
quantSay := "directory"
if dots > 1 {
quantSay = "directories"
}
mklog.Log("Going", color.MagentaString(fmt.Sprint(dots)), quantSay, "back (dirstack)")
continue
}
dir, file := filepath.Split(name)
if dir != "" && file != "" {
temp := append(dirstack, dir)
if *flagPrompt && !utils.YesNoPrompt("Create path "+filepath.Join(append(temp, file)...)+"?", true) {
mklog.Log("Skipping path", color.MagentaString(filepath.Join(append(temp, file)...)))
continue
}
utils.CreateDir(filepath.Join(temp...))
utils.CreateFile(filepath.Join(append(temp, file)...))
continue
}
if dir != "" {
separated_dirs := strings.Split(dir, "/")
if *flagPrompt && !utils.YesNoPrompt("Create directory "+filepath.Join(append(dirstack, separated_dirs[:len(separated_dirs)-1]...)...)+"?", true) {
mklog.Log("Skipping directory", color.MagentaString(filepath.Join(append(dirstack, separated_dirs[:len(separated_dirs)-1]...)...)))
mklog.Warn(color.MagentaString(filepath.Join(append(dirstack, separated_dirs[:len(separated_dirs)-1]...)...)), "not added to the dirstack")
continue
}
if strings.HasPrefix(dir, "/") {
dirstack = nil
dirstack = append(dirstack, "/")
}
dirstack = append(dirstack, separated_dirs[:len(separated_dirs)-1]...)
utils.CreateDir(filepath.Join(dirstack...))
}
if file != "" {
if *flagPrompt && !utils.YesNoPrompt("Create file "+filepath.Join(append(dirstack, file)...)+"?", true) {
mklog.Log("Skipping file", color.MagentaString(filepath.Join(append(dirstack, file)...)))
continue
}
utils.CreateFile(filepath.Join(append(dirstack, file)...))
}
}
if *flagPreview {
utils.BuildTree(previewFolder, "")
if utils.YesNoPrompt("Preview mode. Do you want to continue?", false) {
utils.CopyFolder(previewFolder, previousWd)
}
os.RemoveAll(previewFolder)
}
}