From 9cf954776f1eab0c1074fc1910c1cee2310ed068 Mon Sep 17 00:00:00 2001 From: admju Date: Mon, 2 Sep 2024 11:44:44 +0000 Subject: [PATCH] format Unix --- src/chart/conf.go | 68 +++++----- src/chart/conf_test.go | 60 ++++----- src/chart/main_test.go | 44 +++---- src/cmd/generateCmd.go | 52 ++++---- src/cmd/installCmd.go | 88 ++++++------- src/cmd/uninstallCmd.go | 84 ++++++------ src/helm/chart.go | 258 ++++++++++++++++++------------------- src/helm/main_test.go | 46 +++---- src/helm/repo.go | 106 +++++++-------- src/helm/repo_test.go | 44 +++---- src/helm/version.go | 40 +++--- src/helm/version_test.go | 42 +++--- src/install/common.go | 180 +++++++++++++------------- src/kubectl/context.go | 216 +++++++++++++++---------------- src/kubectl/object.go | 162 +++++++++++------------ src/kubectl/version.go | 62 ++++----- src/tool/conf.go | 34 ++--- src/tool/helm.go | 104 +++++++-------- src/tool/kubectl.go | 82 ++++++------ src/tool/tool.go | 160 +++++++++++------------ src/utils/copyFile.go | 68 +++++----- src/utils/copyFile_test.go | 94 +++++++------- src/utils/download_test.go | 56 ++++---- src/utils/main_test.go | 44 +++---- 24 files changed, 1097 insertions(+), 1097 deletions(-) diff --git a/src/chart/conf.go b/src/chart/conf.go index a6b1e3a..bf380a5 100644 --- a/src/chart/conf.go +++ b/src/chart/conf.go @@ -1,34 +1,34 @@ -package chart - -import ( - "os" - "gopkg.in/yaml.v2" -) - -type ChartData struct { - Name string `yaml:"name"` - Chart string `yaml:"chart"` - Version string `yaml:"version"` - - Opts string `yaml:"helm_opts"` - Values string `yaml:"helm_values"` - FileValues string `yaml:"helm_filevalues"` - -} - -type ChartRepoData struct { - Name string `yaml:"name"` - Repository string `yaml:"repository"` - Charts []ChartData `yaml:"charts"` -} - -type chartsRepoData struct { - Charts []ChartRepoData `yaml:"opencloud"` -} - -func FromConfigFile(filename string) ([]ChartRepoData) { - yamlFile, _ := os.ReadFile(filename) - var data chartsRepoData - yaml.Unmarshal(yamlFile, &data) - return data.Charts -} +package chart + +import ( + "os" + "gopkg.in/yaml.v2" +) + +type ChartData struct { + Name string `yaml:"name"` + Chart string `yaml:"chart"` + Version string `yaml:"version"` + + Opts string `yaml:"helm_opts"` + Values string `yaml:"helm_values"` + FileValues string `yaml:"helm_filevalues"` + +} + +type ChartRepoData struct { + Name string `yaml:"name"` + Repository string `yaml:"repository"` + Charts []ChartData `yaml:"charts"` +} + +type chartsRepoData struct { + Charts []ChartRepoData `yaml:"opencloud"` +} + +func FromConfigFile(filename string) ([]ChartRepoData) { + yamlFile, _ := os.ReadFile(filename) + var data chartsRepoData + yaml.Unmarshal(yamlFile, &data) + return data.Charts +} diff --git a/src/chart/conf_test.go b/src/chart/conf_test.go index 05e1b71..7a4be9c 100644 --- a/src/chart/conf_test.go +++ b/src/chart/conf_test.go @@ -1,30 +1,30 @@ -package chart - -// https://pkg.go.dev/github.com/stretchr/testify/assert - -import ( - "testing" - "path/filepath" - "github.com/stretchr/testify/assert" -) - - -func TestReadConfChart(t *testing.T){ - src := filepath.Join(TEST_SRC_DIR, "oc.yml") - - assert.FileExists(t, src, "FromConfigFile error") - - data := FromConfigFile(src) - assert.Equal(t, data[0].Name, "bitnami", "FromConfigFile error") - assert.Equal(t, data[0].Repository, "https://charts.bitnami.com/bitnami", "FromConfigFile error") - - wordpress := data[0].Charts[0] - assert.Equal(t, wordpress.Name, "wordpress", "FromConfigFile error") - assert.Equal(t, wordpress.Chart, "bitnami/wordpress", "FromConfigFile error") - assert.Equal(t, wordpress.Version, "23.1.0", "FromConfigFile error") - - phpmyadmin := data[0].Charts[1] - assert.Equal(t, phpmyadmin.Name, "phpmyadmin", "FromConfigFile error") - assert.Equal(t, phpmyadmin.Chart, "bitnami/phpmyadmin", "FromConfigFile error") - assert.Equal(t, phpmyadmin.Version, "17.0.4", "FromConfigFile error") -} +package chart + +// https://pkg.go.dev/github.com/stretchr/testify/assert + +import ( + "testing" + "path/filepath" + "github.com/stretchr/testify/assert" +) + + +func TestReadConfChart(t *testing.T){ + src := filepath.Join(TEST_SRC_DIR, "oc.yml") + + assert.FileExists(t, src, "FromConfigFile error") + + data := FromConfigFile(src) + assert.Equal(t, data[0].Name, "bitnami", "FromConfigFile error") + assert.Equal(t, data[0].Repository, "https://charts.bitnami.com/bitnami", "FromConfigFile error") + + wordpress := data[0].Charts[0] + assert.Equal(t, wordpress.Name, "wordpress", "FromConfigFile error") + assert.Equal(t, wordpress.Chart, "bitnami/wordpress", "FromConfigFile error") + assert.Equal(t, wordpress.Version, "23.1.0", "FromConfigFile error") + + phpmyadmin := data[0].Charts[1] + assert.Equal(t, phpmyadmin.Name, "phpmyadmin", "FromConfigFile error") + assert.Equal(t, phpmyadmin.Chart, "bitnami/phpmyadmin", "FromConfigFile error") + assert.Equal(t, phpmyadmin.Version, "17.0.4", "FromConfigFile error") +} diff --git a/src/chart/main_test.go b/src/chart/main_test.go index 3b82b4a..8cee456 100644 --- a/src/chart/main_test.go +++ b/src/chart/main_test.go @@ -1,23 +1,23 @@ -package chart - -import ( - "os" - "testing" - "path/filepath" -) - -var TEST_DEST_DIR = "../wrk_chart" -var TEST_SRC_DIR = filepath.Join("../../test", "chart") - -func TestMain(m *testing.M) { - folderPath := TEST_DEST_DIR - - os.RemoveAll(folderPath) - os.MkdirAll(folderPath, os.ModePerm) - - // call flag.Parse() here if TestMain uses flags - exitCode := m.Run() - - os.RemoveAll(folderPath) - os.Exit(exitCode) +package chart + +import ( + "os" + "testing" + "path/filepath" +) + +var TEST_DEST_DIR = "../wrk_chart" +var TEST_SRC_DIR = filepath.Join("../../test", "chart") + +func TestMain(m *testing.M) { + folderPath := TEST_DEST_DIR + + os.RemoveAll(folderPath) + os.MkdirAll(folderPath, os.ModePerm) + + // call flag.Parse() here if TestMain uses flags + exitCode := m.Run() + + os.RemoveAll(folderPath) + os.Exit(exitCode) } \ No newline at end of file diff --git a/src/cmd/generateCmd.go b/src/cmd/generateCmd.go index 22d38aa..7486e76 100644 --- a/src/cmd/generateCmd.go +++ b/src/cmd/generateCmd.go @@ -1,27 +1,27 @@ -package cmd - -import ( - - log "oc-deploy/log_wrapper" - - "oc-deploy/versionOc" - "oc-deploy/generate" -) - -func GenerateCmd(project string, version string) { - log.Log().Info().Msg("Generate >> ") - - version, err := versionOc.Get(version) - if err != nil { - log.Log().Fatal().Msg("OpenCloud >> " + err.Error()) - } - log.Log().Info().Msg(" >> Version : " + version) - - obj := generate.GenerateClass{Workspace: "workspace_" + project, Version: version} - fic, err := obj.New() - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - log.Log().Info().Msg(" >> Value : " + fic) - +package cmd + +import ( + + log "oc-deploy/log_wrapper" + + "oc-deploy/versionOc" + "oc-deploy/generate" +) + +func GenerateCmd(project string, version string) { + log.Log().Info().Msg("Generate >> ") + + version, err := versionOc.Get(version) + if err != nil { + log.Log().Fatal().Msg("OpenCloud >> " + err.Error()) + } + log.Log().Info().Msg(" >> Version : " + version) + + obj := generate.GenerateClass{Workspace: "workspace_" + project, Version: version} + fic, err := obj.New() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + log.Log().Info().Msg(" >> Value : " + fic) + } \ No newline at end of file diff --git a/src/cmd/installCmd.go b/src/cmd/installCmd.go index 750ed4a..f39710a 100644 --- a/src/cmd/installCmd.go +++ b/src/cmd/installCmd.go @@ -1,45 +1,45 @@ -package cmd - -import ( - "fmt" - log "oc-deploy/log_wrapper" - - "oc-deploy/install" -) - -func InstallCmd(context string, version string) { - log.Log().Info().Msg("Install >> ") - - log.Log().Info().Msg(" << Contexte : " + context) - - workspace := fmt.Sprintf("workspace_%s", context) - obj := install.InstallClass{Workspace: workspace, Version: version} - - file, err := obj.NewInstall() - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - log.Log().Info().Msg(fmt.Sprintf(" << Version : %s", obj.Version)) - log.Log().Info().Msg(fmt.Sprintf(" >> Config : %s", file)) - - err = obj.Tools() - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - - err = obj.ChartRepo() - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - - err = obj.K8s(context) - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - - err = obj.InstallCharts() - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - +package cmd + +import ( + "fmt" + log "oc-deploy/log_wrapper" + + "oc-deploy/install" +) + +func InstallCmd(context string, version string) { + log.Log().Info().Msg("Install >> ") + + log.Log().Info().Msg(" << Contexte : " + context) + + workspace := fmt.Sprintf("workspace_%s", context) + obj := install.InstallClass{Workspace: workspace, Version: version} + + file, err := obj.NewInstall() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + log.Log().Info().Msg(fmt.Sprintf(" << Version : %s", obj.Version)) + log.Log().Info().Msg(fmt.Sprintf(" >> Config : %s", file)) + + err = obj.Tools() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + + err = obj.ChartRepo() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + + err = obj.K8s(context) + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + + err = obj.InstallCharts() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + } \ No newline at end of file diff --git a/src/cmd/uninstallCmd.go b/src/cmd/uninstallCmd.go index c2b6d6a..3e77d64 100644 --- a/src/cmd/uninstallCmd.go +++ b/src/cmd/uninstallCmd.go @@ -1,43 +1,43 @@ -package cmd - -import ( - "fmt" - // "strings" - // "github.com/spf13/cobra" - - log "oc-deploy/log_wrapper" - - // "oc-deploy/versionOc" - "oc-deploy/install" -) - -func UninstallCmd(context string) { - log.Log().Info().Msg("Unnstall >> ") - - log.Log().Info().Msg(" << Contexte : " + context) - - workspace := fmt.Sprintf("workspace_%s", context) - obj := install.InstallClass{Workspace: workspace} - - file, err := obj.NewUninstall() - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - log.Log().Info().Msg(fmt.Sprintf(" << Version : %s", obj.Version)) - log.Log().Info().Msg(fmt.Sprintf(" >> Config : %s", file)) - - err = obj.Tools() - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - - err = obj.K8s(context) - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } - - err = obj.UninstallCharts() - if err != nil { - log.Log().Fatal().Msg(" >> " + err.Error()) - } +package cmd + +import ( + "fmt" + // "strings" + // "github.com/spf13/cobra" + + log "oc-deploy/log_wrapper" + + // "oc-deploy/versionOc" + "oc-deploy/install" +) + +func UninstallCmd(context string) { + log.Log().Info().Msg("Unnstall >> ") + + log.Log().Info().Msg(" << Contexte : " + context) + + workspace := fmt.Sprintf("workspace_%s", context) + obj := install.InstallClass{Workspace: workspace} + + file, err := obj.NewUninstall() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + log.Log().Info().Msg(fmt.Sprintf(" << Version : %s", obj.Version)) + log.Log().Info().Msg(fmt.Sprintf(" >> Config : %s", file)) + + err = obj.Tools() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + + err = obj.K8s(context) + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } + + err = obj.UninstallCharts() + if err != nil { + log.Log().Fatal().Msg(" >> " + err.Error()) + } } \ No newline at end of file diff --git a/src/helm/chart.go b/src/helm/chart.go index 8761581..97d7719 100644 --- a/src/helm/chart.go +++ b/src/helm/chart.go @@ -1,129 +1,129 @@ -package helm - -import ( - "fmt" - "os" - "os/exec" - "strings" - "errors" - "path/filepath" - "encoding/json" - log "oc-deploy/log_wrapper" -) - -type HelmChart struct { - Bin string - Name string - Chart string - Version string - Workspace string - Opts string - Values string - FileValues string -} - -type installInfoOutput struct { - Description string `json:"description"` - Notes string `json:"notes"` - Status string `json:"status"` -} - -type installOutput struct { - Info installInfoOutput `json:"info"` -} - -func (this HelmChart) Install() (string, error) { - bin := this.Bin - - existe, err := this.exists() - if err != nil { - return "", err - } - - if existe { - return "Existe déjà", nil - } - - msg := fmt.Sprintf("%s install %s %s %s --output json", bin, this.Name, this.Chart, this.Opts) - - if this.Version != "" { - msg = fmt.Sprintf("%s --version %s", msg, this.Version) - } - - if this.FileValues != "" { - fic := filepath.Join(this.Workspace, this.FileValues) - if _, err := os.Stat(fic); err != nil { - log.Log().Warn().Msg(fic) - } else { - msg = fmt.Sprintf("%s --values %s", msg, fic) - } - } - - msg = strings.Replace(msg, " ", " ", -1) - - 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 { - res := string(stdout) - res = strings.TrimSuffix(res, "\n") - return "", errors.New(res) - } - - var objmap installOutput - - json.Unmarshal(stdout, &objmap) - - res := objmap.Info.Status - - return res, nil -} - -func (this HelmChart) Uninstall() (string, error) { - bin := this.Bin - - log.Log().Info().Msg(" >> Chart : " + this.Name) - - existe, err := this.exists() - if err != nil { - return "", err - } - if ! existe { - return "Non présent", nil - } - - msg := fmt.Sprintf("%s uninstall %s", bin, this.Name) - log.Log().Debug().Msg(msg) - - cmd := exec.Command(bin, "uninstall", this.Name) - stdout, err := cmd.CombinedOutput() - - return string(stdout), err -} - -// ../bin/helm list --filter phpmyadminm --short -func (this HelmChart) exists() (bool, error) { - bin := this.Bin - - msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, this.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 { - return false, errors.New(string(stdout)) - } - - res := string(stdout) - res = strings.TrimSuffix(res, "\n") - - log.Log().Debug().Msg(string(stdout)) - - return res != "", nil -} +package helm + +import ( + "fmt" + "os" + "os/exec" + "strings" + "errors" + "path/filepath" + "encoding/json" + log "oc-deploy/log_wrapper" +) + +type HelmChart struct { + Bin string + Name string + Chart string + Version string + Workspace string + Opts string + Values string + FileValues string +} + +type installInfoOutput struct { + Description string `json:"description"` + Notes string `json:"notes"` + Status string `json:"status"` +} + +type installOutput struct { + Info installInfoOutput `json:"info"` +} + +func (this HelmChart) Install() (string, error) { + bin := this.Bin + + existe, err := this.exists() + if err != nil { + return "", err + } + + if existe { + return "Existe déjà", nil + } + + msg := fmt.Sprintf("%s install %s %s %s --output json", bin, this.Name, this.Chart, this.Opts) + + if this.Version != "" { + msg = fmt.Sprintf("%s --version %s", msg, this.Version) + } + + if this.FileValues != "" { + fic := filepath.Join(this.Workspace, this.FileValues) + if _, err := os.Stat(fic); err != nil { + log.Log().Warn().Msg(fic) + } else { + msg = fmt.Sprintf("%s --values %s", msg, fic) + } + } + + msg = strings.Replace(msg, " ", " ", -1) + + 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 { + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + return "", errors.New(res) + } + + var objmap installOutput + + json.Unmarshal(stdout, &objmap) + + res := objmap.Info.Status + + return res, nil +} + +func (this HelmChart) Uninstall() (string, error) { + bin := this.Bin + + log.Log().Info().Msg(" >> Chart : " + this.Name) + + existe, err := this.exists() + if err != nil { + return "", err + } + if ! existe { + return "Non présent", nil + } + + msg := fmt.Sprintf("%s uninstall %s", bin, this.Name) + log.Log().Debug().Msg(msg) + + cmd := exec.Command(bin, "uninstall", this.Name) + stdout, err := cmd.CombinedOutput() + + return string(stdout), err +} + +// ../bin/helm list --filter phpmyadminm --short +func (this HelmChart) exists() (bool, error) { + bin := this.Bin + + msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, this.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 { + return false, errors.New(string(stdout)) + } + + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + + log.Log().Debug().Msg(string(stdout)) + + return res != "", nil +} diff --git a/src/helm/main_test.go b/src/helm/main_test.go index 7fa572c..7c5042e 100644 --- a/src/helm/main_test.go +++ b/src/helm/main_test.go @@ -1,24 +1,24 @@ -package helm - -import ( - "os" - "testing" - "path/filepath" -) - -var TEST_DEST_DIR = "../wrk_helm" -var TEST_SRC_DIR = filepath.Join("../../test", "helm") -var TEST_BIN_DIR = filepath.Join("../../test", "bin") - -func TestMain(m *testing.M) { - folderPath := TEST_DEST_DIR - - os.RemoveAll(folderPath) - os.MkdirAll(folderPath, os.ModePerm) - - // call flag.Parse() here if TestMain uses flags - exitCode := m.Run() - - os.RemoveAll(folderPath) - os.Exit(exitCode) +package helm + +import ( + "os" + "testing" + "path/filepath" +) + +var TEST_DEST_DIR = "../wrk_helm" +var TEST_SRC_DIR = filepath.Join("../../test", "helm") +var TEST_BIN_DIR = filepath.Join("../../test", "bin") + +func TestMain(m *testing.M) { + folderPath := TEST_DEST_DIR + + os.RemoveAll(folderPath) + os.MkdirAll(folderPath, os.ModePerm) + + // call flag.Parse() here if TestMain uses flags + exitCode := m.Run() + + os.RemoveAll(folderPath) + os.Exit(exitCode) } \ No newline at end of file diff --git a/src/helm/repo.go b/src/helm/repo.go index 0b9da56..de1dbf3 100644 --- a/src/helm/repo.go +++ b/src/helm/repo.go @@ -1,53 +1,53 @@ -package helm - -import ( - "fmt" - "strings" - "os/exec" - - log "oc-deploy/log_wrapper" -) - -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) { - helm_bin := this.Bin - - force_update := "--force-update=false" - if this.ForceUpdate { - force_update = "--force-update=true" - } - - msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, this.Name, this.Repository, force_update, this.Opts) - log.Log().Debug().Msg(msg) - - cmd := exec.Command(helm_bin, "repo", "add", this.Name, this.Repository, force_update) - stdout, err := cmd.CombinedOutput() - - res := string(stdout) - res = strings.TrimSuffix(res, "\n") - - return res, err -} - -// helm repo remove [NAME] -func (this HelmRepo) RemoveRepository() (string, error) { - helm_bin := this.Bin - - msg := fmt.Sprintf("%s repo remove %s", helm_bin, this.Name) - log.Log().Debug().Msg(msg) - - cmd := exec.Command(helm_bin, "repo", "remove", this.Name) - stdout, err := cmd.CombinedOutput() - - res := string(stdout) - res = strings.TrimSuffix(res, "\n") - - return res, err -} +package helm + +import ( + "fmt" + "strings" + "os/exec" + + log "oc-deploy/log_wrapper" +) + +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) { + helm_bin := this.Bin + + force_update := "--force-update=false" + if this.ForceUpdate { + force_update = "--force-update=true" + } + + msg := fmt.Sprintf("%s repo add %s %s %s %s", helm_bin, this.Name, this.Repository, force_update, this.Opts) + log.Log().Debug().Msg(msg) + + cmd := exec.Command(helm_bin, "repo", "add", this.Name, this.Repository, force_update) + stdout, err := cmd.CombinedOutput() + + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + + return res, err +} + +// helm repo remove [NAME] +func (this HelmRepo) RemoveRepository() (string, error) { + helm_bin := this.Bin + + msg := fmt.Sprintf("%s repo remove %s", helm_bin, this.Name) + log.Log().Debug().Msg(msg) + + cmd := exec.Command(helm_bin, "repo", "remove", this.Name) + stdout, err := cmd.CombinedOutput() + + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + + return res, err +} diff --git a/src/helm/repo_test.go b/src/helm/repo_test.go index 9cedc24..38f8d39 100644 --- a/src/helm/repo_test.go +++ b/src/helm/repo_test.go @@ -1,23 +1,23 @@ -package helm - -import ( - // "os" - // "path/filepath" - - "testing" - - // "github.com/stretchr/testify/assert" -) - -func TestHelmRepoAdd(t *testing.T){ - // fmt.Println(" TestVersion ", TEST_BIN_DIR) - - // bin := filepath.Join(TEST_BIN_DIR, "helm") - // os.Chmod(bin, 0700) - // assert.FileExists(t, bin, "TestHelmVersion error") - - // version, err := Version(bin) - - // assert.Nilf(t, err, "error message %s", bin) - // assert.Equal(t, version, "v3.15.4+gfa9efb0", "TestHelmVersion error") +package helm + +import ( + // "os" + // "path/filepath" + + "testing" + + // "github.com/stretchr/testify/assert" +) + +func TestHelmRepoAdd(t *testing.T){ + // fmt.Println(" TestVersion ", TEST_BIN_DIR) + + // bin := filepath.Join(TEST_BIN_DIR, "helm") + // os.Chmod(bin, 0700) + // assert.FileExists(t, bin, "TestHelmVersion error") + + // version, err := Version(bin) + + // assert.Nilf(t, err, "error message %s", bin) + // assert.Equal(t, version, "v3.15.4+gfa9efb0", "TestHelmVersion error") } \ No newline at end of file diff --git a/src/helm/version.go b/src/helm/version.go index 8c9eb21..5f3a088 100644 --- a/src/helm/version.go +++ b/src/helm/version.go @@ -1,20 +1,20 @@ -package helm - -import ( - "strings" - "os/exec" -) - -func Version(path string) (string, error) { - - cmd := exec.Command(path, "version", "--short") - stdout, err := cmd.CombinedOutput() - - if err != nil { - return "", err - } - - res := string(stdout) - res = strings.TrimSuffix(res, "\n") - return res, nil -} +package helm + +import ( + "strings" + "os/exec" +) + +func Version(path string) (string, error) { + + cmd := exec.Command(path, "version", "--short") + stdout, err := cmd.CombinedOutput() + + if err != nil { + return "", err + } + + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + return res, nil +} diff --git a/src/helm/version_test.go b/src/helm/version_test.go index 1b9a081..52d244f 100644 --- a/src/helm/version_test.go +++ b/src/helm/version_test.go @@ -1,22 +1,22 @@ -package helm - -import ( - "os" - "path/filepath" - - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestHelmVersion(t *testing.T){ - - bin := filepath.Join(TEST_BIN_DIR, "helm") - os.Chmod(bin, 0700) - assert.FileExists(t, bin, "TestHelmVersion error") - - version, err := Version(bin) - - assert.Nilf(t, err, "error message %s", bin) - assert.Equal(t, version, "v3.15.4+gfa9efb0", "TestHelmVersion error") +package helm + +import ( + "os" + "path/filepath" + + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestHelmVersion(t *testing.T){ + + bin := filepath.Join(TEST_BIN_DIR, "helm") + os.Chmod(bin, 0700) + assert.FileExists(t, bin, "TestHelmVersion error") + + version, err := Version(bin) + + assert.Nilf(t, err, "error message %s", bin) + assert.Equal(t, version, "v3.15.4+gfa9efb0", "TestHelmVersion error") } \ No newline at end of file diff --git a/src/install/common.go b/src/install/common.go index 737c196..5ec3694 100644 --- a/src/install/common.go +++ b/src/install/common.go @@ -1,90 +1,90 @@ -package install - -import ( - "fmt" - "errors" - - log "oc-deploy/log_wrapper" - "oc-deploy/tool" - "oc-deploy/kubectl" -) - -func (this *InstallClass) Tools() (error) { - - var mem []tool.ToolClass - - for _, v := range this.tools { - - tool2 := tool.ToolClass{} - v.Bin = this.Workspace - err := tool2.New(v) - if err != nil { - return err - } - mem = append(mem,tool2) - } - - this.toolsBin = make(map[string]string) - - for _, p := range mem { - data := p.Obj.Get() - log.Log().Info().Msg(fmt.Sprintf(" >> Outils : %s", data.Name)) - err := p.Locate() - if err != nil { - log.Log().Info().Msg(fmt.Sprintf(" << %s ", err)) - return err - } - log.Log().Info().Msg(fmt.Sprintf(" << %s ", p.Path)) - version, err1 := p.Version() - if err1 != nil { - log.Log().Info().Msg(fmt.Sprintf(" << %s ", err1)) - return err1 - } - log.Log().Info().Msg(fmt.Sprintf(" << %s ", version)) - - this.toolsBin[data.Name] = p.Path - } - - return nil -} - -func (this *InstallClass) getToolBin(name string) (string, error) { - for key, value := range this.toolsBin { - if key == name { - return value, nil - } - } - - return "", errors.New("Error") -} - -func (this *InstallClass) K8s(context string) (error) { - bin_path, _ := this.getToolBin("kubectl") - - kube := kubectl.KubeContext{Bin: bin_path} - - err := kube.UseContext(context) - if err != nil { - log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", err)) - return err - } - - currentcontext, namespace, server, err := kube.GetContext() - if err != nil { - log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", err)) - return err - } - log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", currentcontext)) - - log.Log().Info().Msg(fmt.Sprintf(" << : %s ", namespace)) - log.Log().Info().Msg(fmt.Sprintf(" << : %s ", server)) - - err = kube.Check() - if err != nil { - log.Log().Info().Msg(fmt.Sprintf(" << : %s ", err)) - return err - } - log.Log().Info().Msg(fmt.Sprintf(" << : %s ", "OK")) - - return nil -} +package install + +import ( + "fmt" + "errors" + + log "oc-deploy/log_wrapper" + "oc-deploy/tool" + "oc-deploy/kubectl" +) + +func (this *InstallClass) Tools() (error) { + + var mem []tool.ToolClass + + for _, v := range this.tools { + + tool2 := tool.ToolClass{} + v.Bin = this.Workspace + err := tool2.New(v) + if err != nil { + return err + } + mem = append(mem,tool2) + } + + this.toolsBin = make(map[string]string) + + for _, p := range mem { + data := p.Obj.Get() + log.Log().Info().Msg(fmt.Sprintf(" >> Outils : %s", data.Name)) + err := p.Locate() + if err != nil { + log.Log().Info().Msg(fmt.Sprintf(" << %s ", err)) + return err + } + log.Log().Info().Msg(fmt.Sprintf(" << %s ", p.Path)) + version, err1 := p.Version() + if err1 != nil { + log.Log().Info().Msg(fmt.Sprintf(" << %s ", err1)) + return err1 + } + log.Log().Info().Msg(fmt.Sprintf(" << %s ", version)) + + this.toolsBin[data.Name] = p.Path + } + + return nil +} + +func (this *InstallClass) getToolBin(name string) (string, error) { + for key, value := range this.toolsBin { + if key == name { + return value, nil + } + } + + return "", errors.New("Error") +} + +func (this *InstallClass) K8s(context string) (error) { + bin_path, _ := this.getToolBin("kubectl") + + kube := kubectl.KubeContext{Bin: bin_path} + + err := kube.UseContext(context) + if err != nil { + log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", err)) + return err + } + + currentcontext, namespace, server, err := kube.GetContext() + if err != nil { + log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", err)) + return err + } + log.Log().Info().Msg(fmt.Sprintf(" << Kube : %s ", currentcontext)) + + log.Log().Info().Msg(fmt.Sprintf(" << : %s ", namespace)) + log.Log().Info().Msg(fmt.Sprintf(" << : %s ", server)) + + err = kube.Check() + if err != nil { + log.Log().Info().Msg(fmt.Sprintf(" << : %s ", err)) + return err + } + log.Log().Info().Msg(fmt.Sprintf(" << : %s ", "OK")) + + return nil +} diff --git a/src/kubectl/context.go b/src/kubectl/context.go index 7b99706..39b5292 100644 --- a/src/kubectl/context.go +++ b/src/kubectl/context.go @@ -1,108 +1,108 @@ -package kubectl - -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"` - Contexts [] kubeConfigContexts `json:"contexts"` - Clusters [] kubeConfigClusters `json:"clusters"` -} - -type kubeConfigContexts struct { - Name string `json:"name"` - Context kubeConfigContext `json:"context"` -} - -type kubeConfigContext struct { - Cluster string `json:"cluster"` - User string `json:"user"` - Namespace string `json:"namespace"` -} - -type kubeConfigCluster struct { - Server string `json:"server"` -} - -type kubeConfigClusters struct { - Name string `json:"name"` - Cluster kubeConfigCluster `json:"cluster"` -} - -func (this KubeContext) GetCurrentContext() (string, error) { - - cmd := exec.Command(this.Bin, "config", "current-context") - stdout, err := cmd.CombinedOutput() - - res := string(stdout) - res = strings.TrimSuffix(res, "\n") - - return res, err -} - -// Current Context -// namespace, server -func (this KubeContext) GetContext() (string, string, string, error) { - - cmd := exec.Command(this.Bin, "config", "view", "-o", "json") - stdout, _ := cmd.CombinedOutput() - - var objmap kubeConfig - - json.Unmarshal(stdout, &objmap) - currentContext := objmap.CurrentContext - - currentCluster := "" - currentNamespace := "" - for _, v := range objmap.Contexts { - if v.Name == currentContext { - currentNamespace = v.Context.Namespace - currentCluster = v.Context.Cluster - } - } - - currentServer := "" - for _, v := range objmap.Clusters { - if v.Name == currentCluster { - currentServer = v.Cluster.Server - } - } - - return currentContext, currentNamespace, currentServer, nil -} - -func (this KubeContext) UseContext(newContext string) (error) { - - cmd := exec.Command(this.Bin, "config", "use-context", newContext) - stdout, err := cmd.CombinedOutput() - - if err != nil { - log.Log().Debug().Msg(string(stdout)) - return errors.New(string(stdout)) - } - - return nil -} - -func (this KubeContext) Check() (error) { - - cmd := exec.Command(this.Bin, "cluster-info") - stdout, err := cmd.CombinedOutput() - if err != nil { - log.Log().Debug().Msg(string(stdout)) - return errors.New("Kube non disponible") - } - - return nil -} +package kubectl + +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"` + Contexts [] kubeConfigContexts `json:"contexts"` + Clusters [] kubeConfigClusters `json:"clusters"` +} + +type kubeConfigContexts struct { + Name string `json:"name"` + Context kubeConfigContext `json:"context"` +} + +type kubeConfigContext struct { + Cluster string `json:"cluster"` + User string `json:"user"` + Namespace string `json:"namespace"` +} + +type kubeConfigCluster struct { + Server string `json:"server"` +} + +type kubeConfigClusters struct { + Name string `json:"name"` + Cluster kubeConfigCluster `json:"cluster"` +} + +func (this KubeContext) GetCurrentContext() (string, error) { + + cmd := exec.Command(this.Bin, "config", "current-context") + stdout, err := cmd.CombinedOutput() + + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + + return res, err +} + +// Current Context +// namespace, server +func (this KubeContext) GetContext() (string, string, string, error) { + + cmd := exec.Command(this.Bin, "config", "view", "-o", "json") + stdout, _ := cmd.CombinedOutput() + + var objmap kubeConfig + + json.Unmarshal(stdout, &objmap) + currentContext := objmap.CurrentContext + + currentCluster := "" + currentNamespace := "" + for _, v := range objmap.Contexts { + if v.Name == currentContext { + currentNamespace = v.Context.Namespace + currentCluster = v.Context.Cluster + } + } + + currentServer := "" + for _, v := range objmap.Clusters { + if v.Name == currentCluster { + currentServer = v.Cluster.Server + } + } + + return currentContext, currentNamespace, currentServer, nil +} + +func (this KubeContext) UseContext(newContext string) (error) { + + cmd := exec.Command(this.Bin, "config", "use-context", newContext) + stdout, err := cmd.CombinedOutput() + + if err != nil { + log.Log().Debug().Msg(string(stdout)) + return errors.New(string(stdout)) + } + + return nil +} + +func (this KubeContext) Check() (error) { + + cmd := exec.Command(this.Bin, "cluster-info") + stdout, err := cmd.CombinedOutput() + if err != nil { + log.Log().Debug().Msg(string(stdout)) + return errors.New("Kube non disponible") + } + + return nil +} diff --git a/src/kubectl/object.go b/src/kubectl/object.go index 7ad6eec..9aff676 100644 --- a/src/kubectl/object.go +++ b/src/kubectl/object.go @@ -1,82 +1,82 @@ -package kubectl - -import ( - "fmt" - "strings" - "errors" - "time" - "os/exec" - "encoding/json" - log "oc-deploy/log_wrapper" -) - -type KubeObject struct { - Bin string // Chemin vers le binaire - Name string -} - -type getOutput struct { - Kind string `json:"kind"` - Status getStatusOutput `json:"status"` -} - -type getStatusOutput struct { - Replicas int `json:"replicas"` - UnavailableReplicas int `json:"unavailableReplicas"` -} - -func (this KubeObject) Get() (map[string]any, error) { - bin := this.Bin - name := this.Name - - msg := fmt.Sprintf("%s get deployment %s -o json", bin, name) - log.Log().Debug().Msg(msg) - - m := make(map[string]any) - - cmd_args := strings.Split(msg, " ") - - cmd := exec.Command(cmd_args[0], cmd_args[1:]...) - stdout, err := cmd.CombinedOutput() - if err != nil { - return m, errors.New(string(stdout)) - } - - var objmap getOutput - - json.Unmarshal(stdout, &objmap) - - kind := objmap.Kind - status := objmap.Status - - m["name"] = name - m["kind"] = kind - m["replicas"] = status.Replicas - m["UnavailableReplicas"] = status.UnavailableReplicas - - return m, nil -} - -func (this KubeObject) Wait() (error) { - - boucle := 10 - sleep := 10000 * time.Millisecond - - for _ = range boucle { - - log.Log().Debug().Msg(fmt.Sprintf("Check Deployement %s", this.Name)) - - m, err := this.Get() - if err != nil { - return err - } - ko := m["UnavailableReplicas"].(int) - if ko == 0 { - return nil - } - - time.Sleep(sleep) - - } - return errors.New("Temps d'attente dépassé") +package kubectl + +import ( + "fmt" + "strings" + "errors" + "time" + "os/exec" + "encoding/json" + log "oc-deploy/log_wrapper" +) + +type KubeObject struct { + Bin string // Chemin vers le binaire + Name string +} + +type getOutput struct { + Kind string `json:"kind"` + Status getStatusOutput `json:"status"` +} + +type getStatusOutput struct { + Replicas int `json:"replicas"` + UnavailableReplicas int `json:"unavailableReplicas"` +} + +func (this KubeObject) Get() (map[string]any, error) { + bin := this.Bin + name := this.Name + + msg := fmt.Sprintf("%s get deployment %s -o json", bin, name) + log.Log().Debug().Msg(msg) + + m := make(map[string]any) + + cmd_args := strings.Split(msg, " ") + + cmd := exec.Command(cmd_args[0], cmd_args[1:]...) + stdout, err := cmd.CombinedOutput() + if err != nil { + return m, errors.New(string(stdout)) + } + + var objmap getOutput + + json.Unmarshal(stdout, &objmap) + + kind := objmap.Kind + status := objmap.Status + + m["name"] = name + m["kind"] = kind + m["replicas"] = status.Replicas + m["UnavailableReplicas"] = status.UnavailableReplicas + + return m, nil +} + +func (this KubeObject) Wait() (error) { + + boucle := 10 + sleep := 10000 * time.Millisecond + + for _ = range boucle { + + log.Log().Debug().Msg(fmt.Sprintf("Check Deployement %s", this.Name)) + + m, err := this.Get() + if err != nil { + return err + } + ko := m["UnavailableReplicas"].(int) + if ko == 0 { + return nil + } + + time.Sleep(sleep) + + } + return errors.New("Temps d'attente dépassé") } \ No newline at end of file diff --git a/src/kubectl/version.go b/src/kubectl/version.go index 2dc4726..6706e98 100644 --- a/src/kubectl/version.go +++ b/src/kubectl/version.go @@ -1,31 +1,31 @@ -package kubectl - -import ( - "os/exec" - "encoding/json" -) - -type toolClientVersion struct { - GitVersion string `json:"gitVersion"` -} - -type toolVersion struct { - ClientVersion toolClientVersion `json:"clientVersion"` -} - -func Version(path string) (string, error) { - - cmd := exec.Command(path, "version", "-o", "json", "--client=true") - stdout, err := cmd.CombinedOutput() - - if err != nil { - return "", err - } - - var objmap toolVersion - - json.Unmarshal(stdout, &objmap) - res := objmap.ClientVersion.GitVersion - - return res, nil -} +package kubectl + +import ( + "os/exec" + "encoding/json" +) + +type toolClientVersion struct { + GitVersion string `json:"gitVersion"` +} + +type toolVersion struct { + ClientVersion toolClientVersion `json:"clientVersion"` +} + +func Version(path string) (string, error) { + + cmd := exec.Command(path, "version", "-o", "json", "--client=true") + stdout, err := cmd.CombinedOutput() + + if err != nil { + return "", err + } + + var objmap toolVersion + + json.Unmarshal(stdout, &objmap) + res := objmap.ClientVersion.GitVersion + + return res, nil +} diff --git a/src/tool/conf.go b/src/tool/conf.go index a8b6b73..c22f60c 100644 --- a/src/tool/conf.go +++ b/src/tool/conf.go @@ -1,17 +1,17 @@ -package tool - -import ( - "os" - "gopkg.in/yaml.v2" -) - -type toolsData struct { - Tools []ToolData `yaml:"tools"` -} - -func FromConfigFile(filename string) ([]ToolData) { - yamlFile, _ := os.ReadFile(filename) - var data toolsData - yaml.Unmarshal(yamlFile, &data) - return data.Tools -} +package tool + +import ( + "os" + "gopkg.in/yaml.v2" +) + +type toolsData struct { + Tools []ToolData `yaml:"tools"` +} + +func FromConfigFile(filename string) ([]ToolData) { + yamlFile, _ := os.ReadFile(filename) + var data toolsData + yaml.Unmarshal(yamlFile, &data) + return data.Tools +} diff --git a/src/tool/helm.go b/src/tool/helm.go index 33ac173..a8e946c 100644 --- a/src/tool/helm.go +++ b/src/tool/helm.go @@ -1,53 +1,53 @@ -package tool - -import ( - "fmt" - "os" - -// log "oc-deploy/log_wrapper" - "oc-deploy/utils" - "oc-deploy/helm" -) - -type HelmInstallData struct { - obj ToolData - tmp string -} - -func (this HelmInstallData) Get() (ToolData) { - return this.obj -} - -func (this HelmInstallData) Download() (error) { - - bin_dir := this.obj.Bin - err2 := os.MkdirAll(bin_dir, os.ModePerm) - if err2 != nil { - fmt.Println(err2) - return err2 - } - - tmp_file := fmt.Sprintf("%s/oc-deploy-%s", this.tmp, this.obj.Name) - url := fmt.Sprintf(this.obj.Url, this.obj.Version) - - err := utils.DownloadFromUrl(tmp_file, url, 0777) - if err != nil { - return err - } - - r, _ := os.Open(tmp_file) - err1 := utils.ExtractTarGz(bin_dir, r) - - os.Remove(tmp_file) - - bin_file := fmt.Sprintf("%s/%s", bin_dir, this.obj.Name) - - os.Chmod(bin_file, 0755) - - return err1 -} - -/////////////// -func (this HelmInstallData) Version(path string) (string, error) { - return helm.Version(path) +package tool + +import ( + "fmt" + "os" + +// log "oc-deploy/log_wrapper" + "oc-deploy/utils" + "oc-deploy/helm" +) + +type HelmInstallData struct { + obj ToolData + tmp string +} + +func (this HelmInstallData) Get() (ToolData) { + return this.obj +} + +func (this HelmInstallData) Download() (error) { + + bin_dir := this.obj.Bin + err2 := os.MkdirAll(bin_dir, os.ModePerm) + if err2 != nil { + fmt.Println(err2) + return err2 + } + + tmp_file := fmt.Sprintf("%s/oc-deploy-%s", this.tmp, this.obj.Name) + url := fmt.Sprintf(this.obj.Url, this.obj.Version) + + err := utils.DownloadFromUrl(tmp_file, url, 0777) + if err != nil { + return err + } + + r, _ := os.Open(tmp_file) + err1 := utils.ExtractTarGz(bin_dir, r) + + os.Remove(tmp_file) + + bin_file := fmt.Sprintf("%s/%s", bin_dir, this.obj.Name) + + os.Chmod(bin_file, 0755) + + return err1 +} + +/////////////// +func (this HelmInstallData) Version(path string) (string, error) { + return helm.Version(path) } \ No newline at end of file diff --git a/src/tool/kubectl.go b/src/tool/kubectl.go index 150ef0c..c9027b3 100644 --- a/src/tool/kubectl.go +++ b/src/tool/kubectl.go @@ -1,41 +1,41 @@ -package tool - -import ( - "fmt" - "os" - "path/filepath" - - log "oc-deploy/log_wrapper" - "oc-deploy/utils" - "oc-deploy/kubectl" -) - -type KubecltInstallData struct { - obj ToolData -} - -func (this KubecltInstallData) Get() (ToolData) { - return this.obj -} - -func (this KubecltInstallData) Download() (error) { - - bin_dir := this.obj.Bin - bin := filepath.Join(bin_dir, this.obj.Name) - url := fmt.Sprintf(this.obj.Url, this.obj.Version) - - log.Log().Debug().Msg(fmt.Sprintf("Téléchargement : %s, %s", bin, url)) - os.MkdirAll(bin_dir, os.ModePerm) - - err := utils.DownloadFromUrl(bin, url, 0777) - if err != nil {return err} - - os.Chmod(bin, 0755) - - return nil -} - -/////////////// -func (this KubecltInstallData) Version(path string) (string, error) { - return kubectl.Version(path) -} +package tool + +import ( + "fmt" + "os" + "path/filepath" + + log "oc-deploy/log_wrapper" + "oc-deploy/utils" + "oc-deploy/kubectl" +) + +type KubecltInstallData struct { + obj ToolData +} + +func (this KubecltInstallData) Get() (ToolData) { + return this.obj +} + +func (this KubecltInstallData) Download() (error) { + + bin_dir := this.obj.Bin + bin := filepath.Join(bin_dir, this.obj.Name) + url := fmt.Sprintf(this.obj.Url, this.obj.Version) + + log.Log().Debug().Msg(fmt.Sprintf("Téléchargement : %s, %s", bin, url)) + os.MkdirAll(bin_dir, os.ModePerm) + + err := utils.DownloadFromUrl(bin, url, 0777) + if err != nil {return err} + + os.Chmod(bin, 0755) + + return nil +} + +/////////////// +func (this KubecltInstallData) Version(path string) (string, error) { + return kubectl.Version(path) +} diff --git a/src/tool/tool.go b/src/tool/tool.go index 4c75c75..2077f6d 100644 --- a/src/tool/tool.go +++ b/src/tool/tool.go @@ -1,80 +1,80 @@ -package tool - -import ( - "fmt" - "os" - "os/exec" - "errors" - "path/filepath" -) - -type ToolData struct { - Name string `yaml:"name"` - Url string `yaml:"url"` - Version string `yaml:"version"` - Bin string -} - -type ToolClass struct { - Obj Forme - Path string -} - -type Forme interface { - Download() error - Version(string) (string, error) - Get() ToolData -} - -////////// -func (this *ToolClass) New(data ToolData) (error) { - f, err := factory(data) - if err != nil { - return err - } - this.Obj = f - return nil -} - -func (this *ToolClass) Locate() (error) { - - obj := this.Obj - data := obj.Get() - path := filepath.Join(data.Bin, data.Name) - - if _, err := os.Stat(path); err != nil { - path2, _ := exec.LookPath(data.Name) - if path2 != "" { - path = path2 - } else { - err = obj.Download() - if err != nil { - return err - } - } - } - - this.Path = path - return nil -} - -func (this *ToolClass) Version() (string, error) { - obj := (this.Obj) - return obj.Version(this.Path) -} - -////////// -func factory(data ToolData) (Forme, error) { - var f Forme - - switch data.Name { - case "kubectl": - f = KubecltInstallData{obj: data} - case "helm": - f = HelmInstallData{obj: data, tmp: "/tmp"} - default: - return f, errors.New(fmt.Sprintf("Outil Inconnu : %s", data.Name)) - } - - return f, nil -} +package tool + +import ( + "fmt" + "os" + "os/exec" + "errors" + "path/filepath" +) + +type ToolData struct { + Name string `yaml:"name"` + Url string `yaml:"url"` + Version string `yaml:"version"` + Bin string +} + +type ToolClass struct { + Obj Forme + Path string +} + +type Forme interface { + Download() error + Version(string) (string, error) + Get() ToolData +} + +////////// +func (this *ToolClass) New(data ToolData) (error) { + f, err := factory(data) + if err != nil { + return err + } + this.Obj = f + return nil +} + +func (this *ToolClass) Locate() (error) { + + obj := this.Obj + data := obj.Get() + path := filepath.Join(data.Bin, data.Name) + + if _, err := os.Stat(path); err != nil { + path2, _ := exec.LookPath(data.Name) + if path2 != "" { + path = path2 + } else { + err = obj.Download() + if err != nil { + return err + } + } + } + + this.Path = path + return nil +} + +func (this *ToolClass) Version() (string, error) { + obj := (this.Obj) + return obj.Version(this.Path) +} + +////////// +func factory(data ToolData) (Forme, error) { + var f Forme + + switch data.Name { + case "kubectl": + f = KubecltInstallData{obj: data} + case "helm": + f = HelmInstallData{obj: data, tmp: "/tmp"} + default: + return f, errors.New(fmt.Sprintf("Outil Inconnu : %s", data.Name)) + } + + return f, nil +} diff --git a/src/utils/copyFile.go b/src/utils/copyFile.go index dd0dcd4..13271e4 100644 --- a/src/utils/copyFile.go +++ b/src/utils/copyFile.go @@ -1,35 +1,35 @@ -package utils - -import ( - "os" - "io" - "path/filepath" -) - -func CopyFile(src string, dst string) (error) { - - if _, err := os.Stat(src); err != nil { - return err - } - - fin, errOpen := os.Open(src) - if errOpen != nil { - return errOpen - } - defer fin.Close() - - folderPath := filepath.Dir(dst) - os.MkdirAll(folderPath, os.ModePerm) - - fout, errCreate := os.Create(dst) - if errCreate != nil { - return errCreate - } - defer fout.Close() - - _, errCopy := io.Copy(fout, fin) - if errCopy != nil { - return errCopy - } - return nil +package utils + +import ( + "os" + "io" + "path/filepath" +) + +func CopyFile(src string, dst string) (error) { + + if _, err := os.Stat(src); err != nil { + return err + } + + fin, errOpen := os.Open(src) + if errOpen != nil { + return errOpen + } + defer fin.Close() + + folderPath := filepath.Dir(dst) + os.MkdirAll(folderPath, os.ModePerm) + + fout, errCreate := os.Create(dst) + if errCreate != nil { + return errCreate + } + defer fout.Close() + + _, errCopy := io.Copy(fout, fin) + if errCopy != nil { + return errCopy + } + return nil } \ No newline at end of file diff --git a/src/utils/copyFile_test.go b/src/utils/copyFile_test.go index cb73c42..e6adb2a 100644 --- a/src/utils/copyFile_test.go +++ b/src/utils/copyFile_test.go @@ -1,47 +1,47 @@ -package utils - -// https://pkg.go.dev/github.com/stretchr/testify/assert#Nilf - -import ( - "testing" - "path/filepath" - "github.com/stretchr/testify/assert" -) - -func TestCopyFileExist(t *testing.T){ - src := filepath.Join(TEST_SRC_DIR, "fichier1") - dest := filepath.Join(TEST_DEST_DIR, "fichier1") - - err := CopyFile(src, dest) - - assert.Nilf(t, err, "error message %s", src) - assert.FileExists(t, dest, "CopyFile error") -} - -func TestCopyErrSrcFileNotExist(t *testing.T) { - src := filepath.Join(TEST_SRC_DIR, "inconnu") - dest := filepath.Join(TEST_DEST_DIR, "inconnu") - - err := CopyFile(src, dest) - - assert.NotNilf(t, err, "CopyFile error %s", src) -} - -func TestCopyFileErrCreate(t *testing.T){ - src := filepath.Join(TEST_SRC_DIR, "fichier1") - dest := filepath.Join("/INCONNU", "fichier1") - - err := CopyFile(src, dest) - - assert.NotNilf(t, err, "error message %s", src) - assert.NoFileExists(t, dest, "CopyFile error") -} - -func TestCopyErrSrcWrite(t *testing.T) { - src := "/" - dest := filepath.Join(TEST_DEST_DIR, "inconnu") - - err := CopyFile(src, dest) - - assert.NotNilf(t, err, "CopyFile error %s", src) -} +package utils + +// https://pkg.go.dev/github.com/stretchr/testify/assert#Nilf + +import ( + "testing" + "path/filepath" + "github.com/stretchr/testify/assert" +) + +func TestCopyFileExist(t *testing.T){ + src := filepath.Join(TEST_SRC_DIR, "fichier1") + dest := filepath.Join(TEST_DEST_DIR, "fichier1") + + err := CopyFile(src, dest) + + assert.Nilf(t, err, "error message %s", src) + assert.FileExists(t, dest, "CopyFile error") +} + +func TestCopyErrSrcFileNotExist(t *testing.T) { + src := filepath.Join(TEST_SRC_DIR, "inconnu") + dest := filepath.Join(TEST_DEST_DIR, "inconnu") + + err := CopyFile(src, dest) + + assert.NotNilf(t, err, "CopyFile error %s", src) +} + +func TestCopyFileErrCreate(t *testing.T){ + src := filepath.Join(TEST_SRC_DIR, "fichier1") + dest := filepath.Join("/INCONNU", "fichier1") + + err := CopyFile(src, dest) + + assert.NotNilf(t, err, "error message %s", src) + assert.NoFileExists(t, dest, "CopyFile error") +} + +func TestCopyErrSrcWrite(t *testing.T) { + src := "/" + dest := filepath.Join(TEST_DEST_DIR, "inconnu") + + err := CopyFile(src, dest) + + assert.NotNilf(t, err, "CopyFile error %s", src) +} diff --git a/src/utils/download_test.go b/src/utils/download_test.go index 9bfa427..2944a30 100644 --- a/src/utils/download_test.go +++ b/src/utils/download_test.go @@ -1,28 +1,28 @@ -package utils - -// https://pkg.go.dev/github.com/stretchr/testify/assert#Nilf - -import ( - "path/filepath" - - "testing" - - "github.com/stretchr/testify/assert" - "github.com/jarcoal/httpmock" -) - -func TestDownload(t *testing.T){ - httpmock.Activate() - defer httpmock.DeactivateAndReset() - - url := "http://test.download.com" - - httpmock.RegisterResponder("GET", url, - httpmock.NewStringResponder(200, `CONTENU_URL`)) - - dest := filepath.Join(TEST_DEST_DIR, "url") - - err := DownloadFromUrl(dest, url, 777) - assert.Nilf(t, err, "error message %s", url) - assert.FileExists(t, dest, "DownloadFromUrl error") -} +package utils + +// https://pkg.go.dev/github.com/stretchr/testify/assert#Nilf + +import ( + "path/filepath" + + "testing" + + "github.com/stretchr/testify/assert" + "github.com/jarcoal/httpmock" +) + +func TestDownload(t *testing.T){ + httpmock.Activate() + defer httpmock.DeactivateAndReset() + + url := "http://test.download.com" + + httpmock.RegisterResponder("GET", url, + httpmock.NewStringResponder(200, `CONTENU_URL`)) + + dest := filepath.Join(TEST_DEST_DIR, "url") + + err := DownloadFromUrl(dest, url, 777) + assert.Nilf(t, err, "error message %s", url) + assert.FileExists(t, dest, "DownloadFromUrl error") +} diff --git a/src/utils/main_test.go b/src/utils/main_test.go index 41d4414..2128336 100644 --- a/src/utils/main_test.go +++ b/src/utils/main_test.go @@ -1,23 +1,23 @@ -package utils - -import ( - "os" - "testing" - "path/filepath" -) - -var TEST_DEST_DIR = "../wrk_utils" -var TEST_SRC_DIR = filepath.Join("../../test", "utils") - -func TestMain(m *testing.M) { - folderPath := TEST_DEST_DIR - - os.RemoveAll(folderPath) - os.MkdirAll(folderPath, os.ModePerm) - - // call flag.Parse() here if TestMain uses flags - exitCode := m.Run() - - os.RemoveAll(folderPath) - os.Exit(exitCode) +package utils + +import ( + "os" + "testing" + "path/filepath" +) + +var TEST_DEST_DIR = "../wrk_utils" +var TEST_SRC_DIR = filepath.Join("../../test", "utils") + +func TestMain(m *testing.M) { + folderPath := TEST_DEST_DIR + + os.RemoveAll(folderPath) + os.MkdirAll(folderPath, os.ModePerm) + + // call flag.Parse() here if TestMain uses flags + exitCode := m.Run() + + os.RemoveAll(folderPath) + os.Exit(exitCode) } \ No newline at end of file