From c998224e28d91b1bce6a1188d07af948bc936d77 Mon Sep 17 00:00:00 2001 From: Jaeseok Lee Date: Sun, 22 Dec 2024 16:25:40 +0900 Subject: [PATCH] feat: port is not allowed in extra_args --- config/config.go | 25 ++++++++++++++++++------- doc/config_schema.json | 2 +- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index f94176c..b8e5223 100644 --- a/config/config.go +++ b/config/config.go @@ -84,7 +84,7 @@ type preset struct { TcpPort *int `toml:"tcp_port"` LayerIcons map[string]string `toml:"layer_icons"` Hooks *hooks `toml:"hooks"` - ExtraArgs []string `toml:"extra_args"` + ExtraArgs extraArgs `toml:"extra_args"` } func (p *preset) applyDefaults(defaults *preset) { @@ -109,11 +109,7 @@ func (p *preset) applyDefaults(defaults *preset) { p.Hooks = defaults.Hooks } if p.ExtraArgs == nil { - if defaults.ExtraArgs == nil { - p.ExtraArgs = []string{} - } else { - p.ExtraArgs = defaults.ExtraArgs - } + p.ExtraArgs = defaults.ExtraArgs } } @@ -142,7 +138,11 @@ func (p *preset) intoExported() (*Preset, error) { result.Hooks = *x } if p.ExtraArgs != nil { - result.ExtraArgs = p.ExtraArgs + x, err := p.ExtraArgs.intoExported() + if err != nil { + return nil, err + } + result.ExtraArgs = x } return result, nil } @@ -180,6 +180,17 @@ func (p *hooks) intoExported() (*Hooks, error) { }, nil } +type extraArgs []string + +func (e extraArgs) intoExported() ([]string, error) { + for _, s := range e { + if strings.HasPrefix(s, "--port") || strings.HasPrefix(s, "-p") { + return nil, fmt.Errorf("port argument is not allowed in extra_args, use tcp_port instead") + } + } + return e, nil +} + func ReadConfigOrCreateIfNotExist(configFilePath string) (*Config, error) { var cfg *config = &config{} // Golang map don't keep track of insertion order, so we need to get the diff --git a/doc/config_schema.json b/doc/config_schema.json index 1db0ccc..498ee88 100644 --- a/doc/config_schema.json +++ b/doc/config_schema.json @@ -67,7 +67,7 @@ "items": { "type": "string" }, - "description": "You may pass extra arguments to kanata such as --nodelay, additional --cfg params, etc." + "description": "You may pass extra arguments to kanata except for the port (use tcp_port instead), such as --nodelay, additional --cfg params, etc." } }, "additionalProperties": false,