Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: re-download cli-templates if there is invalid yaml #842

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading