This commit is contained in:
admju
2024-09-10 13:05:48 +00:00
parent 13025746e6
commit 75b7b94a50
10 changed files with 277 additions and 37 deletions

View File

@@ -4,15 +4,11 @@ import (
"fmt"
"strings"
"errors"
"os/exec"
"encoding/json"
log "oc-deploy/log_wrapper"
)
// type KubeContext struct {
// Bin string // Chemin vers le binaire
// }
type kubeConfig struct {
CurrentContext string `json:"current-context"`
@@ -43,7 +39,7 @@ type kubeConfigClusters struct {
func (this KubectlCommand) GetCurrentContext() (string, error) {
bin := this.Bin
msg := fmt.Sprintf("%s get config current-context", bin)
msg := fmt.Sprintf("%s config current-context", bin)
log.Log().Debug().Msg(msg)
cmd_args := strings.Split(msg, " ")
@@ -56,8 +52,7 @@ func (this KubectlCommand) GetCurrentContext() (string, error) {
return res, err
}
// Current Context
// namespace, server
// currentContext, currentNamespace, currentServer
func (this KubectlCommand) GetContext() (string, string, string, error) {
bin := this.Bin
@@ -101,9 +96,14 @@ func (this KubectlCommand) GetContext() (string, string, string, error) {
func (this KubectlCommand) UseContext(newContext string) (error) {
cmd := exec.Command(this.Bin, "config", "use-context", newContext)
stdout, err := cmd.CombinedOutput()
bin := this.Bin
msg := fmt.Sprintf("%s config use-context %s", bin, newContext)
log.Log().Debug().Msg(msg)
cmd_args := strings.Split(msg, " ")
cmd := this.Exec(cmd_args[0], cmd_args[1:]...)
stdout, err := cmd.CombinedOutput()
if err != nil {
log.Log().Debug().Msg(string(stdout))
return errors.New(string(stdout))