github.com/verrazzano/verrazzano@v1.7.0/pkg/helm/chart.go (about)

     1  // Copyright (c) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package helm
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"sigs.k8s.io/yaml"
    11  )
    12  
    13  // ChartInfo contains Helm Chart.yaml data
    14  type ChartInfo struct {
    15  	APIVersion  string
    16  	Description string
    17  	Name        string
    18  	Version     string
    19  	AppVersion  string
    20  }
    21  
    22  // ChartInfoFnType - Package-level var and functions to allow overriding getReleaseState for unit test purposes
    23  type ChartInfoFnType func(chartDir string) (ChartInfo, error)
    24  
    25  var chartInfoFn ChartInfoFnType = getChartInfo
    26  
    27  // SetChartInfoFunction Override the chart info function for unit testing
    28  func SetChartInfoFunction(f ChartInfoFnType) {
    29  	chartInfoFn = f
    30  }
    31  
    32  // SetDefaultChartInfoFunction Reset the chart info function
    33  func SetDefaultChartInfoFunction() {
    34  	chartInfoFn = getChartInfo
    35  }
    36  
    37  // GetChartInfo - public entry point
    38  func GetChartInfo(chartDir string) (ChartInfo, error) {
    39  	return chartInfoFn(chartDir)
    40  }
    41  
    42  // getChartInfo loads the Chart.yaml from the specified chart dir into a ChartInfo struct
    43  func getChartInfo(chartDir string) (ChartInfo, error) {
    44  	chartBytes, err := os.ReadFile(filepath.Join(chartDir + "/Chart.yaml"))
    45  	if err != nil {
    46  		return ChartInfo{}, err
    47  	}
    48  	chart := ChartInfo{}
    49  	err = yaml.Unmarshal(chartBytes, &chart)
    50  	if err != nil {
    51  		return ChartInfo{}, err
    52  	}
    53  	return chart, nil
    54  }