Improve configure defaultValue handling
- simplify code - fix comset defaults type presentation issues
This commit is contained in:
parent
05bd765623
commit
ac8af9674e
6 changed files with 13 additions and 22 deletions
|
|
@ -112,7 +112,7 @@ func (c *CmdConfigure) askYesNo(label string) bool {
|
|||
|
||||
type question struct {
|
||||
label, help string
|
||||
defaultValue, exampleValue interface{}
|
||||
defaultValue, exampleValue string
|
||||
invalidValues []string
|
||||
valueType string
|
||||
minNumberValue, maxNumberValue int64
|
||||
|
|
@ -206,7 +206,7 @@ func (c *CmdConfigure) askValue(q question) string {
|
|||
} else {
|
||||
help += " (" + c.localizedString("Value_Optional", nil) + ")"
|
||||
}
|
||||
if q.exampleValue != nil && q.exampleValue != "" {
|
||||
if q.exampleValue != "" {
|
||||
help += fmt.Sprintf(" ("+c.localizedString("Value_Sample", nil)+": %s)", q.exampleValue)
|
||||
}
|
||||
|
||||
|
|
@ -220,18 +220,9 @@ func (c *CmdConfigure) askValue(q question) string {
|
|||
} else {
|
||||
prompt := &survey.Input{
|
||||
Message: q.label,
|
||||
Default: q.defaultValue,
|
||||
Help: help,
|
||||
}
|
||||
if q.defaultValue != nil {
|
||||
switch q.defaultValue.(type) {
|
||||
case string:
|
||||
prompt.Default = q.defaultValue.(string)
|
||||
case int:
|
||||
prompt.Default = strconv.Itoa(q.defaultValue.(int))
|
||||
case bool:
|
||||
prompt.Default = strconv.FormatBool(q.defaultValue.(bool))
|
||||
}
|
||||
}
|
||||
err = c.surveyAskOne(prompt, &input, survey.WithValidator(validate))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ params:
|
|||
- name: modbus
|
||||
choice: ["rs485"]
|
||||
baudrate: 38400
|
||||
comset: "8E1"
|
||||
comset: 8E1
|
||||
render: |
|
||||
type: abl
|
||||
{{include "modbus" .}}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ params:
|
|||
- name: modbus
|
||||
choice: ["rs485"]
|
||||
baudrate: 9600
|
||||
comset: "8N1"
|
||||
comset: 8N1
|
||||
render: |
|
||||
type: alphatec
|
||||
{{include "modbus" .}}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ params:
|
|||
- name: modbus
|
||||
choice: ["rs485"]
|
||||
baudrate: 19200
|
||||
comset: "8E1"
|
||||
comset: 8E1
|
||||
render: |
|
||||
type: heidelberg
|
||||
{{include "modbus" .}}
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ params:
|
|||
help:
|
||||
de: Kommunikationsparameter für den Adapter
|
||||
en: Communication parameter for the adapter
|
||||
default: "8N1"
|
||||
default: 8N1
|
||||
- name: host
|
||||
description:
|
||||
de: IP-Adresse oder Hostname
|
||||
|
|
|
|||
|
|
@ -57,20 +57,20 @@ func (t *Template) ModbusValues(renderMode string, setDefaults bool, values map[
|
|||
for _, p := range typeParams {
|
||||
values[p.Name] = p.DefaultValue(renderMode)
|
||||
|
||||
var defaultValue interface{}
|
||||
var defaultValue string
|
||||
|
||||
switch p.Name {
|
||||
case ModbusParamNameId:
|
||||
if modbusParam.ID != 0 {
|
||||
defaultValue = modbusParam.ID
|
||||
defaultValue = fmt.Sprintf("%d", modbusParam.ID)
|
||||
}
|
||||
case ModbusParamNamePort:
|
||||
if modbusParam.Port != 0 {
|
||||
defaultValue = modbusParam.Port
|
||||
defaultValue = fmt.Sprintf("%d", modbusParam.Port)
|
||||
}
|
||||
case ModbusParamNameBaudrate:
|
||||
if modbusParam.Baudrate != 0 {
|
||||
defaultValue = modbusParam.Baudrate
|
||||
defaultValue = fmt.Sprintf("%d", modbusParam.Baudrate)
|
||||
}
|
||||
case ModbusParamNameComset:
|
||||
if modbusParam.Comset != "" {
|
||||
|
|
@ -78,12 +78,12 @@ func (t *Template) ModbusValues(renderMode string, setDefaults bool, values map[
|
|||
}
|
||||
}
|
||||
|
||||
if defaultValue == nil {
|
||||
if defaultValue == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if renderMode == TemplateRenderModeInstance {
|
||||
t.SetParamDefault(p.Name, fmt.Sprintf("%d", defaultValue))
|
||||
t.SetParamDefault(p.Name, defaultValue)
|
||||
} else {
|
||||
values[p.Name] = defaultValue
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue