Skip to content

Commit dc16601

Browse files
committed
feat: cli argv support (wip)
1 parent 870caf5 commit dc16601

File tree

2 files changed

+68
-14
lines changed

2 files changed

+68
-14
lines changed

internal/updater/updater.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,34 +92,43 @@ func ScanFiles(rootDir string) ([]string, error) {
9292
return files, nil
9393
}
9494

95-
func UpdateConfInDir(rootDir string, fn func(s string) (string, error)) error {
96-
95+
func UpdateConfInDir(rootDir string, outputDir string, indent int, fn func(s string) (string, error)) error {
9796
files, err := ScanFiles(rootDir)
9897
if err != nil {
9998
return err
10099
}
101100
for _, file := range files {
102-
data, err := os.ReadFile(file)
101+
buf, err := os.ReadFile(file)
103102
if err != nil {
103+
fmt.Printf("Formatter Nginx Conf %s failed, can not open the file\n", err)
104104
return err
105105
}
106-
modifiedData, err := fn(FixVars(FixReturn(EncodeEscapeChars(string(data)))))
106+
modifiedData, err := fn(FixVars(FixReturn(EncodeEscapeChars(string(buf)))))
107107
if err != nil {
108+
fmt.Printf("Formatter Nginx Conf %s failed, can not format the file\n", err)
108109
return err
109110
}
110111

111-
err = os.WriteFile(file, []byte(DecodeEscapeChars(modifiedData)), 0644)
112+
output := ""
113+
relPath, err := filepath.Rel(rootDir, file)
114+
if err != nil {
115+
output = filepath.Join(outputDir, file)
116+
} else {
117+
output = filepath.Join(outputDir, relPath)
118+
}
119+
120+
err = os.WriteFile(output, []byte(DecodeEscapeChars(modifiedData)), 0644)
112121
if err != nil {
122+
fmt.Printf("Formatter Nginx Conf %s failed, can not save the file\n", output)
113123
return err
114124
}
115125

116-
rel, err := filepath.Rel(rootDir, file)
126+
relPath, err = filepath.Rel(rootDir, output)
117127
if err != nil {
118-
fmt.Printf("Formatter Nginx Conf %s Successed\n", file)
128+
fmt.Printf("Formatter Nginx Conf %s Successed\n", output)
119129
} else {
120-
fmt.Printf("Formatter Nginx Conf %s Successed\n", rel)
130+
fmt.Printf("Formatter Nginx Conf %s Successed\n", relPath)
121131
}
122132
}
123-
124133
return nil
125134
}

main.go

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"fmt"
56
"log"
67
"os"
@@ -10,15 +11,59 @@ import (
1011
"github.com/soulteary/nginx-formatter/internal/version"
1112
)
1213

14+
var FORMATTER_SRC = ""
15+
var FORMATTER_DEST = ""
16+
var FORMATTER_INDENT = 2
17+
18+
func InitArgv() {
19+
var inputDir string
20+
var outputDir string
21+
var indent int
22+
flag.StringVar(&inputDir, "input", "", "Input directory")
23+
flag.StringVar(&outputDir, "output", "", "Output directory")
24+
flag.IntVar(&indent, "indent", 2, "Indent size")
25+
flag.Parse()
26+
27+
if inputDir == "" {
28+
dir, err := os.Getwd()
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
fmt.Println("No input directory specified, use the current working directory:", dir)
33+
FORMATTER_SRC = dir
34+
} else {
35+
fmt.Println("Specify the working directory as:", inputDir)
36+
FORMATTER_SRC = inputDir
37+
}
38+
39+
if outputDir == "" {
40+
dir, err := os.Getwd()
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
fmt.Println("No output directory specified, use the current working directory:", dir)
45+
FORMATTER_DEST = dir
46+
} else {
47+
err := os.MkdirAll(outputDir, 0755)
48+
if err != nil {
49+
fmt.Println(err)
50+
os.Exit(1)
51+
}
52+
}
53+
54+
if indent <= 0 {
55+
FORMATTER_INDENT = 2
56+
} else {
57+
FORMATTER_INDENT = indent
58+
}
59+
}
60+
1361
func main() {
1462
fmt.Printf("Nginx Formatter v%s\n\n", version.Version)
1563

16-
dir, err := os.Getwd()
17-
if err != nil {
18-
log.Fatal(err)
19-
}
64+
InitArgv()
2065

21-
err = updater.UpdateConfInDir(dir, formatter.Formatter)
66+
err := updater.UpdateConfInDir(FORMATTER_SRC, FORMATTER_DEST, FORMATTER_INDENT, formatter.Formatter)
2267
if err != nil {
2368
log.Fatal(err)
2469
}

0 commit comments

Comments
 (0)