Skip to content

Commit 1654e4a

Browse files
committed
fix: yaml parser error when host config not exist
1 parent b55be3e commit 1654e4a

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

internal/parser/yaml.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,25 @@ func GroupYAMLConfig(input string) []Define.HostConfig {
8686
hostConfig := originConfig
8787
hostConfig.Name = hostName
8888
hostConfig.Extra.Prefix = prefix
89+
if hostConfig.Config != nil {
90+
if groupConfig.Common != nil {
91+
for key, value := range groupConfig.Common {
8992

90-
if groupConfig.Common != nil {
91-
for key, value := range groupConfig.Common {
92-
if _, ok := hostConfig.Config[key]; !ok {
93-
hostConfig.Config[key] = value
93+
if _, ok := hostConfig.Config[key]; !ok {
94+
hostConfig.Config[key] = value
95+
}
9496
}
9597
}
96-
}
9798

98-
if yamlConfig.Default != nil {
99-
for key, value := range yamlConfig.Default {
100-
if _, ok := hostConfig.Config[key]; !ok {
101-
hostConfig.Config[key] = value
99+
if yamlConfig.Default != nil {
100+
for key, value := range yamlConfig.Default {
101+
if _, ok := hostConfig.Config[key]; !ok {
102+
hostConfig.Config[key] = value
103+
}
102104
}
103105
}
106+
hostConfigs = append(hostConfigs, hostConfig)
104107
}
105-
hostConfigs = append(hostConfigs, hostConfig)
106108
}
107109
}
108110
}

internal/parser/yaml_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,28 @@ func TestConvertToYAML(t *testing.T) {
103103
},
104104
},
105105
},
106+
{
107+
name: "Both global and normal config",
108+
input: []Define.HostConfig{
109+
{
110+
Name: "host1",
111+
Notes: "note1",
112+
Config: nil,
113+
},
114+
},
115+
expected: Define.YAMLOutput{
116+
Groups: map[string]Define.GroupConfig{
117+
"Group host1": {
118+
Common: make(map[string]string),
119+
Hosts: map[string]Define.HostConfig{
120+
"host1": {
121+
Notes: "note1",
122+
},
123+
},
124+
},
125+
},
126+
},
127+
},
106128
}
107129

108130
for _, tt := range tests {

0 commit comments

Comments
 (0)