Skip to content

Commit 48b3c00

Browse files
committed
feat: location ast
1 parent 19534ae commit 48b3c00

File tree

11 files changed

+124
-22
lines changed

11 files changed

+124
-22
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
vendor
2-
bin
2+
bin
3+
dump.json

README.md

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ A nginx config parser
77
## Basic Usage
88
Usage:
99
```sh
10-
go-ngx-config parse -f <NGINX_CONF_FILE>
10+
# -f file path location nginx configm, e,g: ./examples/basic/nginx.conf
11+
# -o output json file path location, e.g: ./examples/basic/output
12+
13+
14+
go-ngx-config parse -f <NGINX_CONF_FILE> -o <OUTPUT_JSON_FILE_DUMP>
1115
```
1216

1317
<details>
@@ -98,21 +102,14 @@ go-ngx-config parse -f <NGINX_CONF_FILE>
98102
]
99103
},
100104
{
101-
"Block": {
102-
"Directives": [
103-
{
104-
"Block": null,
105-
"Name": "root",
106-
"Parameters": [
107-
"html"
108-
]
109-
}
110-
]
111-
},
112105
"Name": "location",
113-
"Parameters": [
114-
"/"
115-
]
106+
"Modifier": "=",
107+
"Match": "/favicon.ico"
108+
},
109+
{
110+
"Name": "location",
111+
"Modifier": "",
112+
"Match": "/"
116113
}
117114
]
118115
}
@@ -152,7 +149,7 @@ go-ngx-config parse -f <NGINX_CONF_FILE>
152149
]
153150
}
154151
],
155-
"Filepath": "examples/basic/nginx.conf"
152+
"Filepath": "./examples/basic/nginx.conf"
156153
}
157154
```
158155
</details>

cmd/go-ngx-config/commands.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func NewParseCommand() *cobra.Command {
2323
}
2424

2525
parseCmd.Flags().StringP("file", "f", "", "nginx.conf file location")
26+
parseCmd.Flags().StringP("output", "o", "", "output file location")
2627

2728
return parseCmd
2829
}

cmd/go-ngx-config/parser_command.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,33 @@ package main
22

33
import (
44
"encoding/json"
5+
"errors"
6+
"os"
7+
"time"
58

69
"github.com/adityalstkp/go-ngx-config/pkg/cli"
10+
"github.com/sirupsen/logrus"
711
"github.com/spf13/cobra"
812
)
913

1014
func RunParseNgx(cmd *cobra.Command, args []string) error {
15+
startTime := time.Now()
16+
1117
filePath, err := cmd.Flags().GetString("file")
1218
if err != nil {
1319
return err
1420
}
1521

16-
ast, err := cli.NewNgxConfParser(filePath)
22+
outputFilePath, err := cmd.Flags().GetString("output")
23+
if err != nil {
24+
return err
25+
}
26+
27+
cliOpts := cli.NgxConfParserCliOptions{
28+
Filepath: filePath,
29+
}
30+
31+
ast, err := cli.NewNgxConfParser(cliOpts)
1732
if err != nil {
1833
return err
1934
}
@@ -23,7 +38,32 @@ func RunParseNgx(cmd *cobra.Command, args []string) error {
2338
return err
2439
}
2540

26-
println(string(ast_json))
41+
if outputFilePath != "" {
42+
if _, err := os.Stat(outputFilePath); errors.Is(err, os.ErrNotExist) {
43+
err := os.MkdirAll(outputFilePath, os.ModePerm)
44+
if err != nil {
45+
return err
46+
}
47+
}
48+
49+
dumpAstJsonFile := outputFilePath + "/dump.json"
50+
f, err := os.Create(dumpAstJsonFile)
51+
if err != nil {
52+
return err
53+
}
54+
defer f.Close()
55+
56+
_, err = f.Write(ast_json)
57+
if err != nil {
58+
return err
59+
}
60+
} else {
61+
println(string(ast_json))
62+
}
63+
64+
elapsed := time.Since(startTime)
65+
logrus.Info("Process time: ", elapsed)
66+
2767
return nil
2868

2969
}

examples/basic/nginx.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,15 @@ http {
2222

2323
error_page 500 502 503 504 /50x.html;
2424

25+
location = /favicon.ico {
26+
root html;
27+
}
28+
2529
location / {
2630
root html;
2731
}
2832

33+
2934
}
3035

3136
}

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@ module github.com/adityalstkp/go-ngx-config
22

33
go 1.14
44

5-
require github.com/spf13/cobra v1.5.0
5+
require (
6+
github.com/sirupsen/logrus v1.9.0
7+
github.com/spf13/cobra v1.5.0
8+
)

go.sum

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
35
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
6+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
47
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8+
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
9+
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
510
github.com/spf13/cobra v1.5.0 h1:X+jTBEBqF0bHN+9cSMgmfuvv2VHJ9ezmFNf9Y/XstYU=
611
github.com/spf13/cobra v1.5.0/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM=
712
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
813
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
14+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
15+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
16+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
17+
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
918
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1019
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
20+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/ast/location.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package ast
2+
3+
import (
4+
"errors"
5+
6+
"github.com/adityalstkp/go-ngx-config/internal/directive"
7+
)
8+
9+
type Location struct {
10+
*directive.Directive
11+
Name string
12+
Modifier string
13+
Match string
14+
}
15+
16+
func NewLocation(directive *directive.Directive) (*Location, error) {
17+
location := &Location{
18+
Name: "location",
19+
}
20+
21+
if len(directive.Parameters) == 0 {
22+
return nil, errors.New("not enough argument in location block")
23+
}
24+
25+
if len(directive.Parameters) == 1 {
26+
location.Match = directive.Parameters[0]
27+
return location, nil
28+
} else if len(directive.Parameters) == 2 {
29+
location.Modifier = directive.Parameters[0]
30+
location.Match = directive.Parameters[1]
31+
return location, nil
32+
}
33+
34+
return location, nil
35+
}

internal/parser/parser.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ func newParserFromLexer(lexer *lexer.Lexer) *Parser {
5151
"server": func(d *directive.Directive) statement.IDirective {
5252
return parser.wrapServer(d)
5353
},
54+
"location": func(d *directive.Directive) statement.IDirective {
55+
return parser.wrapLocation(d)
56+
},
5457
}
5558

5659
return parser
@@ -133,3 +136,8 @@ func (p *Parser) wrapServer(directive *directive.Directive) *ast.Server {
133136
s, _ := ast.NewServer(directive)
134137
return s
135138
}
139+
140+
func (p *Parser) wrapLocation(directive *directive.Directive) *ast.Location {
141+
l, _ := ast.NewLocation(directive)
142+
return l
143+
}

pkg/cli/cli.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"github.com/adityalstkp/go-ngx-config/internal/parser"
88
)
99

10-
func NewNgxConfParser(file string) (*ast.Config, error) {
11-
parser, err := parser.NewParser(file)
10+
func NewNgxConfParser(cliOpts NgxConfParserCliOptions) (*ast.Config, error) {
11+
parser, err := parser.NewParser(cliOpts.Filepath)
1212
if err != nil {
1313
return nil, err
1414
}

0 commit comments

Comments
 (0)