@@ -191,34 +191,57 @@ func (c *fileConfig) Write() error {
191191}
192192
193193func (c * fileConfig ) Aliases () (* AliasConfig , error ) {
194+ // The complexity here is for dealing with either a missing or empty aliases key. It's something
195+ // we'll likely want for other config sections at some point.
194196 entry , err := c .FindEntry ("aliases" )
195197 var nfe * NotFoundError
196- if err != nil && errors .As (err , & nfe ) {
197- // TODO does this make sense at all? want to simulate existing but empty key.
198- return & AliasConfig {Parent : c }, nil
199- }
200-
201- aliasConfig , err := c .parseAliasConfig (entry .ValueNode )
202- if err != nil {
198+ notFound := errors .As (err , & nfe )
199+ if err != nil && ! notFound {
203200 return nil , err
204201 }
205202
206- return aliasConfig , nil
207- }
203+ toInsert := []* yaml.Node {}
208204
209- func (c * fileConfig ) parseAliasConfig (aliasesEntry * yaml.Node ) (* AliasConfig , error ) {
210- if aliasesEntry .Kind != yaml .MappingNode {
211- return & AliasConfig {
212- Parent : c ,
213- }, nil
205+ keyNode := entry .KeyNode
206+ valueNode := entry .ValueNode
207+
208+ if keyNode == nil {
209+ keyNode = & yaml.Node {
210+ Kind : yaml .ScalarNode ,
211+ Value : "aliases" ,
212+ }
213+ toInsert = append (toInsert , keyNode )
214214 }
215215
216- aliasConfig := AliasConfig {
217- Parent : c ,
218- ConfigMap : ConfigMap {Root : aliasesEntry },
216+ if valueNode == nil || valueNode .Kind != yaml .MappingNode {
217+ valueNode = & yaml.Node {
218+ Kind : yaml .MappingNode ,
219+ Value : "" ,
220+ }
221+ toInsert = append (toInsert , valueNode )
222+ }
223+
224+ if len (toInsert ) > 0 {
225+ newContent := []* yaml.Node {}
226+ if notFound {
227+ newContent = append (c .Root ().Content , keyNode , valueNode )
228+ } else {
229+ for i := 0 ; i < len (c .Root ().Content ); i ++ {
230+ if i == entry .Index {
231+ newContent = append (newContent , keyNode , valueNode )
232+ i ++
233+ } else {
234+ newContent = append (newContent , c .Root ().Content [i ])
235+ }
236+ }
237+ }
238+ c .Root ().Content = newContent
219239 }
220240
221- return & aliasConfig , nil
241+ return & AliasConfig {
242+ Parent : c ,
243+ ConfigMap : ConfigMap {Root : valueNode },
244+ }, nil
222245}
223246
224247func (c * fileConfig ) Hosts () ([]* HostConfig , error ) {
0 commit comments