From 11a4d5cc9014f64f5187581ed2581c907ab4b8b1 Mon Sep 17 00:00:00 2001 From: admju Date: Tue, 3 Sep 2024 13:18:20 +0000 Subject: [PATCH] lint --- src/chart/conf.go | 9 ++- src/chart/conf_test.go | 2 +- src/generate/generate.go | 1 - src/helm/chart.go | 149 +++++++++++++++++++------------------ src/install/install.go | 11 ++- src/install/uninstall.go | 12 ++- src/kubectl/context.go | 5 +- src/tool/conf.go | 9 ++- src/tool/helm.go | 6 +- src/tool/tool.go | 3 +- src/utils/download.go | 2 +- src/versionOc/versionOc.go | 8 +- 12 files changed, 121 insertions(+), 96 deletions(-) diff --git a/src/chart/conf.go b/src/chart/conf.go index 2936149..86fd9d6 100644 --- a/src/chart/conf.go +++ b/src/chart/conf.go @@ -26,9 +26,12 @@ type chartsRepoData struct { Charts []ChartRepoData `yaml:"opencloud"` } -func FromConfigFile(filename string) ([]ChartRepoData) { +func FromConfigFile(filename string) ([]ChartRepoData, error) { yamlFile, _ := os.ReadFile(filename) var data chartsRepoData - yaml.Unmarshal(yamlFile, &data) - return data.Charts + err := yaml.Unmarshal(yamlFile, &data) + if err != nil { + return data.Charts, err + } + return data.Charts, nil } diff --git a/src/chart/conf_test.go b/src/chart/conf_test.go index 7a4be9c..20384d9 100644 --- a/src/chart/conf_test.go +++ b/src/chart/conf_test.go @@ -14,7 +14,7 @@ func TestReadConfChart(t *testing.T){ assert.FileExists(t, src, "FromConfigFile error") - data := FromConfigFile(src) + 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") diff --git a/src/generate/generate.go b/src/generate/generate.go index 38f7662..a1f6226 100644 --- a/src/generate/generate.go +++ b/src/generate/generate.go @@ -8,7 +8,6 @@ import ( type GenerateClass struct { Version string Workspace string - url string } func (this GenerateClass) New() (string, error) { diff --git a/src/helm/chart.go b/src/helm/chart.go index 97d7719..f727ba0 100644 --- a/src/helm/chart.go +++ b/src/helm/chart.go @@ -1,31 +1,31 @@ package helm import ( - "fmt" - "os" - "os/exec" - "strings" - "errors" - "path/filepath" - "encoding/json" + "fmt" + "os" + "os/exec" + "strings" + "errors" + "path/filepath" + "encoding/json" log "oc-deploy/log_wrapper" ) type HelmChart struct { - Bin string + Bin string Name string - Chart string - Version string - Workspace string - Opts string - Values string - FileValues 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"` + Notes string `json:"notes"` + Status string `json:"status"` } type installOutput struct { @@ -33,97 +33,100 @@ type installOutput struct { } func (this HelmChart) Install() (string, error) { - bin := this.Bin + bin := this.Bin - existe, err := this.exists() - if err != nil { - return "", err - } - - if existe { - return "Existe déjà", nil - } + 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) + 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.Version != "" { + msg = fmt.Sprintf("%s --version %s", msg, this.Version) + } - if this.FileValues != "" { + 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) - } - } + 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) + log.Log().Debug().Msg(msg) - cmd_args := strings.Split(msg, " ") + cmd_args := strings.Split(msg, " ") - cmd := exec.Command(cmd_args[0], cmd_args[1:]...) - stdout, err := cmd.CombinedOutput() + 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) - } + if err != nil { + res := string(stdout) + res = strings.TrimSuffix(res, "\n") + return "", errors.New(res) + } - var objmap installOutput + var objmap installOutput - json.Unmarshal(stdout, &objmap) + err = json.Unmarshal(stdout, &objmap) + if err != nil { + return "", err + } - res := objmap.Info.Status + res := objmap.Info.Status - return res, nil + return res, nil } func (this HelmChart) Uninstall() (string, error) { - bin := this.Bin + bin := this.Bin - log.Log().Info().Msg(" >> Chart : " + this.Name) + log.Log().Info().Msg(" >> Chart : " + this.Name) - existe, err := this.exists() - if err != nil { - return "", err - } + 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) + 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() + cmd := exec.Command(bin, "uninstall", this.Name) + stdout, err := cmd.CombinedOutput() - return string(stdout), err + return string(stdout), err } // ../bin/helm list --filter phpmyadminm --short func (this HelmChart) exists() (bool, error) { - bin := this.Bin + bin := this.Bin - msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, this.Name) - log.Log().Debug().Msg(msg) + msg := fmt.Sprintf("%s list --filter %s --no-headers", bin, this.Name) + log.Log().Debug().Msg(msg) - cmd_args := strings.Split(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)) - } + 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") + res := string(stdout) + res = strings.TrimSuffix(res, "\n") - log.Log().Debug().Msg(string(stdout)) + log.Log().Debug().Msg(string(stdout)) - return res != "", nil + return res != "", nil } diff --git a/src/install/install.go b/src/install/install.go index c0838a3..2a2c42c 100644 --- a/src/install/install.go +++ b/src/install/install.go @@ -52,8 +52,15 @@ func (this *InstallClass) NewInstall() (string, error) { // Lecture du fichier de conf // this.versionFile = dst - this.tools = tool.FromConfigFile(dst) - this.charts = chart.FromConfigFile(dst) + var err error + this.tools, err = tool.FromConfigFile(dst) + if err != nil { + return dst, err + } + this.charts, _ = chart.FromConfigFile(dst) + if err != nil { + return dst, err + } return dst, nil } diff --git a/src/install/uninstall.go b/src/install/uninstall.go index fabfd8f..b432ad5 100644 --- a/src/install/uninstall.go +++ b/src/install/uninstall.go @@ -27,8 +27,14 @@ func (this *InstallClass) NewUninstall() (string, error) { this.Version = version // Lecture du fichier de conf - this.tools = tool.FromConfigFile(dst) - this.charts = chart.FromConfigFile(dst) + this.tools, err = tool.FromConfigFile(dst) + if err != nil { + return dst, err + } + this.charts, _ = chart.FromConfigFile(dst) + if err != nil { + return dst, err + } return dst, nil } @@ -66,4 +72,4 @@ func (this *InstallClass) uninstallChart(helm_bin string, kubectl_bin string, ch return } log.Log().Info().Msg(fmt.Sprintf(" >> %s (%s)", helmchart.Name, res)) -} \ No newline at end of file +} diff --git a/src/kubectl/context.go b/src/kubectl/context.go index 574b66e..d0a783d 100644 --- a/src/kubectl/context.go +++ b/src/kubectl/context.go @@ -60,7 +60,10 @@ func (this KubeContext) GetContext() (string, string, string, error) { var objmap kubeConfig - json.Unmarshal(stdout, &objmap) + err := json.Unmarshal(stdout, &objmap) + if err != nil { + return "", "", "", err + } currentContext := objmap.CurrentContext currentCluster := "" diff --git a/src/tool/conf.go b/src/tool/conf.go index c22f60c..d14f325 100644 --- a/src/tool/conf.go +++ b/src/tool/conf.go @@ -9,9 +9,12 @@ type toolsData struct { Tools []ToolData `yaml:"tools"` } -func FromConfigFile(filename string) ([]ToolData) { +func FromConfigFile(filename string) ([]ToolData, error) { yamlFile, _ := os.ReadFile(filename) var data toolsData - yaml.Unmarshal(yamlFile, &data) - return data.Tools + err := yaml.Unmarshal(yamlFile, &data) + if err != nil { + return data.Tools, nil + } + return data.Tools, nil } diff --git a/src/tool/helm.go b/src/tool/helm.go index 20c636e..fb85320 100644 --- a/src/tool/helm.go +++ b/src/tool/helm.go @@ -36,14 +36,16 @@ func (this HelmInstallData) Download() (error) { r, _ := os.Open(tmp_file) err1 := utils.ExtractTarGz(bin_dir, r) + if err1 != nil {return err1} os.Remove(tmp_file) bin_file := fmt.Sprintf("%s/%s", bin_dir, this.obj.Name) - os.Chmod(bin_file, 0755) + errChmod := os.Chmod(bin_file, 0755) + if errChmod != nil {return errChmod} - return err1 + return nil } /////////////// diff --git a/src/tool/tool.go b/src/tool/tool.go index 2077f6d..f0c22f8 100644 --- a/src/tool/tool.go +++ b/src/tool/tool.go @@ -4,7 +4,6 @@ import ( "fmt" "os" "os/exec" - "errors" "path/filepath" ) @@ -73,7 +72,7 @@ func factory(data ToolData) (Forme, error) { case "helm": f = HelmInstallData{obj: data, tmp: "/tmp"} default: - return f, errors.New(fmt.Sprintf("Outil Inconnu : %s", data.Name)) + return f, fmt.Errorf("Outil Inconnu : %s", data.Name) } return f, nil diff --git a/src/utils/download.go b/src/utils/download.go index 39374ff..31f500a 100644 --- a/src/utils/download.go +++ b/src/utils/download.go @@ -43,7 +43,7 @@ func ExtractTarGz(dest string, gzipStream io.Reader) error { tarReader := tar.NewReader(uncompressedStream) - for true { + for { header, err := tarReader.Next() if err == io.EOF { diff --git a/src/versionOc/versionOc.go b/src/versionOc/versionOc.go index d72962c..223b5fd 100644 --- a/src/versionOc/versionOc.go +++ b/src/versionOc/versionOc.go @@ -5,7 +5,7 @@ import ( "fmt" "os" "errors" - "io/ioutil" + "io" "gopkg.in/yaml.v2" log "oc-deploy/log_wrapper" ) @@ -21,7 +21,7 @@ func GetFromFile(fileversion string) (string, error) { } defer fin.Close() - byteValue, err := ioutil.ReadAll(fin) + byteValue, err := io.ReadAll(fin) if err != nil { return "", err } @@ -64,7 +64,7 @@ func readLatestFile() (string, error) { } defer fin.Close() - byteValue, err := ioutil.ReadAll(fin) + byteValue, err := io.ReadAll(fin) if err != nil { return "", err } @@ -74,4 +74,4 @@ func readLatestFile() (string, error) { yaml.Unmarshal(byteValue, &objmap) return objmap.Version, nil -} \ No newline at end of file +}