Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
7bd65047
Unverified
Commit
7bd65047
authored
Nov 08, 2021
by
Derek Nola
Committed by
GitHub
Nov 08, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Match to last After keyword for parser (#4383)
* Made parser able to skip over subcommands * Edge case coverage, reworked regex with groups Signed-off-by:
Derek Nola
<
derek.nola@suse.com
>
parent
8915e4c7
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
3 deletions
+62
-3
defaultparser.go
pkg/configfilearg/defaultparser.go
+1
-1
parser.go
pkg/configfilearg/parser.go
+25
-1
parser_test.go
pkg/configfilearg/parser_test.go
+36
-1
No files found.
pkg/configfilearg/defaultparser.go
View file @
7bd65047
...
...
@@ -7,7 +7,7 @@ import (
func
MustParse
(
args
[]
string
)
[]
string
{
parser
:=
&
Parser
{
After
:
[]
string
{
"server"
,
"agent"
,
"etcd-snapshot"
},
After
:
[]
string
{
"server"
,
"agent"
,
"etcd-snapshot
:1
"
},
FlagNames
:
[]
string
{
"--config"
,
"-c"
},
EnvName
:
version
.
ProgramUpper
+
"_CONFIG_FILE"
,
DefaultConfig
:
"/etc/rancher/"
+
version
.
Program
+
"/config.yaml"
,
...
...
pkg/configfilearg/parser.go
View file @
7bd65047
...
...
@@ -7,6 +7,8 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/rancher/k3s/pkg/agent/util"
...
...
@@ -101,14 +103,36 @@ func (p *Parser) findStart(args []string) ([]string, []string, bool) {
return
[]
string
{},
args
,
true
}
afterIndex
:=
make
(
map
[
string
]
int
)
re
,
err
:=
regexp
.
Compile
(
`(.+):(\d+)`
)
if
err
!=
nil
{
return
args
,
nil
,
false
}
// After keywords ending with ":<NUM>" can set + NUM of arguments as the split point.
// used for matching on subcommmands
for
i
,
arg
:=
range
p
.
After
{
if
match
:=
re
.
FindAllStringSubmatch
(
arg
,
-
1
);
match
!=
nil
{
p
.
After
[
i
]
=
match
[
0
][
1
]
afterIndex
[
match
[
0
][
1
]],
err
=
strconv
.
Atoi
(
match
[
0
][
2
])
if
err
!=
nil
{
return
args
,
nil
,
false
}
}
}
for
i
,
val
:=
range
args
{
for
_
,
test
:=
range
p
.
After
{
if
val
==
test
{
if
skip
:=
afterIndex
[
test
];
skip
!=
0
{
if
len
(
args
)
<=
i
+
skip
||
strings
.
HasPrefix
(
args
[
i
+
skip
],
"-"
)
{
return
args
[
0
:
i
+
1
],
args
[
i
+
1
:
],
true
}
return
args
[
0
:
i
+
skip
+
1
],
args
[
i
+
skip
+
1
:
],
true
}
return
args
[
0
:
i
+
1
],
args
[
i
+
1
:
],
true
}
}
}
return
args
,
nil
,
false
}
...
...
pkg/configfilearg/parser_test.go
View file @
7bd65047
...
...
@@ -48,11 +48,46 @@ func Test_UnitParser_findStart(t *testing.T) {
prefix
:
[]
string
{
"not-server"
,
"foo"
,
"bar"
},
found
:
false
,
},
{
name
:
"command (with optional subcommands) but no flags"
,
args
:
[]
string
{
"etcd-snapshot"
},
prefix
:
[]
string
{
"etcd-snapshot"
},
suffix
:
[]
string
{},
found
:
true
,
},
{
name
:
"command (with optional subcommands) and flags"
,
args
:
[]
string
{
"etcd-snapshot"
,
"-f"
},
prefix
:
[]
string
{
"etcd-snapshot"
},
suffix
:
[]
string
{
"-f"
},
found
:
true
,
},
{
name
:
"command and subcommand with no flags"
,
args
:
[]
string
{
"etcd-snapshot"
,
"list"
},
prefix
:
[]
string
{
"etcd-snapshot"
,
"list"
},
suffix
:
[]
string
{},
found
:
true
,
},
{
name
:
"command and subcommand with flags"
,
args
:
[]
string
{
"etcd-snapshot"
,
"list"
,
"-f"
},
prefix
:
[]
string
{
"etcd-snapshot"
,
"list"
},
suffix
:
[]
string
{
"-f"
},
found
:
true
,
},
{
name
:
"command and too many subcommands"
,
args
:
[]
string
{
"etcd-snapshot"
,
"list"
,
"delete"
,
"foo"
,
"bar"
},
prefix
:
[]
string
{
"etcd-snapshot"
,
"list"
},
suffix
:
[]
string
{
"delete"
,
"foo"
,
"bar"
},
found
:
true
,
},
}
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
p
:=
Parser
{
After
:
[]
string
{
"server"
,
"agent"
},
After
:
[]
string
{
"server"
,
"agent"
,
"etcd-snapshot:1"
},
}
prefix
,
suffix
,
found
:=
p
.
findStart
(
tt
.
args
)
if
!
reflect
.
DeepEqual
(
prefix
,
tt
.
prefix
)
{
...
...
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