Skip to content

Commit 1f5ba5c

Browse files
luo6luo6
authored andcommitted
init
0 parents  commit 1f5ba5c

File tree

18 files changed

+994
-0
lines changed

18 files changed

+994
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.idea
2+
conf.d
3+
nginx.conf
4+
build
5+
build.sh

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 ICU-Coders
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
2+
<p align="center" >
3+
<img src="https://raw.githubusercontent.com/ICU-Coders/IconLib/master/icon.jpg" alt="ICU-Coders" title="ICU-Coders">
4+
</p>
5+
6+
# nginx-config-reader(ncr)
7+
8+
nginx 配置文件读取工具,用于快速预览当前服务下所有nginx配置文件
9+
10+
![MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat)
11+
12+
[English](./README_EN.md)
13+
14+
效果图
15+
![example](./example.jpg)
16+
17+
18+
```shell
19+
go run ./main.go
20+
```
21+
或者
22+
```shell
23+
cd build
24+
chmod o+x ./ncr_linunx
25+
./ncr_linunx
26+
```
27+
### 可选参数
28+
```
29+
-i input 指定解析文件地址
30+
-l log 输出nginx配置log的位置
31+
-s sort 排序("host", "server", "server_name")/(listen", "port")/uri/dir 默认listen
32+
-m match 匹配字符筛选
33+
-h help 输出帮助内容
34+
```
35+
36+
## MIT License
37+
38+
Copyright (c) 2022 ICU-Coders
39+
40+
Permission is hereby granted, free of charge, to any person obtaining a copy
41+
of this software and associated documentation files (the "Software"), to deal
42+
in the Software without restriction, including without limitation the rights
43+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
44+
copies of the Software, and to permit persons to whom the Software is
45+
furnished to do so, subject to the following conditions:
46+
47+
The above copyright notice and this permission notice shall be included in all
48+
copies or substantial portions of the Software.
49+
50+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
51+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
53+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
55+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
56+
SOFTWARE.

README_EN.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
<p align="center" >
3+
<img src="https://raw.githubusercontent.com/ICU-Coders/IconLib/master/icon.jpg" alt="ICU-Coders" title="ICU-Coders">
4+
</p>
5+
6+
# nginx-config-reader(ncr)
7+
8+
nginx configuration file reader tool, used to quickly preview all nginx configuration files under the current service
9+
10+
![MIT](https://img.shields.io/badge/License-MIT-blue.svg?style=flat)
11+
12+
Results Show
13+
![example](./example.jpg)
14+
15+
### How To Use
16+
17+
```shell
18+
go run ./main.go
19+
```
20+
OR
21+
```shell
22+
cd build
23+
chmod o+x ./ncr_linunx
24+
./ncr_linunx
25+
```
26+
### Optional parameters
27+
```
28+
-i input Specify the address of the parsed file
29+
-l log Output the location of the nginx configuration log
30+
-s sort server/listen/uri/dir, default `listen`
31+
-m match Matching character filter
32+
-h help Help content
33+
```
34+
35+
## MIT License
36+
37+
Copyright (c) 2022 ICU-Coders
38+
39+
Permission is hereby granted, free of charge, to any person obtaining a copy
40+
of this software and associated documentation files (the "Software"), to deal
41+
in the Software without restriction, including without limitation the rights
42+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43+
copies of the Software, and to permit persons to whom the Software is
44+
furnished to do so, subject to the following conditions:
45+
46+
The above copyright notice and this permission notice shall be included in all
47+
copies or substantial portions of the Software.
48+
49+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
55+
SOFTWARE.

cmd/ngixn.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
output2 "nginx-config-reader/output"
5+
"os/exec"
6+
"regexp"
7+
)
8+
9+
func CheckNginx() (string, error) {
10+
command := exec.Command("nginx", "-t")
11+
output, err := command.CombinedOutput()
12+
if err != nil {
13+
output2.ErrorFatal(1001, err)
14+
}
15+
result := string(output)
16+
compile, err := regexp.Compile("the configuration file(.*?)syntax is ok")
17+
if err != nil {
18+
output2.ErrorFatal(1002, err)
19+
}
20+
21+
submatch := compile.FindStringSubmatch(result)
22+
if len(submatch) < 2 {
23+
output2.ErrorFatal(1003, result)
24+
}
25+
return submatch[1], nil
26+
}

config/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package config
2+
3+
import "flag"
4+
5+
var RootPath string
6+
var ShowLog bool
7+
var SortType string // port/host/uri/dir
8+
var Match string
9+
10+
func init() {
11+
flag.StringVar(&RootPath, "i", "", "请输入待解析文件路径(Please enter the path of the file to be parsed)")
12+
flag.StringVar(&SortType, "s", "port", "输出结果排序依据(Output results are sorted based on)")
13+
flag.BoolVar(&ShowLog, "l", false, "是否显示日志路径(Whether to display the log path)")
14+
flag.StringVar(&Match, "m", "", "高亮匹配到的字符(Matching character filter)")
15+
flag.Parse()
16+
}

example.jpg

94 KB
Loading

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module nginx-config-reader
2+
3+
go 1.17
4+
5+
require github.com/fatih/color v1.13.0
6+
7+
require (
8+
github.com/mattn/go-colorable v0.1.9 // indirect
9+
github.com/mattn/go-isatty v0.0.14 // indirect
10+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
11+
)

go.sum

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
2+
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
3+
github.com/mattn/go-colorable v0.1.9 h1:sqDoxXbdeALODt0DAeJCVp38ps9ZogZEAXjus69YV3U=
4+
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
5+
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
6+
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
7+
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
8+
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
9+
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
10+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
11+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

main.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"github.com/fatih/color"
5+
"log"
6+
"nginx-config-reader/cmd"
7+
"nginx-config-reader/config"
8+
"nginx-config-reader/output"
9+
"nginx-config-reader/parser"
10+
)
11+
12+
func main() {
13+
14+
if len(config.RootPath) == 0 {
15+
var err error
16+
config.RootPath, err = cmd.CheckNginx()
17+
if err != nil {
18+
output.ErrorFatal(1004, err)
19+
}
20+
}
21+
22+
// 从根配置开始解析,如果用户输入文件,解析用户文件,否则按照nginx -t解析输出文件
23+
content, err := parser.ConfigParser(config.RootPath)
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
28+
color.New(color.FgCyan).Println("> Generate results...")
29+
output.Nginx(content)
30+
}

0 commit comments

Comments
 (0)