From f09a57351f2cef91ec4ba902717c1b9912cce916 Mon Sep 17 00:00:00 2001 From: aiordache Date: Thu, 21 Jan 2021 14:23:52 +0100 Subject: [PATCH] remove unused utils pkg Signed-off-by: aiordache --- go.mod | 1 - kube/charts/charts.go | 14 ++++- kube/utils/config.go | 142 ------------------------------------------ kube/utils/errors.go | 56 ----------------- kube/utils/labels.go | 35 ----------- kube/utils/utils.go | 34 ---------- 6 files changed, 12 insertions(+), 270 deletions(-) delete mode 100644 kube/utils/config.go delete mode 100644 kube/utils/errors.go delete mode 100644 kube/utils/labels.go delete mode 100644 kube/utils/utils.go diff --git a/go.mod b/go.mod index 074731c7c..50aeb9e05 100644 --- a/go.mod +++ b/go.mod @@ -45,7 +45,6 @@ require ( github.com/opencontainers/go-digest v1.0.0 github.com/opencontainers/image-spec v1.0.1 github.com/pkg/errors v0.9.1 - github.com/prometheus/common v0.10.0 github.com/prometheus/tsdb v0.10.0 github.com/sanathkr/go-yaml v0.0.0-20170819195128-ed9d249f429b github.com/sirupsen/logrus v1.7.0 diff --git a/kube/charts/charts.go b/kube/charts/charts.go index df035eca7..8a74789ea 100644 --- a/kube/charts/charts.go +++ b/kube/charts/charts.go @@ -20,6 +20,7 @@ package charts import ( "context" + "os" "path/filepath" "strings" @@ -28,7 +29,6 @@ import ( "github.com/docker/compose-cli/api/context/store" "github.com/docker/compose-cli/kube/charts/helm" "github.com/docker/compose-cli/kube/charts/kubernetes" - kubeutils "github.com/docker/compose-cli/kube/utils" chart "helm.sh/helm/v3/pkg/chart" util "helm.sh/helm/v3/pkg/chartutil" helmenv "helm.sh/helm/v3/pkg/cli" @@ -57,7 +57,7 @@ var _ API = sdk{} func NewSDK(ctx store.KubeContext) (sdk, error) { return sdk{ - environment: kubeutils.Environment(), + environment: environment(), h: helm.NewHelmActions(nil), }, nil } @@ -124,3 +124,13 @@ func (s sdk) GenerateChart(project *types.Project, dirname string) error { dirname = filepath.Dir(dirname) return s.SaveChart(project, dirname) } + +func environment() map[string]string { + vars := make(map[string]string) + env := os.Environ() + for _, v := range env { + k := strings.SplitN(v, "=", 2) + vars[k[0]] = k[1] + } + return vars +} diff --git a/kube/utils/config.go b/kube/utils/config.go deleted file mode 100644 index 408558923..000000000 --- a/kube/utils/config.go +++ /dev/null @@ -1,142 +0,0 @@ -// +build kube - -/* - Copyright 2020 Docker Compose CLI authors - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package utils - -import ( - "io/ioutil" - "os" - "path/filepath" - "regexp" - "strings" - - "github.com/compose-spec/compose-go/loader" - "github.com/compose-spec/compose-go/types" - "github.com/prometheus/common/log" -) - -var SupportedFilenames = []string{"compose.yaml", "compose.yml", "docker-compose.yml", "docker-compose.yaml"} - -func GetConfigs(name string, configPaths []string) (string, []types.ConfigFile, error) { - configPath, err := getConfigPaths(configPaths) - if err != nil { - return "", nil, err - } - if configPath == nil { - return "", nil, nil - } - workingDir := filepath.Dir(configPath[0]) - - if name == "" { - name = os.Getenv("COMPOSE_PROJECT_NAME") - } - if name == "" { - r := regexp.MustCompile(`[^a-z0-9\\-_]+`) - name = r.ReplaceAllString(strings.ToLower(filepath.Base(workingDir)), "") - } - - configs, err := parseConfigs(configPath) - if err != nil { - return "", nil, err - } - return workingDir, configs, nil -} - -func getConfigPaths(configPaths []string) ([]string, error) { - paths := []string{} - pwd, err := os.Getwd() - if err != nil { - return nil, err - } - - if len(configPaths) != 0 { - for _, f := range configPaths { - if f == "-" { - paths = append(paths, f) - continue - } - if !filepath.IsAbs(f) { - f = filepath.Join(pwd, f) - } - if _, err := os.Stat(f); err != nil { - return nil, err - } - paths = append(paths, f) - } - return paths, nil - } - - sep := os.Getenv("COMPOSE_FILE_SEPARATOR") - if sep == "" { - sep = string(os.PathListSeparator) - } - f := os.Getenv("COMPOSE_FILE") - if f != "" { - return strings.Split(f, sep), nil - } - - for { - candidates := []string{} - for _, n := range SupportedFilenames { - f := filepath.Join(pwd, n) - if _, err := os.Stat(f); err == nil { - candidates = append(candidates, f) - } - } - if len(candidates) > 0 { - winner := candidates[0] - if len(candidates) > 1 { - log.Warnf("Found multiple config files with supported names: %s", strings.Join(candidates, ", ")) - log.Warnf("Using %s\n", winner) - } - return []string{winner}, nil - } - parent := filepath.Dir(pwd) - if parent == pwd { - return nil, nil - } - pwd = parent - } -} - -func parseConfigs(configPaths []string) ([]types.ConfigFile, error) { - files := []types.ConfigFile{} - for _, f := range configPaths { - var ( - b []byte - err error - ) - if f == "-" { - b, err = ioutil.ReadAll(os.Stdin) - } else { - if _, err := os.Stat(f); err != nil { - return nil, err - } - b, err = ioutil.ReadFile(f) - } - if err != nil { - return nil, err - } - config, err := loader.ParseYAML(b) - if err != nil { - return nil, err - } - files = append(files, types.ConfigFile{Filename: f, Config: config}) - } - return files, nil -} diff --git a/kube/utils/errors.go b/kube/utils/errors.go deleted file mode 100644 index 0b58b9a54..000000000 --- a/kube/utils/errors.go +++ /dev/null @@ -1,56 +0,0 @@ -// +build kube - -/* - Copyright 2020 Docker Compose CLI authors - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package utils - -import ( - "fmt" - "strings" -) - -func CombineErrors(errors []error) error { - if len(errors) == 0 { - return nil - } - if len(errors) == 1 { - return errors[0] - } - err := combinedError{} - for _, e := range errors { - if c, ok := e.(combinedError); ok { - err.errors = append(err.errors, c.errors...) - } else { - err.errors = append(err.errors, e) - } - } - return combinedError{errors} -} - -type combinedError struct { - errors []error -} - -func (c combinedError) Error() string { - points := make([]string, len(c.errors)) - for i, err := range c.errors { - points[i] = fmt.Sprintf("* %s", err.Error()) - } - return fmt.Sprintf( - "%d errors occurred:\n\t%s", - len(c.errors), strings.Join(points, "\n\t")) -} diff --git a/kube/utils/labels.go b/kube/utils/labels.go deleted file mode 100644 index 0ef3ecc07..000000000 --- a/kube/utils/labels.go +++ /dev/null @@ -1,35 +0,0 @@ -// +build kube - -/* - Copyright 2020 Docker Compose CLI authors - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package utils - -const ( - LabelDockerComposePrefix = "com.docker.compose" - LabelService = LabelDockerComposePrefix + ".service" - LabelVersion = LabelDockerComposePrefix + ".version" - LabelContainerNumber = LabelDockerComposePrefix + ".container-number" - LabelOneOff = LabelDockerComposePrefix + ".oneoff" - LabelNetwork = LabelDockerComposePrefix + ".network" - LabelSlug = LabelDockerComposePrefix + ".slug" - LabelVolume = LabelDockerComposePrefix + ".volume" - LabelConfigHash = LabelDockerComposePrefix + ".config-hash" - LabelProject = LabelDockerComposePrefix + ".project" - LabelWorkingDir = LabelDockerComposePrefix + ".working_dir" - LabelConfigFiles = LabelDockerComposePrefix + ".config_files" - LabelEnvironmentFile = LabelDockerComposePrefix + ".environment_file" -) diff --git a/kube/utils/utils.go b/kube/utils/utils.go deleted file mode 100644 index dab4cd5fd..000000000 --- a/kube/utils/utils.go +++ /dev/null @@ -1,34 +0,0 @@ -// +build kube - -/* - Copyright 2020 Docker Compose CLI authors - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - -package utils - -import ( - "os" - "strings" -) - -func Environment() map[string]string { - vars := make(map[string]string) - env := os.Environ() - for _, v := range env { - k := strings.SplitN(v, "=", 2) - vars[k[0]] = k[1] - } - return vars -}