1
0
mirror of https://github.com/helm/chart-testing.git synced 2026-02-05 18:45:18 +01:00
Files
chart-testing/pkg/util/util.go
Reinhard Nägele f632cd5081 Re-write it in Go (#35)
* Re-write it in Go

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Fix loading config from home dir

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Print config

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Remove git gc test code

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Remove year in copyright header

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Add alias for lint-and-install

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Fix examples

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Remove OWNERS file

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Add docs generation

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Update CircleCI

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Update readme

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Document building and releasing

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>
Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Remove Makefile

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Hide doc-gen command

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Add support for Helm extra args

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Update tool dependencies

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Update Goreleaser

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Upgrade pip

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Update Gopkg.lock

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Add log messages

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Fix CircleCI env var for tag

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Add Docker login

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Readme update for MacOS (#1)

* Add build.sh mac prerequisites, and README markdown linting fixes

Signed-off-by: Scott Rigby <scott@r6by.com>

* Update README.md

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>
Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Update Gopkg.lock

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Update config search locations

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Add config files to distro

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Add debug flag

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Add note on config files for linting

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Fix link

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Revert "Update Gopkg.lock"

This reverts commit fcbfbdc9db.

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Fix link

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>

* Fix readme

Signed-off-by: Reinhard Nägele <unguiculus@gmail.com>
2018-11-07 13:06:20 -05:00

176 lines
4.2 KiB
Go

// Copyright The Helm 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 util
import (
"fmt"
"github.com/Masterminds/semver"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"io/ioutil"
"math/rand"
"os"
"path"
"strings"
"time"
)
const chars = "1234567890abcdefghijklmnopqrstuvwxyz"
type Maintainer struct {
Name string `yaml:"name"`
Email string `yaml:"email"`
}
type ChartYaml struct {
Name string `yaml:"name"`
Version string `yaml:"version"`
Deprecated bool `yaml:"deprecated"`
Maintainers []Maintainer
}
func Flatten(items []interface{}) ([]string, error) {
return doFlatten([]string{}, items)
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func doFlatten(result []string, items interface{}) ([]string, error) {
var err error
switch v := items.(type) {
case string:
result = append(result, v)
case []string:
result = append(result, v...)
case []interface{}:
for _, item := range v {
result, err = doFlatten(result, item)
if err != nil {
return nil, err
}
}
default:
return nil, errors.New(fmt.Sprintf("Flatten does not support %T", v))
}
return result, err
}
func StringSliceContains(slice []string, s string) bool {
for _, element := range slice {
if s == element {
return true
}
}
return false
}
func FileExists(file string) bool {
if _, err := os.Stat(file); err != nil {
return false
}
return true
}
// RandomString string creates a random string of numbers and lower-case ascii characters with the specified length.
func RandomString(length int) string {
n := len(chars)
bytes := make([]byte, length)
for i := range bytes {
bytes[i] = chars[rand.Intn(n)]
}
return string(bytes)
}
type DirectoryLister struct{}
// ListChildDirs lists subdirectories of parentDir matching the test function.
func (l DirectoryLister) ListChildDirs(parentDir string, test func(dir string) bool) ([]string, error) {
fileInfos, err := ioutil.ReadDir(parentDir)
if err != nil {
return nil, err
}
var dirs []string
for _, dir := range fileInfos {
dirName := dir.Name()
parentSlashChildDir := path.Join(parentDir, dirName)
if test(parentSlashChildDir) {
dirs = append(dirs, parentSlashChildDir)
}
}
return dirs, nil
}
type ChartUtils struct{}
func (u ChartUtils) IsChartDir(dir string) bool {
return FileExists(path.Join(dir, "Chart.yaml"))
}
func (u ChartUtils) ReadChartYaml(dir string) (*ChartYaml, error) {
yamlBytes, err := ioutil.ReadFile(path.Join(dir, "Chart.yaml"))
if err != nil {
return nil, errors.Wrap(err, "Could not read 'Chart.yaml'")
}
return ReadChartYaml(yamlBytes)
}
func ReadChartYaml(yamlBytes []byte) (*ChartYaml, error) {
chartYaml := &ChartYaml{}
if err := yaml.Unmarshal(yamlBytes, chartYaml); err != nil {
return nil, errors.Wrap(err, "Could not unmarshal 'Chart.yaml'")
}
return chartYaml, nil
}
func CompareVersions(left string, right string) (int, error) {
leftVersion, err := semver.NewVersion(left)
if err != nil {
return 0, errors.Wrap(err, "Error parsing semantic version")
}
rightVersion, err := semver.NewVersion(right)
if err != nil {
return 0, errors.Wrap(err, "Error parsing semantic version")
}
return leftVersion.Compare(rightVersion), nil
}
func CreateInstallParams(chart string, buildId string) (release string, namespace string) {
release = path.Base(chart)
namespace = release
if buildId != "" {
namespace += buildId
}
randomSuffix := RandomString(10)
release = fmt.Sprintf("%s-%s", release, randomSuffix)
namespace = fmt.Sprintf("%s-%s", namespace, randomSuffix)
return
}
func PrintDelimiterLine(delimiterChar string) {
delim := make([]string, 120)
for i := 0; i < 120; i++ {
delim[i] = delimiterChar
}
fmt.Println(strings.Join(delim, ""))
}