Skip to content

Commit

Permalink
Set up Positional enum
Browse files Browse the repository at this point in the history
  • Loading branch information
francislavoie committed Oct 7, 2023
1 parent da9e024 commit af5bfc2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
20 changes: 15 additions & 5 deletions caddyconfig/httpcaddyfile/directives.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,15 @@ func RegisterHandlerDirective(dir string, setupFunc UnmarshalHandlerFunc) {
// to ('before' or 'after') a directive included in Caddy's
// standard distribution. It cannot be relative to another
// plugin's directive.
func RegisterDirectiveOrder(dir string, position string, standardDir string) {
func RegisterDirectiveOrder(dir string, position Positional, standardDir string) {
// check if directive was already ordered
if directiveIsOrdered(dir) {
panic("directive '" + dir + "' already ordered")
}

switch position {
case "before":
case "after":
case Before:
case After:
default:
panic("the 2nd argument must be either 'before' or 'after', got '" + position + "'")
}
Expand All @@ -183,9 +183,9 @@ func RegisterDirectiveOrder(dir string, position string, standardDir string) {
if d != standardDir {
continue
}
if position == "before" {
if position == Before {
newOrder = append(newOrder[:i], append([]string{dir}, newOrder[i:]...)...)
} else if position == "after" {
} else if position == After {
newOrder = append(newOrder[:i+1], append([]string{dir}, newOrder[i+1:]...)...)
}
break
Expand Down Expand Up @@ -623,6 +623,16 @@ func (sb serverBlock) isAllHTTP() bool {
return true
}

// Positional are the supported modes for ordering directives.
type Positional string

const (
Before Positional = "before"
After Positional = "after"
First Positional = "first"
Last Positional = "last"
)

type (
// UnmarshalFunc is a function which can unmarshal Caddyfile
// tokens into zero or more config values using a Helper type.
Expand Down
14 changes: 7 additions & 7 deletions caddyconfig/httpcaddyfile/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func parseOptOrder(d *caddyfile.Dispenser, _ any) (any, error) {
if !d.Next() {
return nil, d.ArgErr()
}
pos := d.Val()
pos := Positional(d.Val())

// if directive exists, first remove it
for i, d := range newOrder {
Expand All @@ -122,22 +122,22 @@ func parseOptOrder(d *caddyfile.Dispenser, _ any) (any, error) {

// act on the positional
switch pos {
case "first":
case First:
newOrder = append([]string{dirName}, newOrder...)
if d.NextArg() {
return nil, d.ArgErr()
}
directiveOrder = newOrder
return newOrder, nil
case "last":
case Last:
newOrder = append(newOrder, dirName)
if d.NextArg() {
return nil, d.ArgErr()
}
directiveOrder = newOrder
return newOrder, nil
case "before":
case "after":
case Before:
case After:
default:
return nil, d.Errf("unknown positional '%s'", pos)
}
Expand All @@ -154,9 +154,9 @@ func parseOptOrder(d *caddyfile.Dispenser, _ any) (any, error) {
// insert directive into proper position
for i, d := range newOrder {
if d == otherDir {
if pos == "before" {
if pos == Before {
newOrder = append(newOrder[:i], append([]string{dirName}, newOrder[i:]...)...)
} else if pos == "after" {
} else if pos == After {
newOrder = append(newOrder[:i+1], append([]string{dirName}, newOrder[i+1:]...)...)
}
break
Expand Down

0 comments on commit af5bfc2

Please sign in to comment.