diff options
author | Toby Allen <[email protected]> | 2017-01-14 06:42:00 +0000 |
---|---|---|
committer | Matt Holt <[email protected]> | 2017-01-13 23:42:00 -0700 |
commit | 21d92d68739714347cfebb47a339e650464fa581 (patch) | |
tree | 149f3526ab1e136eebf0ff77886de1c42ce9ce8b /caddy.go | |
parent | 696792781a5a8e9c70959b6c2de414d2700257e5 (diff) | |
download | caddy-21d92d68739714347cfebb47a339e650464fa581.tar.gz caddy-21d92d68739714347cfebb47a339e650464fa581.zip |
Add a cli parameter to -validate a Caddyfile. Issue #1328 (#1344)
* Allow -validate flag to validate caddyfile and return
* Ensure logging without -log flag
* Changes to validate seperatly to Starup func
* Removed change to Start signature. Created function to ValidateCaddyfile
* comment and tidyup
* ValidateandExecuteDirectives with justValidate option
* remove debugging code
* Tidy up comments
* additional parameter added to calls to mustLogFataf
* ValidateAndExecuteDirectives needs to only return err
Diffstat (limited to 'caddy.go')
-rw-r--r-- | caddy.go | 83 |
1 files changed, 50 insertions, 33 deletions
@@ -432,31 +432,7 @@ func startWithListenerFds(cdyfile Input, inst *Instance, restartFds map[string]r cdyfile = CaddyfileInput{} } - stypeName := cdyfile.ServerType() - - stype, err := getServerType(stypeName) - if err != nil { - return err - } - - inst.caddyfileInput = cdyfile - - sblocks, err := loadServerBlocks(stypeName, cdyfile.Path(), bytes.NewReader(cdyfile.Body())) - if err != nil { - return err - } - - inst.context = stype.NewContext() - if inst.context == nil { - return fmt.Errorf("server type %s produced a nil Context", stypeName) - } - - sblocks, err = inst.context.InspectServerBlocks(cdyfile.Path(), sblocks) - if err != nil { - return err - } - - err = executeDirectives(inst, cdyfile.Path(), stype.Directives(), sblocks) + err := ValidateAndExecuteDirectives(cdyfile, inst, false) if err != nil { return err } @@ -516,9 +492,48 @@ func startWithListenerFds(cdyfile Input, inst *Instance, restartFds map[string]r return nil } -func executeDirectives(inst *Instance, filename string, - directives []string, sblocks []caddyfile.ServerBlock) error { +func ValidateAndExecuteDirectives(cdyfile Input, inst *Instance, justValidate bool) error { + + // If parsing only inst will be nil, create an instance for this function call only. + if justValidate { + inst = &Instance{serverType: cdyfile.ServerType(), wg: new(sync.WaitGroup)} + } + + stypeName := cdyfile.ServerType() + + stype, err := getServerType(stypeName) + if err != nil { + return err + } + + inst.caddyfileInput = cdyfile + + sblocks, err := loadServerBlocks(stypeName, cdyfile.Path(), bytes.NewReader(cdyfile.Body())) + if err != nil { + return err + } + + inst.context = stype.NewContext() + if inst.context == nil { + return fmt.Errorf("server type %s produced a nil Context", stypeName) + } + sblocks, err = inst.context.InspectServerBlocks(cdyfile.Path(), sblocks) + if err != nil { + return err + } + + err = executeDirectives(inst, cdyfile.Path(), stype.Directives(), sblocks, justValidate) + if err != nil { + return err + } + + return nil + +} + +func executeDirectives(inst *Instance, filename string, + directives []string, sblocks []caddyfile.ServerBlock, justValidate bool) error { // map of server block ID to map of directive name to whatever. storages := make(map[int]map[string]interface{}) @@ -568,12 +583,14 @@ func executeDirectives(inst *Instance, filename string, } } - // See if there are any callbacks to execute after this directive - if allCallbacks, ok := parsingCallbacks[inst.serverType]; ok { - callbacks := allCallbacks[dir] - for _, callback := range callbacks { - if err := callback(inst.context); err != nil { - return err + if !justValidate { + // See if there are any callbacks to execute after this directive + if allCallbacks, ok := parsingCallbacks[inst.serverType]; ok { + callbacks := allCallbacks[dir] + for _, callback := range callbacks { + if err := callback(inst.context); err != nil { + return err + } } } } |