Files
oc-deploy/src/helm/repo.go

97 lines
2.0 KiB
Go
Raw Normal View History

2024-09-02 11:44:44 +00:00
package helm
import (
2024-09-05 07:26:32 +00:00
"fmt"
"strings"
"os/exec"
2024-09-09 07:38:43 +00:00
"encoding/json"
2024-09-02 11:44:44 +00:00
log "oc-deploy/log_wrapper"
2024-09-09 07:38:43 +00:00
"oc-deploy/utils"
2024-09-02 11:44:44 +00:00
)
type HelmRepo struct {
Name string
2024-09-05 07:26:32 +00:00
Repository string // Url du dépôt
ForceUpdate bool
Opts string
2024-09-02 11:44:44 +00:00
}
2024-09-09 07:38:43 +00:00
func (this RealHelmCommand) AddRepository(repo HelmRepo) (string, error) {
2024-09-05 07:26:32 +00:00
helm_bin := this.Bin
2024-09-02 11:44:44 +00:00
2024-09-05 07:26:32 +00:00
force_update := "--force-update=false"
2024-09-09 07:38:43 +00:00
if repo.ForceUpdate {
2024-09-05 07:26:32 +00:00
force_update = "--force-update=true"
2024-09-09 07:38:43 +00:00
} else {
list, _ := this.ListRepository()
if utils.StringInSlice(repo.Name, list) {
return "Existe déjà", nil
}
2024-09-05 07:26:32 +00:00
}
2024-09-02 11:44:44 +00:00
2024-09-09 07:38:43 +00:00
msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, repo.Name, repo.Repository, force_update, repo.Opts)
2024-09-05 07:26:32 +00:00
log.Log().Debug().Msg(msg)
2024-09-02 11:44:44 +00:00
2024-09-09 07:38:43 +00:00
cmd_args := strings.Split(msg, " ")
cmd := exec.Command(cmd_args[0], cmd_args[1:]...)
2024-09-05 07:26:32 +00:00
stdout, err := cmd.CombinedOutput()
2024-09-02 11:44:44 +00:00
2024-09-05 07:26:32 +00:00
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
2024-09-02 11:44:44 +00:00
2024-09-05 07:26:32 +00:00
return res, err
2024-09-02 11:44:44 +00:00
}
2024-09-09 07:38:43 +00:00
type parseList struct {
Name string `json:"name"`
}
func (this RealHelmCommand) ListRepository() ([]string, error) {
helm_bin := this.Bin
res := make([]string, 0, 0)
msg := fmt.Sprintf("%s repo list -o json", helm_bin)
log.Log().Debug().Msg(msg)
cmd_args := strings.Split(msg, " ")
cmd := exec.Command(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput()
if err != nil {
return res, err
}
var objmap []parseList
err = json.Unmarshal([]byte(stdout), &objmap)
if err != nil {
return res, err
}
for _, ele := range objmap {
res = append(res, ele.Name)
}
return res, err
}
2024-09-02 11:44:44 +00:00
// helm repo remove [NAME]
2024-09-09 07:38:43 +00:00
func (this RealHelmCommand) RemoveRepository(repo HelmRepo) (string, error) {
2024-09-05 07:26:32 +00:00
helm_bin := this.Bin
2024-09-02 11:44:44 +00:00
2024-09-09 07:38:43 +00:00
msg := fmt.Sprintf("%s repo remove %s", helm_bin, repo.Name)
2024-09-05 07:26:32 +00:00
log.Log().Debug().Msg(msg)
2024-09-02 11:44:44 +00:00
2024-09-09 07:38:43 +00:00
cmd := exec.Command(helm_bin, "repo", "remove", repo.Name)
2024-09-05 07:26:32 +00:00
stdout, err := cmd.CombinedOutput()
2024-09-02 11:44:44 +00:00
2024-09-05 07:26:32 +00:00
res := string(stdout)
res = strings.TrimSuffix(res, "\n")
2024-09-02 11:44:44 +00:00
2024-09-05 07:26:32 +00:00
return res, err
2024-09-02 11:44:44 +00:00
}