Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StringSliceTrySplit: return a copy of the underlying slice #3870

Merged
merged 2 commits into from Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/crio/main.go
Expand Up @@ -144,7 +144,7 @@ func main() {
}...)

app.Before = func(c *cli.Context) (err error) {
config, err := criocli.GetConfigFromContext(c)
config, err := criocli.GetAndMergeConfigFromContext(c)
if err != nil {
return err
}
Expand Down
11 changes: 11 additions & 0 deletions internal/criocli/criocli.go
Expand Up @@ -27,6 +27,14 @@ func GetConfigFromContext(c *cli.Context) (*libconfig.Config, error) {
if !ok {
return nil, fmt.Errorf("type assertion error when accessing server config")
}
return config, nil
}

func GetAndMergeConfigFromContext(c *cli.Context) (*libconfig.Config, error) {
config, err := GetConfigFromContext(c)
if err != nil {
return nil, err
}
if err := mergeConfig(config, c); err != nil {
return nil, err
}
Expand Down Expand Up @@ -788,6 +796,9 @@ func StringSliceTrySplit(ctx *cli.Context, name string) []string {
"Parsed commma separated CLI flag %q into dedicated values %v",
name, values,
)
} else {
// Copy the slice to avoid the cli flags being overwritten
values = append(values[:0:0], values...)
}

return values
Expand Down
12 changes: 12 additions & 0 deletions internal/criocli/criocli_test.go
Expand Up @@ -46,4 +46,16 @@ var _ = t.Describe("CLI", func() {
Entry("separated", "a", "b", "c"),
)

It("should return a copy of the slice", func() {
// Given
slice.Set("value1")
slice.Set("value2")

// When
res := criocli.StringSliceTrySplit(ctx, flagName)
res[0] = "value3"

// Then
Expect(slice.Value()[0]).To(Equal("value1"))
})
})