-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
420 lines (388 loc) · 8.27 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package main
import (
"flag"
"log"
"os"
"os/exec"
"path/filepath"
)
var (
codePath string
err error
)
func init() {
if len(os.Args) > 1 {
tempCodePath := flag.String("path", "/user/example/folder/file", "The file's location in the system.")
flag.Parse()
codePath = *tempCodePath
} else {
log.Fatal("Error: The system path has not been given.")
}
// System path
if !folderExists(codePath) {
log.Fatal("Error: The system path has not been given.")
}
}
func main() {
if fileExists(codePath) {
// Format a single file
formatFile()
} else if folderExists(codePath) {
// Format a whole folder
formatDirectory()
} else {
log.Fatal("Error: The machine direction is invalid.")
}
}
func formatFile() {
switch filepath.Ext(codePath) {
// Golang
case ".go":
formatGoFiles(codePath)
// Shell Script
case ".sh":
formatShellScriptFiles(codePath)
// HTML
case ".html":
formatHTMLFiles(codePath)
// CSS
case ".css":
formatCSSFiles(codePath)
// JavaScript
case ".js":
formatJSFiles(codePath)
// TypeScript
case ".ts":
formatTypeScriptFiles(codePath)
// Python
case ".py":
formatPythonFiles(codePath)
// Java
case ".java":
formatJavaFiles(codePath)
// c##
case ".cs":
formatCSFiles(codePath)
// C++
case ".cpp":
formatCPPFiles(codePath)
// C
case ".c":
formatCFiles(codePath)
// PHP
case ".php":
formatPHPFiles(codePath)
// Kotlin
case ".kts":
formatKotlinFiles(codePath)
// Ruby
case ".rb":
formatRubyFiles(codePath)
// Swift
case ".swift":
formatSwiftFiles(codePath)
// Rust
case ".rs":
formatRustFiles(codePath)
// Scala
case ".scala":
formatScalaFiles(codePath)
// Dart
case ".dart":
formatDartFiles(codePath)
// PowerShell
case ".ps1":
formatPowerShellFiles(codePath)
// Yaml
case ".yaml":
formatYamlFiles(codePath)
// JSON
case ".json":
formatJSONFiles(codePath)
// Markdown
case ".md":
formatMarkdownFiles(codePath)
default:
log.Println("Error:", codePath)
}
}
func formatDirectory() {
filepath.Walk(codePath, func(path string, info os.FileInfo, err error) error {
switch filepath.Ext(path) {
// Golang
case ".go":
formatGoFiles(path)
// Shell Script
case ".sh":
formatShellScriptFiles(path)
// HTML
case ".html":
formatHTMLFiles(path)
// CSS
case ".css":
formatCSSFiles(path)
// JavaScript
case ".js":
formatJSFiles(path)
// TypeScript
case ".ts":
formatTypeScriptFiles(path)
// Python
case ".py":
formatPythonFiles(path)
// Java
case ".java":
formatJavaFiles(path)
// c##
case ".cs":
formatCSFiles(path)
// C++
case ".cpp":
formatCPPFiles(path)
// C
case ".c":
formatCFiles(path)
// PHP
case ".php":
formatPHPFiles(path)
// Kotlin
case ".kts":
formatKotlinFiles(path)
// Ruby
case ".rb":
formatRubyFiles(path)
// Swift
case ".swift":
formatSwiftFiles(path)
// Rust
case ".rs":
formatRustFiles(path)
// Scala
case ".scala":
formatScalaFiles(path)
// Dart
case ".dart":
formatDartFiles(path)
// PowerShell
case ".ps1":
formatPowerShellFiles(path)
// Yaml
case ".yaml":
formatYamlFiles(path)
// JSON
case ".json":
formatJSONFiles(path)
// Markdown
case ".md":
formatMarkdownFiles(path)
default:
log.Println("Error:", path)
}
return nil
})
}
// Golang
func formatGoFiles(filePath string) {
commandExists("go")
cmd := exec.Command("go", "fmt", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Shell Script
func formatShellScriptFiles(filePath string) {
commandExists("shfmt")
cmd := exec.Command("shfmt", "-l -w", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// HTML
func formatHTMLFiles(filePath string) {
commandExists("html-minifier")
cmd := exec.Command("html-minifier", filePath, "-o", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// CSS
func formatCSSFiles(filePath string) {
commandExists("cssnano")
cmd := exec.Command("cssnano", filePath, filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// JS
func formatJSFiles(filePath string) {
commandExists("uglifyjs")
cmd := exec.Command("uglifyjs", "-b", "--", filePath, "-o", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Python
func formatPythonFiles(filePath string) {
commandExists("yapf")
cmd := exec.Command("yapf", "--in-place", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Java
func formatJavaFiles(filePath string) {
commandExists("google-java-format")
cmd := exec.Command("google-java-format", "--replace", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// C++
func formatCPPFiles(filePath string) {
commandExists("clang-format")
cmd := exec.Command("clang-format", "-style=Google", "-i", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// C##
func formatCSFiles(filePath string) {
commandExists("dotnet-format")
cmd := exec.Command("dotnet-format", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// C
func formatCFiles(filePath string) {
commandExists("clang-format")
cmd := exec.Command("clang-format", "-style=Google", "-i", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// TypeScript
func formatTypeScriptFiles(filePath string) {
commandExists("gts")
cmd := exec.Command("gts", "fix", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// PHP
func formatPHPFiles(filePath string) {
commandExists("phptidy")
cmd := exec.Command("phptidy", "replace", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Kotlin
func formatKotlinFiles(filePath string) {
commandExists("ktlint")
cmd := exec.Command("ktlint", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Ruby
func formatRubyFiles(filePath string) {
commandExists("rufo")
cmd := exec.Command("rufo", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Swift
func formatSwiftFiles(filePath string) {
commandExists("swift-format")
cmd := exec.Command("swift-format", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Rust
func formatRustFiles(filePath string) {
commandExists("rustfmt")
cmd := exec.Command("rustfmt", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Scala
func formatScalaFiles(filePath string) {
commandExists("scalafmt")
cmd := exec.Command("scalafmt", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Dart
func formatDartFiles(filePath string) {
commandExists("dart")
cmd := exec.Command("dart", "format", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// PowerShell
func formatPowerShellFiles(filePath string) {
commandExists("Edit-DTWBeautifyScript")
cmd := exec.Command("Edit-DTWBeautifyScript", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// JSON
func formatJSONFiles(filePath string) {
commandExists("json-format")
cmd := exec.Command("json-format", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Markdown
func formatMarkdownFiles(filePath string) {
commandExists("mdformat")
cmd := exec.Command("mdformat", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Yaml
func formatYamlFiles(filePath string) {
commandExists("yamllint")
cmd := exec.Command("yamllint", filePath)
err = cmd.Run()
handleErrors(err)
log.Println("Optimizing:", filePath)
}
// Directory Check
func folderExists(foldername string) bool {
info, err := os.Stat(foldername)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
// File Check
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
// Application Check
func commandExists(cmd string) {
cmd, err := exec.LookPath(cmd)
if err != nil {
log.Printf("Error: The application %s was not found in the system.\n", cmd)
}
}
// Handle errors
func handleErrors(err error) {
if err != nil {
log.Println(err)
}
}