templatefile/pkg/templater/repo-helm.go

69 lines
1.1 KiB
Go

package templater
import (
"encoding/json"
"fmt"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
)
type HelmRepository struct {
Repository
}
func (hr *HelmRepository) repoExists() (bool, error) {
var repos []HelmRepository
stdOut, stdErr, err := utils.RunSystemCommand("helm", "repo", "list", "-o", "json")
if err != nil {
return false, fmt.Errorf("%s %v", stdErr, err)
}
err = json.Unmarshal(stdOut, &repos)
if err != nil {
return false, err
}
for _, repo := range repos {
if repo.Name == hr.Name {
return true, nil
}
}
return false, nil
}
func (hr *HelmRepository) Add() error {
repoExists, err := hr.repoExists()
if err != nil {
return err
}
if repoExists {
return nil
} else {
_, stdErr, err := utils.RunSystemCommand("helm", "repo", "add", hr.Name, hr.URL)
if err != nil {
return fmt.Errorf("%s %v", stdErr, err)
}
}
return nil
}
func (hr *HelmRepository) Update() error {
return nil
}
func (hr *HelmRepository) Delete() error {
return nil
}
func (hr *HelmRepository) Manage() error {
if hr.Enabled {
return hr.Add()
} else {
return hr.Delete()
}
}