-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
160 lines (147 loc) · 3.63 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
)
var (
outputName string
inputName string
unpackMode bool //json to dir
packMode bool //dir to json
debug bool
version bool
singleSeparate bool
importNew bool
withGraph bool
split bool
number uint
dirs = []string{dirSnippet, dirMenu, dirLang, dirTable, dirParam, dirData, dirPage, dirCon}
)
func init() {
flag.BoolVar(&unpackMode, "unpack", false, "-u, unpacking mode")
flag.StringVar(&inputName, "input", ".", "-i, path for input files, filename for pack and dirname/ (slashed) for unpack")
flag.StringVar(&outputName, "output", "output", "-o, output filename for JSON if input file name not pointed")
flag.BoolVar(&split, "s", false, "split json file by type")
flag.UintVar(&number, "n", 0, "split json file by number")
// shorthand
flag.StringVar(&outputName, "o", "output", "-output")
flag.StringVar(&inputName, "i", ".", "input")
flag.BoolVar(&unpackMode, "u", false, "-unpack")
flag.BoolVar(&version, "v", false, "-version")
flag.BoolVar(&debug, "d", false, "debug")
flag.BoolVar(&withGraph, "g", false, "make graphical structure in dot-file")
}
func main() {
flag.Parse()
args := flag.Args()
if argsCount := len(args); argsCount == 0 {
// without args run gui
SimpleGui()
} else {
if argsCount == 1 && version {
fmt.Println(currentTitle)
} else if argsCount >= 1 {
if version {
fmt.Println(currentTitle)
}
name := args[0]
if stats, err := os.Stat(name); err == nil {
if stats.IsDir() {
packMode = true
inputName = name
} else if stats.Mode() != os.ModeType {
unpackMode = true
inputName = name
}
} else {
fmt.Println(err)
}
}
checkOutput()
}
if unpackMode {
unpackJSON(inputName)
} else if packMode {
packJSON(inputName)
}
}
func checkOutput() {
if outputName == "output" && inputName != "." { // we have only inputname
parts := strings.Split(inputName, separator)
pLen := len(parts)
outputName = parts[pLen-1]
if unpackMode {
if !split {
ext := filepath.Ext(outputName)
outputName = outputName[:len(outputName)-len(ext)]
outputName = outputName + separator
}
} else {
if strings.HasSuffix(inputName, separator) {
outputName = parts[pLen-2]
}
}
}
if unpackMode {
if stats, err := os.Stat(inputName); inputName == "." || stats.IsDir() || err != nil {
if err != nil {
fmt.Println(err)
}
fmt.Println(helpMsg)
return
}
if !strings.HasSuffix(outputName, separator) && !split {
outputName = outputName + separator
}
if debug {
fmt.Println("output dir name:", outputName)
}
} else {
if !strings.HasSuffix(inputName, separator) {
fmt.Println(helpMsg)
return
}
}
}
func createDir(path string) {
if _, e := os.Stat(path); os.IsNotExist(e) {
if err := os.MkdirAll(path, os.ModePerm); err != nil {
fmt.Println("error create dir", err)
}
}
}
func writeFileString(filename, content string) {
outFile, err := os.Create(filepath.Join(outputName, filename))
if err != nil {
// fmt.Println("error write file:", err)
return
}
defer outFile.Close()
if _, err := outFile.WriteString(content); err != nil {
fmt.Println(err)
return
}
}
func writeFileString2(filename, content string) {
outFile, err := os.Create(filepath.Join(filename))
if err != nil {
fmt.Println("error write file:", err)
return
}
defer outFile.Close()
if _, err := outFile.WriteString(content); err != nil {
fmt.Println(err)
return
}
}
func stringInSlice(arr []string, val string) bool {
for _, v := range arr {
if v == val {
return true
}
}
return false
}