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
e1d4cff1
Unverified
Commit
e1d4cff1
authored
May 02, 2023
by
Derek Nola
Committed by
GitHub
May 02, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Enable FindString to search dotD config files (#7323)
* Enable FindString to search dotD config files * Address multiple arg cases Signed-off-by:
Derek Nola
<
derek.nola@suse.com
>
parent
132b41c3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
15 deletions
+76
-15
parser.go
pkg/configfilearg/parser.go
+27
-9
parser_test.go
pkg/configfilearg/parser_test.go
+45
-4
data.yaml
pkg/configfilearg/testdata/data.yaml
+1
-0
01-data.yml
pkg/configfilearg/testdata/data.yaml.d/01-data.yml
+3
-2
No files found.
pkg/configfilearg/parser.go
View file @
e1d4cff1
...
@@ -98,28 +98,46 @@ func (p *Parser) stripInvalidFlags(command string, args []string) ([]string, err
...
@@ -98,28 +98,46 @@ func (p *Parser) stripInvalidFlags(command string, args []string) ([]string, err
func
(
p
*
Parser
)
FindString
(
args
[]
string
,
target
string
)
(
string
,
error
)
{
func
(
p
*
Parser
)
FindString
(
args
[]
string
,
target
string
)
(
string
,
error
)
{
configFile
,
isSet
:=
p
.
findConfigFileFlag
(
args
)
configFile
,
isSet
:=
p
.
findConfigFileFlag
(
args
)
var
lastVal
string
if
configFile
!=
""
{
if
configFile
!=
""
{
bytes
,
err
:=
readConfigFileData
(
configFile
)
_
,
err
:=
os
.
Stat
(
configFile
)
if
!
isSet
&&
os
.
IsNotExist
(
err
)
{
if
!
isSet
&&
os
.
IsNotExist
(
err
)
{
return
""
,
nil
return
""
,
nil
}
else
if
err
!=
nil
{
}
else
if
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
data
:=
yaml
.
MapSlice
{}
files
,
err
:=
dotDFiles
(
configFile
)
if
err
:=
yaml
.
Unmarshal
(
bytes
,
&
data
);
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
files
=
append
([]
string
{
configFile
},
files
...
)
for
_
,
file
:=
range
files
{
bytes
,
err
:=
readConfigFileData
(
file
)
if
err
!=
nil
{
return
""
,
err
}
for
_
,
i
:=
range
data
{
data
:=
yaml
.
MapSlice
{}
k
,
v
:=
convert
.
ToString
(
i
.
Key
),
convert
.
ToString
(
i
.
Value
)
if
err
:=
yaml
.
Unmarshal
(
bytes
,
&
data
);
err
!=
nil
{
if
k
==
target
{
return
""
,
err
return
v
,
nil
}
for
_
,
i
:=
range
data
{
k
,
v
:=
convert
.
ToString
(
i
.
Key
),
convert
.
ToString
(
i
.
Value
)
isAppend
:=
strings
.
HasSuffix
(
k
,
"+"
)
k
=
strings
.
TrimSuffix
(
k
,
"+"
)
if
k
==
target
{
if
isAppend
{
lastVal
=
lastVal
+
","
+
v
}
else
{
lastVal
=
v
}
}
}
}
}
}
}
}
return
lastVal
,
nil
return
""
,
nil
}
}
func
(
p
*
Parser
)
findConfigFileFlag
(
args
[]
string
)
(
string
,
bool
)
{
func
(
p
*
Parser
)
findConfigFileFlag
(
args
[]
string
)
(
string
,
bool
)
{
...
...
pkg/configfilearg/parser_test.go
View file @
e1d4cff1
...
@@ -221,6 +221,7 @@ func Test_UnitParser_findConfigFileFlag(t *testing.T) {
...
@@ -221,6 +221,7 @@ func Test_UnitParser_findConfigFileFlag(t *testing.T) {
func
Test_UnitParser_Parse
(
t
*
testing
.
T
)
{
func
Test_UnitParser_Parse
(
t
*
testing
.
T
)
{
testDataOutput
:=
[]
string
{
testDataOutput
:=
[]
string
{
"--foo-bar=bar-foo"
,
"--foo-bar=bar-foo"
,
"--alice=bob"
,
"--a-slice=1"
,
"--a-slice=1"
,
"--a-slice=1.5"
,
"--a-slice=1.5"
,
"--a-slice=2"
,
"--a-slice=2"
,
...
@@ -237,6 +238,7 @@ func Test_UnitParser_Parse(t *testing.T) {
...
@@ -237,6 +238,7 @@ func Test_UnitParser_Parse(t *testing.T) {
"--c-slice=three"
,
"--c-slice=three"
,
"--d-slice=three"
,
"--d-slice=three"
,
"--d-slice=four"
,
"--d-slice=four"
,
"--f-string=beta"
,
"--e-slice=one"
,
"--e-slice=one"
,
"--e-slice=two"
,
"--e-slice=two"
,
}
}
...
@@ -376,7 +378,7 @@ func Test_UnitParser_FindString(t *testing.T) {
...
@@ -376,7 +378,7 @@ func Test_UnitParser_FindString(t *testing.T) {
want
:
""
,
want
:
""
,
},
},
{
{
name
:
"A custom config
yaml
exists, target exists"
,
name
:
"A custom config exists, target exists"
,
fields
:
fields
{
fields
:
fields
{
FlagNames
:
[]
string
{
"-c"
,
"--config"
},
FlagNames
:
[]
string
{
"-c"
,
"--config"
},
EnvName
:
"_TEST_ENV"
,
EnvName
:
"_TEST_ENV"
,
...
@@ -384,12 +386,12 @@ func Test_UnitParser_FindString(t *testing.T) {
...
@@ -384,12 +386,12 @@ func Test_UnitParser_FindString(t *testing.T) {
},
},
args
:
args
{
args
:
args
{
osArgs
:
[]
string
{
"-c"
,
"./testdata/data.yaml"
},
osArgs
:
[]
string
{
"-c"
,
"./testdata/data.yaml"
},
target
:
"
foo-bar
"
,
target
:
"
alice
"
,
},
},
want
:
"b
az
"
,
want
:
"b
ob
"
,
},
},
{
{
name
:
"A custom config
yaml
exists, target does not exist"
,
name
:
"A custom config exists, target does not exist"
,
fields
:
fields
{
fields
:
fields
{
FlagNames
:
[]
string
{
"-c"
,
"--config"
},
FlagNames
:
[]
string
{
"-c"
,
"--config"
},
EnvName
:
"_TEST_ENV"
,
EnvName
:
"_TEST_ENV"
,
...
@@ -401,6 +403,45 @@ func Test_UnitParser_FindString(t *testing.T) {
...
@@ -401,6 +403,45 @@ func Test_UnitParser_FindString(t *testing.T) {
},
},
want
:
""
,
want
:
""
,
},
},
{
name
:
"Multiple custom configs exist, target exists in a secondary config"
,
fields
:
fields
{
FlagNames
:
[]
string
{
"-c"
,
"--config"
},
EnvName
:
"_TEST_ENV"
,
DefaultConfig
:
"./testdata/data.yaml"
,
},
args
:
args
{
osArgs
:
[]
string
{
"-c"
,
"./testdata/data.yaml"
},
target
:
"f-string"
,
},
want
:
"beta"
,
},
{
name
:
"Multiple custom configs exist, multiple targets exist in multiple secondary config, replacement"
,
fields
:
fields
{
FlagNames
:
[]
string
{
"-c"
,
"--config"
},
EnvName
:
"_TEST_ENV"
,
DefaultConfig
:
"./testdata/data.yaml"
,
},
args
:
args
{
osArgs
:
[]
string
{
"-c"
,
"./testdata/data.yaml"
},
target
:
"foo-bar"
,
},
want
:
"bar-foo"
,
},
{
name
:
"Multiple custom configs exist, multiple targets exist in multiple secondary config, appending"
,
fields
:
fields
{
FlagNames
:
[]
string
{
"-c"
,
"--config"
},
EnvName
:
"_TEST_ENV"
,
DefaultConfig
:
"./testdata/data.yaml"
,
},
args
:
args
{
osArgs
:
[]
string
{
"-c"
,
"./testdata/data.yaml"
},
target
:
"b-string"
,
},
want
:
"one,two"
,
},
}
}
for
_
,
tt
:=
range
tests
{
for
_
,
tt
:=
range
tests
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
t
.
Run
(
tt
.
name
,
func
(
t
*
testing
.
T
)
{
...
...
pkg/configfilearg/testdata/data.yaml
View file @
e1d4cff1
foo-bar
:
baz
foo-bar
:
baz
alice
:
bob
a-slice
:
a-slice
:
-
1
-
1
-
"
2"
-
"
2"
...
...
pkg/configfilearg/testdata/data.yaml.d/01-data.yml
View file @
e1d4cff1
...
@@ -11,4 +11,5 @@ c-slice:
...
@@ -11,4 +11,5 @@ c-slice:
-
two
-
two
d-slice
:
d-slice
:
-
one
-
one
-
two
-
two
\ No newline at end of file
f-string
:
beta
\ No newline at end of file
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