Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
425f6114
Commit
425f6114
authored
Oct 13, 2017
by
Craig Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
initial implementation of caddyfile macros
parent
79072828
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
158 additions
and
50 deletions
+158
-50
caddyfile/parse.go
caddyfile/parse.go
+117
-50
caddyfile/parse_test.go
caddyfile/parse_test.go
+41
-0
No files found.
caddyfile/parse.go
View file @
425f6114
...
@@ -54,6 +54,7 @@ type parser struct {
...
@@ -54,6 +54,7 @@ type parser struct {
block
ServerBlock
// current server block being parsed
block
ServerBlock
// current server block being parsed
validDirectives
[]
string
// a directive must be valid or it's an error
validDirectives
[]
string
// a directive must be valid or it's an error
eof
bool
// if we encounter a valid EOF in a hard place
eof
bool
// if we encounter a valid EOF in a hard place
definedMacros
map
[
string
][]
Token
}
}
func
(
p
*
parser
)
parseAll
()
([]
ServerBlock
,
error
)
{
func
(
p
*
parser
)
parseAll
()
([]
ServerBlock
,
error
)
{
...
@@ -95,6 +96,25 @@ func (p *parser) begin() error {
...
@@ -95,6 +96,25 @@ func (p *parser) begin() error {
return
nil
return
nil
}
}
if
ok
,
name
:=
p
.
isMacro
();
ok
{
if
p
.
definedMacros
==
nil
{
p
.
definedMacros
=
map
[
string
][]
Token
{}
}
if
p
.
definedMacros
[
name
]
!=
nil
{
p
.
Errf
(
"redeclaration of previously declared macro %s"
,
name
)
}
// consume all tokens til matched close brace
tokens
,
err
:=
p
.
macroTokens
()
if
err
!=
nil
{
return
err
}
p
.
definedMacros
[
name
]
=
tokens
// empty block keys so we don't save this block as a real server.
p
.
block
.
Keys
=
nil
return
nil
}
return
p
.
blockContents
()
return
p
.
blockContents
()
}
}
...
@@ -221,7 +241,15 @@ func (p *parser) doImport() error {
...
@@ -221,7 +241,15 @@ func (p *parser) doImport() error {
if
p
.
NextArg
()
{
if
p
.
NextArg
()
{
return
p
.
Err
(
"Import takes only one argument (glob pattern or file)"
)
return
p
.
Err
(
"Import takes only one argument (glob pattern or file)"
)
}
}
// splice out the import directive and its argument (2 tokens total)
tokensBefore
:=
p
.
tokens
[
:
p
.
cursor
-
1
]
tokensAfter
:=
p
.
tokens
[
p
.
cursor
+
1
:
]
var
importedTokens
[]
Token
// first check macros. That is a simple, non-recursive replacement
if
p
.
definedMacros
[
importPattern
]
!=
nil
{
importedTokens
=
p
.
definedMacros
[
importPattern
]
}
else
{
// make path relative to Caddyfile rather than current working directory (issue #867)
// make path relative to Caddyfile rather than current working directory (issue #867)
// and then use glob to get list of matching filenames
// and then use glob to get list of matching filenames
absFile
,
err
:=
filepath
.
Abs
(
p
.
Dispenser
.
filename
)
absFile
,
err
:=
filepath
.
Abs
(
p
.
Dispenser
.
filename
)
...
@@ -249,12 +277,8 @@ func (p *parser) doImport() error {
...
@@ -249,12 +277,8 @@ func (p *parser) doImport() error {
}
}
}
}
// splice out the import directive and its argument (2 tokens total)
tokensBefore
:=
p
.
tokens
[
:
p
.
cursor
-
1
]
tokensAfter
:=
p
.
tokens
[
p
.
cursor
+
1
:
]
// collect all the imported tokens
// collect all the imported tokens
var
importedTokens
[]
Token
for
_
,
importFile
:=
range
matches
{
for
_
,
importFile
:=
range
matches
{
newTokens
,
err
:=
p
.
doSingleImport
(
importFile
)
newTokens
,
err
:=
p
.
doSingleImport
(
importFile
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -286,6 +310,7 @@ func (p *parser) doImport() error {
...
@@ -286,6 +310,7 @@ func (p *parser) doImport() error {
importedTokens
=
append
(
importedTokens
,
newTokens
...
)
importedTokens
=
append
(
importedTokens
,
newTokens
...
)
}
}
}
// splice the imported tokens in the place of the import statement
// splice the imported tokens in the place of the import statement
// and rewind cursor so Next() will land on first imported token
// and rewind cursor so Next() will land on first imported token
...
@@ -433,3 +458,45 @@ type ServerBlock struct {
...
@@ -433,3 +458,45 @@ type ServerBlock struct {
Keys
[]
string
Keys
[]
string
Tokens
map
[
string
][]
Token
Tokens
map
[
string
][]
Token
}
}
func
(
p
*
parser
)
isMacro
()
(
bool
,
string
)
{
keys
:=
p
.
block
.
Keys
// "macro foo {}" style
if
len
(
keys
)
==
2
&&
keys
[
0
]
==
"macro"
{
return
true
,
keys
[
1
]
}
// (foo) style. What to do if more than one server key and some have ()?
if
len
(
keys
)
==
1
&&
strings
.
HasPrefix
(
keys
[
0
],
"("
)
&&
strings
.
HasSuffix
(
keys
[
0
],
")"
)
{
return
true
,
strings
.
TrimSuffix
(
keys
[
0
][
1
:
],
")"
)
}
return
false
,
""
}
// read and store everything in a block for later replay.
func
(
p
*
parser
)
macroTokens
()
([]
Token
,
error
)
{
// TODO: disallow imports in macros for simplicity at import time
// macro must have curlies.
err
:=
p
.
openCurlyBrace
()
if
err
!=
nil
{
return
nil
,
err
}
count
:=
1
tokens
:=
[]
Token
{}
for
p
.
Next
()
{
if
p
.
Val
()
==
"}"
{
count
--
if
count
==
0
{
break
}
}
if
p
.
Val
()
==
"{"
{
count
++
}
tokens
=
append
(
tokens
,
p
.
tokens
[
p
.
cursor
])
}
// make sure we're matched up
if
count
!=
0
{
return
nil
,
p
.
SyntaxErr
(
"}"
)
}
return
tokens
,
nil
}
caddyfile/parse_test.go
View file @
425f6114
...
@@ -15,6 +15,7 @@
...
@@ -15,6 +15,7 @@
package
caddyfile
package
caddyfile
import
(
import
(
"fmt"
"io/ioutil"
"io/ioutil"
"os"
"os"
"path/filepath"
"path/filepath"
...
@@ -514,3 +515,43 @@ func testParser(input string) parser {
...
@@ -514,3 +515,43 @@ func testParser(input string) parser {
p
:=
parser
{
Dispenser
:
NewDispenser
(
"Caddyfile"
,
buf
)}
p
:=
parser
{
Dispenser
:
NewDispenser
(
"Caddyfile"
,
buf
)}
return
p
return
p
}
}
func
TestMacro
(
t
*
testing
.
T
)
{
for
_
,
tst
:=
range
[]
string
{
"(common)"
,
"macro common"
}
{
t
.
Run
(
tst
,
func
(
t
*
testing
.
T
)
{
p
:=
testParser
(
fmt
.
Sprintf
(
`
%s {
gzip foo
errors stderr
}
http://example.com {
import common
}
`
,
tst
))
blocks
,
err
:=
p
.
parseAll
()
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
for
_
,
b
:=
range
blocks
{
t
.
Log
(
b
.
Keys
)
t
.
Log
(
b
.
Tokens
)
}
if
len
(
blocks
)
!=
1
{
t
.
Fatalf
(
"Expect exactly one server block. Got %d."
,
len
(
blocks
))
}
if
actual
,
expected
:=
blocks
[
0
]
.
Keys
[
0
],
"http://example.com"
;
expected
!=
actual
{
t
.
Errorf
(
"Expected server name to be '%s' but was '%s'"
,
expected
,
actual
)
}
if
len
(
blocks
[
0
]
.
Tokens
)
!=
2
{
t
.
Fatalf
(
"Server block should have tokens from import"
)
}
if
actual
,
expected
:=
blocks
[
0
]
.
Tokens
[
"gzip"
][
0
]
.
Text
,
"gzip"
;
expected
!=
actual
{
t
.
Errorf
(
"Expected argument to be '%s' but was '%s'"
,
expected
,
actual
)
}
if
actual
,
expected
:=
blocks
[
0
]
.
Tokens
[
"errors"
][
1
]
.
Text
,
"stderr"
;
expected
!=
actual
{
t
.
Errorf
(
"Expected argument to be '%s' but was '%s'"
,
expected
,
actual
)
}
})
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment