2022-06-15 17:29:45 +02:00
|
|
|
package templater
|
|
|
|
|
2022-07-11 09:27:34 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
type HelmRepository struct {
|
|
|
|
Repository
|
|
|
|
}
|
|
|
|
|
2022-07-11 09:27:34 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
func (hr *HelmRepository) Add() error {
|
2022-07-11 09:27:34 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
return nil
|
2022-07-11 09:27:34 +02:00
|
|
|
|
2022-06-15 17:29:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|