github.com/jaylevin/jenkins-library@v1.230.4/pkg/versioning/helm.go (about)

     1  package versioning
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ghodss/yaml"
     7  	"helm.sh/helm/v3/pkg/chart"
     8  )
     9  
    10  // JSONfile defines an artifact using a json file for versioning
    11  type HelmChart struct {
    12  	path             string
    13  	metadata         chart.Metadata
    14  	utils            Utils
    15  	updateAppVersion bool
    16  }
    17  
    18  func (h *HelmChart) init() error {
    19  	if h.utils == nil {
    20  		return fmt.Errorf("no file utils provided")
    21  	}
    22  	if len(h.path) == 0 {
    23  		charts, err := h.utils.Glob("**/Chart.yaml")
    24  		if len(charts) == 0 || err != nil {
    25  			return fmt.Errorf("failed to find a helm chart file")
    26  		}
    27  		// use first chart which can be found
    28  		h.path = charts[0]
    29  	}
    30  
    31  	if len(h.metadata.Version) == 0 {
    32  		content, err := h.utils.FileRead(h.path)
    33  		if err != nil {
    34  			return fmt.Errorf("failed to read file '%v': %w", h.path, err)
    35  		}
    36  
    37  		err = yaml.Unmarshal(content, &h.metadata)
    38  		if err != nil {
    39  			return fmt.Errorf("helm chart content invalid '%v': %w", h.path, err)
    40  		}
    41  	}
    42  
    43  	return nil
    44  }
    45  
    46  // VersioningScheme returns the relevant versioning scheme
    47  func (h *HelmChart) VersioningScheme() string {
    48  	return "semver2"
    49  }
    50  
    51  // GetVersion returns the current version of the artifact with a JSON-based build descriptor
    52  func (h *HelmChart) GetVersion() (string, error) {
    53  	if err := h.init(); err != nil {
    54  		return "", fmt.Errorf("failed to init helm chart versioning: %w", err)
    55  	}
    56  
    57  	return h.metadata.Version, nil
    58  }
    59  
    60  // SetVersion updates the version of the artifact with a JSON-based build descriptor
    61  func (h *HelmChart) SetVersion(version string) error {
    62  	if err := h.init(); err != nil {
    63  		return fmt.Errorf("failed to init helm chart versioning: %w", err)
    64  	}
    65  
    66  	h.metadata.Version = version
    67  	if h.updateAppVersion {
    68  		h.metadata.AppVersion = version
    69  	}
    70  
    71  	content, err := yaml.Marshal(h.metadata)
    72  	if err != nil {
    73  		return fmt.Errorf("failed to create chart content for '%v': %w", h.path, err)
    74  	}
    75  	err = h.utils.FileWrite(h.path, content, 666)
    76  	if err != nil {
    77  		return fmt.Errorf("failed to write file '%v': %w", h.path, err)
    78  	}
    79  
    80  	return nil
    81  }
    82  
    83  // GetCoordinates returns the coordinates
    84  func (h *HelmChart) GetCoordinates() (Coordinates, error) {
    85  	result := Coordinates{}
    86  	projectVersion, err := h.GetVersion()
    87  	if err != nil {
    88  		return result, err
    89  	}
    90  
    91  	result.ArtifactID = h.metadata.Name
    92  	result.Version = projectVersion
    93  	result.GroupID = h.metadata.Home
    94  
    95  	return result, nil
    96  }