Skip to content

Commit

Permalink
fix: re-download cli-templates if there is invalid yaml (#842)
Browse files Browse the repository at this point in the history
  • Loading branch information
davemooreuws authored Jan 7, 2025
1 parent 91ea6a3 commit d145a18
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions pkg/project/templates/contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (d *downloader) GetByLabel(label string) *TemplateInfo {
return nil
}

func (d *downloader) readTemplatesConfig() ([]TemplateInfo, error) {
func (d *downloader) readTemplatesConfig(client GetterClient, retryCount int) ([]TemplateInfo, error) {
file, err := os.Open(d.configPath)
if err != nil {
return nil, err
Expand All @@ -135,6 +135,24 @@ func (d *downloader) readTemplatesConfig() ([]TemplateInfo, error) {
repo := repository{}

if err := decoder.Decode(&repo); err != nil {
// if an error occurs while decoding the yaml file, delete the file and try again based on the retry count
if retryCount > 0 {
// close the file before deleting it
file.Close()

err = os.Remove(d.configPath)
if err != nil {
return nil, errors.WithMessage(err, "repository file "+d.configPath)
}

err = client.Get()
if err != nil {
return nil, errors.WithMessage(err, "repository file "+d.configPath)
}

return d.readTemplatesConfig(client, retryCount-1)
}

return nil, errors.WithMessage(err, "repository file "+d.configPath)
}

Expand Down Expand Up @@ -166,7 +184,7 @@ func (d *downloader) repository() error {
return err
}

list, err := d.readTemplatesConfig()
list, err := d.readTemplatesConfig(client, 1)
if err != nil {
return err
}
Expand Down

0 comments on commit d145a18

Please sign in to comment.