github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/helm/step_helm_version.go (about) 1 package helm 2 3 import ( 4 "fmt" 5 "os" 6 "path/filepath" 7 8 "github.com/olli-ai/jx/v2/pkg/builds" 9 10 "github.com/olli-ai/jx/v2/pkg/cmd/opts/step" 11 12 "github.com/olli-ai/jx/v2/pkg/cmd/helper" 13 14 "github.com/jenkins-x/jx-logging/pkg/log" 15 "github.com/olli-ai/jx/v2/pkg/cmd/opts" 16 "github.com/olli-ai/jx/v2/pkg/cmd/templates" 17 "github.com/olli-ai/jx/v2/pkg/helm" 18 "github.com/olli-ai/jx/v2/pkg/util" 19 "github.com/spf13/cobra" 20 ) 21 22 // StepHelmVersionOptions contains the command line flags 23 type StepHelmVersionOptions struct { 24 StepHelmOptions 25 26 Version string 27 } 28 29 var ( 30 StepHelmVersionLong = templates.LongDesc(` 31 Updates version of the Helm Chart.yaml in the given directory 32 `) 33 34 StepHelmVersionExample = templates.Examples(` 35 # updates the current Helm Chart.yaml to the latest build number version 36 jx step helm version 37 38 `) 39 ) 40 41 func NewCmdStepHelmVersion(commonOpts *opts.CommonOptions) *cobra.Command { 42 options := StepHelmVersionOptions{ 43 StepHelmOptions: StepHelmOptions{ 44 StepOptions: step.StepOptions{ 45 CommonOptions: commonOpts, 46 }, 47 }, 48 } 49 cmd := &cobra.Command{ 50 Use: "version", 51 Short: "Updates the chart version in the given directory", 52 Aliases: []string{""}, 53 Long: StepHelmVersionLong, 54 Example: StepHelmVersionExample, 55 Run: func(cmd *cobra.Command, args []string) { 56 options.Cmd = cmd 57 options.Args = args 58 err := options.Run() 59 helper.CheckErr(err) 60 }, 61 } 62 options.addStepHelmFlags(cmd) 63 64 cmd.Flags().StringVarP(&options.Version, "version", "v", "", "The version to update. If none specified it defaults to $BUILD_NUMBER") 65 66 return cmd 67 } 68 69 func (o *StepHelmVersionOptions) Run() error { 70 version := o.Version 71 if version == "" { 72 version = builds.GetBuildNumber() 73 } 74 if version == "" { 75 return fmt.Errorf("no version specified and could not detect the build number via $BUILD_NUMBER") 76 } 77 var err error 78 dir := o.Dir 79 if dir == "" { 80 dir, err = os.Getwd() 81 if err != nil { 82 return err 83 } 84 } 85 chartFile := filepath.Join(dir, "Chart.yaml") 86 exists, err := util.FileExists(chartFile) 87 if err != nil { 88 return err 89 } 90 if !exists { 91 return fmt.Errorf("no chart exists at %s", chartFile) 92 } 93 err = helm.SetChartVersion(chartFile, version) 94 if err != nil { 95 return err 96 } 97 log.Logger().Infof("Modified file %s to set the chart to version %s", util.ColorInfo(chartFile), util.ColorInfo(version)) 98 return nil 99 }