Skip to content

Commit e7db3fd

Browse files
committed
chore: improve cli
1 parent 40a6193 commit e7db3fd

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

internal/updater/updater.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ func UpdateConfInDir(rootDir string, outputDir string, indent int, indentChar st
117117
output = filepath.Join(outputDir, relPath)
118118
}
119119

120+
os.MkdirAll(filepath.Dir(output), 0700)
120121
err = os.WriteFile(output, []byte(DecodeEscapeChars(modifiedData)), 0644)
121122
if err != nil {
122123
fmt.Printf("Formatter Nginx Conf %s failed, can not save the file\n", output)

main.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ import (
1111
"github.com/soulteary/nginx-formatter/internal/version"
1212
)
1313

14+
const (
15+
DEFAULT_INDENT_SIZE = 2
16+
DEFAULT_INDENT_CHAR = " "
17+
)
18+
1419
var FORMATTER_SRC = ""
1520
var FORMATTER_DEST = ""
16-
var FORMATTER_INDENT = 2
17-
var FORMATTER_CHAR = " "
21+
var FORMATTER_INDENT = DEFAULT_INDENT_SIZE
22+
var FORMATTER_CHAR = DEFAULT_INDENT_CHAR
1823

1924
func InitArgv() {
2025
var inputDir string
@@ -23,8 +28,8 @@ func InitArgv() {
2328
var indentChar string
2429
flag.StringVar(&inputDir, "input", "", "Input directory")
2530
flag.StringVar(&outputDir, "output", "", "Output directory")
26-
flag.IntVar(&indent, "indent", 2, "Indent size")
27-
flag.StringVar(&indentChar, "char", " ", "Indent char")
31+
flag.IntVar(&indent, "indent", DEFAULT_INDENT_SIZE, fmt.Sprintf("Indent size, defualt: %d", DEFAULT_INDENT_SIZE))
32+
flag.StringVar(&indentChar, "char", DEFAULT_INDENT_CHAR, fmt.Sprintf("Indent char, defualt: `%s`", DEFAULT_INDENT_CHAR))
2833
flag.Parse()
2934

3035
if inputDir == "" {
@@ -52,22 +57,30 @@ func InitArgv() {
5257
fmt.Println(err)
5358
os.Exit(1)
5459
}
60+
fmt.Println("Specify the output directory as:", inputDir)
61+
FORMATTER_DEST = outputDir
5562
}
5663

5764
if indent <= 0 {
58-
FORMATTER_INDENT = 2
65+
fmt.Println("No output indent size specified, use the default value:", DEFAULT_INDENT_SIZE)
66+
FORMATTER_INDENT = DEFAULT_INDENT_SIZE
5967
} else {
68+
fmt.Println("Specify the indent size as:", inputDir)
6069
FORMATTER_INDENT = indent
6170
}
6271

6372
if indentChar == "" {
64-
FORMATTER_CHAR = " "
73+
FORMATTER_CHAR = DEFAULT_INDENT_CHAR
74+
fmt.Printf("No output indent char specified, use the default value: `%s`\n", FORMATTER_CHAR)
6575
} else {
66-
if indentChar != "\t" || indentChar != " " {
67-
indentChar = " "
76+
if indentChar != "\t" || indentChar != " " || indentChar != "\\s" {
77+
indentChar = DEFAULT_INDENT_CHAR
78+
fmt.Printf("Specify the indent char not support, use the default value: `%s`\n", DEFAULT_INDENT_CHAR)
6879
}
6980
FORMATTER_CHAR = indentChar
81+
fmt.Printf("Specify the indent char as: `%s`\n", FORMATTER_CHAR)
7082
}
83+
fmt.Println()
7184
}
7285

7386
func main() {

0 commit comments

Comments
 (0)