This commit is contained in:
admju
2024-09-09 07:38:43 +00:00
parent da9aab90eb
commit 53a614bd7e
9 changed files with 123 additions and 45 deletions

View File

@@ -1,38 +1,16 @@
package helm
import (
"fmt"
"strings"
"errors"
"os/exec"
log "oc-deploy/log_wrapper"
)
type HelmCommandInterface interface {
Status(string) (string, error)
Status(string) (string, error)
AddRepository(HelmRepo) (string, error)
}
type HelmCommandData struct {
bin string
// name string
Bin string
}
type RealHelmCommandStatus HelmCommandData
type RealHelmCommand HelmCommandData
func (this RealHelmCommandStatus) Status(name string) (string, error) {
msg := fmt.Sprintf("%s status %s --show-resources -o json", this.bin, name)
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 {
log.Log().Debug().Msg(string(stdout))
return "", errors.New(string(stdout))
}
return string(stdout), nil
}

View File

@@ -4,30 +4,39 @@ import (
"fmt"
"strings"
"os/exec"
"encoding/json"
log "oc-deploy/log_wrapper"
"oc-deploy/utils"
)
type HelmRepo struct {
Bin string // Chemin vers le binaire
Name string
Repository string // Url du dépôt
ForceUpdate bool
Opts string
}
func (this HelmRepo) AddRepository() (string, error) {
func (this RealHelmCommand) AddRepository(repo HelmRepo) (string, error) {
helm_bin := this.Bin
force_update := "--force-update=false"
if this.ForceUpdate {
if repo.ForceUpdate {
force_update = "--force-update=true"
} else {
list, _ := this.ListRepository()
if utils.StringInSlice(repo.Name, list) {
return "Existe déjà", nil
}
}
msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, this.Name, this.Repository, force_update, this.Opts)
msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, repo.Name, repo.Repository, force_update, repo.Opts)
log.Log().Debug().Msg(msg)
cmd := exec.Command(helm_bin, "repo", "add", this.Name, this.Repository, force_update)
cmd_args := strings.Split(msg, " ")
cmd := exec.Command(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput()
res := string(stdout)
@@ -36,14 +45,48 @@ func (this HelmRepo) AddRepository() (string, error) {
return res, err
}
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
}
// helm repo remove [NAME]
func (this HelmRepo) RemoveRepository() (string, error) {
func (this RealHelmCommand) RemoveRepository(repo HelmRepo) (string, error) {
helm_bin := this.Bin
msg := fmt.Sprintf("%s repo remove %s", helm_bin, this.Name)
msg := fmt.Sprintf("%s repo remove %s", helm_bin, repo.Name)
log.Log().Debug().Msg(msg)
cmd := exec.Command(helm_bin, "repo", "remove", this.Name)
cmd := exec.Command(helm_bin, "repo", "remove", repo.Name)
stdout, err := cmd.CombinedOutput()
res := string(stdout)

View File

@@ -11,7 +11,7 @@ type HelmStatus struct {
}
func (this *HelmStatus) New(bin string) {
this.command = RealHelmCommandStatus{bin: bin}
this.command = RealHelmCommand{Bin: bin}
}
func (this *HelmStatus) NewMock(mock HelmCommandInterface) {

30
src/helm/status.go Normal file
View File

@@ -0,0 +1,30 @@
package helm
import (
"fmt"
"strings"
"errors"
"os/exec"
log "oc-deploy/log_wrapper"
)
func (this RealHelmCommand) Status(name string) (string, error) {
helm_bin := this.Bin
msg := fmt.Sprintf("%s status %s --show-resources -o json", helm_bin, name)
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 {
log.Log().Debug().Msg(string(stdout))
return "", errors.New(string(stdout))
}
return string(stdout), nil
}