Skip to content
This repository was archived by the owner on Sep 1, 2025. It is now read-only.

Commit 8b64fe0

Browse files
author
George Vine
committed
MINOR: global: add support for default-path
1 parent bb83e77 commit 8b64fe0

8 files changed

Lines changed: 312 additions & 1 deletion

File tree

parsers/default-path.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright 2022 HAProxy Technologies
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package parsers
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
23+
"github.com/haproxytech/config-parser/v4/common"
24+
"github.com/haproxytech/config-parser/v4/errors"
25+
"github.com/haproxytech/config-parser/v4/types"
26+
)
27+
28+
type DefaultPath struct {
29+
data *types.DefaultPath
30+
preComments []string // comments that appear before the actual line
31+
}
32+
33+
/*
34+
default-path { current | config | parent | origin <path> }
35+
*/
36+
func (d *DefaultPath) Parse(line string, parts []string, comment string) (string, error) {
37+
if len(parts) > 1 && parts[0] == "default-path" &&
38+
(parts[1] == "current" || parts[1] == "config" || parts[1] == "parent" || parts[1] == "origin") {
39+
data := &types.DefaultPath{
40+
Type: parts[1],
41+
Comment: comment,
42+
}
43+
if parts[1] == "origin" {
44+
if len(parts) != 3 {
45+
return "", errors.ErrInvalidData
46+
}
47+
data.Path = parts[2]
48+
}
49+
d.data = data
50+
return "", nil
51+
}
52+
return "", &errors.ParseError{Parser: "default-path", Line: line}
53+
}
54+
55+
func (d *DefaultPath) Result() ([]common.ReturnResultLine, error) {
56+
if d.data == nil {
57+
return nil, errors.ErrFetch
58+
}
59+
var sb strings.Builder
60+
fmt.Fprint(&sb, "default-path ", d.data.Type)
61+
if d.data.Type == "origin" {
62+
fmt.Fprint(&sb, " ", d.data.Path)
63+
}
64+
return []common.ReturnResultLine{
65+
{
66+
Data: sb.String(),
67+
Comment: d.data.Comment,
68+
},
69+
}, nil
70+
}

parsers/default-path_generated.go

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

section-parsers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ func (p *configParser) getGlobalParser() *Parsers {
212212
addParser(parser, &sequence, &simple.ArrayKeyValue{Name: "setenv"})
213213
addParser(parser, &sequence, &simple.StringSlice{Name: "unsetenv"})
214214
addParser(parser, &sequence, &parsers.Daemon{})
215+
addParser(parser, &sequence, &parsers.DefaultPath{})
215216
addParser(parser, &sequence, &simple.String{Name: "localpeer"})
216217
addParser(parser, &sequence, &simple.Word{Name: "chroot"})
217218
addParser(parser, &sequence, &simple.Number{Name: "uid"})
@@ -328,7 +329,6 @@ func (p *configParser) getGlobalParser() *Parsers {
328329
addParser(parser, &sequence, &simple.String{Name: "wurfl-information-list-separator"})
329330
addParser(parser, &sequence, &simple.String{Name: "wurfl-patch-file"})
330331
addParser(parser, &sequence, &simple.Number{Name: "wurfl-cache-size"})
331-
addParser(parser, &sequence, &simple.String{Name: "default-path"})
332332
addParser(parser, &sequence, &simple.String{Name: "description"})
333333
addParser(parser, &sequence, &simple.Enabled{Name: "expose-experimental-directives"})
334334
addParser(parser, &sequence, &simple.String{Name: "grace"})

tests/configs/haproxy_generated.cfg.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/default-path_generated_test.go

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/global_data_test.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/global_test.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

types/types.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,3 +1474,22 @@ type NumaCPUMapping struct {
14741474
NoOption bool
14751475
Comment string
14761476
}
1477+
1478+
//sections:global
1479+
//name:default-path
1480+
//no:parse
1481+
//test:ok:default-path current
1482+
//test:ok:default-path config
1483+
//test:ok:default-path parent
1484+
//test:ok:default-path origin /some/path
1485+
//test:ok:default-path current # comment
1486+
//test:ok:default-path origin /some/path # comment
1487+
//test:fail:default-path
1488+
//test:fail:option default-path unrecognized
1489+
//test:fail:option default-path origin
1490+
//test:fail:option default-path origin # comment
1491+
type DefaultPath struct {
1492+
Type string
1493+
Path string
1494+
Comment string
1495+
}

0 commit comments

Comments
 (0)