github.com/jenkins-x/jx/v2@v2.1.155/pkg/kustomize/kustomize_cli.go (about)

     1  package kustomize
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"github.com/jenkins-x/jx-logging/pkg/log"
    10  
    11  	"github.com/jenkins-x/jx/v2/pkg/util"
    12  	"github.com/pkg/errors"
    13  )
    14  
    15  // KustomizeCLI implements common kustomize actions based on kustomize CLI
    16  type KustomizeCLI struct {
    17  	Runner util.Commander
    18  }
    19  
    20  // NewKustomizeCLI creates a new KustomizeCLI instance configured to use the provided kustomize CLI in
    21  // the given current working directory
    22  func NewKustomizeCLI() *KustomizeCLI {
    23  	runner := &util.Command{
    24  		Name: "kustomize",
    25  	}
    26  	cli := &KustomizeCLI{
    27  		Runner: runner,
    28  	}
    29  	return cli
    30  }
    31  
    32  // Version executes the Kustomize version command and returns its output
    33  func (k *KustomizeCLI) Version(extraArgs ...string) (string, error) {
    34  	args := []string{"version", "--short"}
    35  	args = append(args, extraArgs...)
    36  	version, err := k.runKustomizeWithOutput(args...)
    37  	if err != nil {
    38  		return "", err
    39  	}
    40  	return extractSemanticVersion(version)
    41  }
    42  
    43  func (k *KustomizeCLI) runKustomizeWithOutput(args ...string) (string, error) {
    44  	k.Runner.SetArgs(args)
    45  	return k.Runner.RunWithoutRetry()
    46  }
    47  
    48  // extractSemanticVersion return the semantic version string out of given version cli output.
    49  // currently tested on {Version:3.5.4 GitCommit ....} and {Version:kustomize/v3.5.4 GitCommit: ...}
    50  func extractSemanticVersion(version string) (string, error) {
    51  	regex, err := regexp.Compile(`[0-9]+\.[0-9]+\.[0-9]+`)
    52  	if err != nil {
    53  		return "", errors.Wrapf(err, "not able to extract a semantic version of kustomize version output")
    54  	}
    55  	return regex.FindString(version), nil
    56  }
    57  
    58  // ContainsKustomizeConfig finds out if there is any kustomize resource in the cwd or subdirectories
    59  func (k *KustomizeCLI) ContainsKustomizeConfig(dir string) bool {
    60  	if len(k.FindKustomizationYamlPaths(dir)) != 0 {
    61  		return true
    62  	}
    63  
    64  	return false
    65  }
    66  
    67  // FindKustomizationYamlPaths looks for the kustomization.yaml i.e. kustomize resources in present and sub-directories
    68  func (k *KustomizeCLI) FindKustomizationYamlPaths(dir string) []string {
    69  	fp, err := filepath.Abs(dir)
    70  	var resources []string
    71  	err = filepath.Walk(fp, func(path string, info os.FileInfo, err error) error {
    72  		if err != nil {
    73  			return err
    74  		}
    75  		if strings.Contains(path, "kustomization.yaml") {
    76  			resources = append(resources, path)
    77  		}
    78  		return nil
    79  	})
    80  	if err != nil {
    81  		log.Logger().Errorf("problem finding kustomize resources %s ", err)
    82  	}
    83  	return resources
    84  }