Skip to content

Commit

Permalink
feat: provide an option to overwrite some args in AppendAll
Browse files Browse the repository at this point in the history
See siderolabs/talos#3011

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
  • Loading branch information
smira authored and talos-bot committed Jan 8, 2021
1 parent 24d06a9 commit 8cbc42d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
39 changes: 38 additions & 1 deletion procfs/cmdline.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,47 @@ func (c *Cmdline) Append(k, v string) {
insert(&c.Parameters, k, v)
}

// AppendAllOptions provides additional options for AppendAll.
type AppendAllOptions struct {
OverwriteArgs []string
}

// AppendAllOption is a functional option for AppendAll.
type AppendAllOption func(*AppendAllOptions)

// WithOverwriteArgs specifies kernel arguments which should be overwritten with AppendAll.
func WithOverwriteArgs(args ...string) AppendAllOption {
return func(opts *AppendAllOptions) {
opts.OverwriteArgs = append(opts.OverwriteArgs, args...)
}
}

// AppendAll appends a set of kernel parameters.
func (c *Cmdline) AppendAll(args []string) error {
func (c *Cmdline) AppendAll(args []string, opts ...AppendAllOption) error {
var options AppendAllOptions

for _, opt := range opts {
opt(&options)
}

parameters := parse(strings.Join(args, " "))
for _, p := range parameters {
overwrite := false

for _, key := range options.OverwriteArgs {
if key == p.key {
overwrite = true

break
}
}

if overwrite {
c.Set(p.key, p)

continue
}

for _, v := range p.values {
c.Append(p.key, v)
}
Expand Down
17 changes: 16 additions & 1 deletion procfs/cmdline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,21 +146,36 @@ func (suite *KernelSuite) TestCmdlineAppendAll() {
for _, t := range []struct {
initial string
params []string
opts []AppendAllOption
expected string
}{
{
"ip=dhcp console=x root=/dev/sdc",
[]string{"root=/dev/sda", "root=/dev/sdb"},
nil,
"ip=dhcp console=x root=/dev/sdc root=/dev/sda root=/dev/sdb",
},
{
"root=/dev/sdb",
[]string{"this=that=those"},
nil,
"root=/dev/sdb this=that=those",
},
{
"console=tty0 console=ttyS0 root=/dev/sdb",
[]string{"console=tty0", "console=ttyS1,115200", "nogui"},
nil,
"console=tty0 console=ttyS0 console=tty0 console=ttyS1,115200 root=/dev/sdb nogui",
},
{
"console=tty0 console=ttyS0 root=/dev/sdb",
[]string{"console=tty0", "console=ttyS1,115200", "nogui"},
[]AppendAllOption{WithOverwriteArgs("console")},
"console=tty0 console=ttyS1,115200 root=/dev/sdb nogui",
},
} {
cmdline := NewCmdline(t.initial)
err := cmdline.AppendAll(t.params)
err := cmdline.AppendAll(t.params, t.opts...)
visited := map[string]bool{}

for _, p := range cmdline.Parameters {
Expand Down

0 comments on commit 8cbc42d

Please sign in to comment.