github.com/verrazzano/verrazzano@v1.7.0/tools/charts-manager/vcm/cmd/diff/diff.go (about)

     1  // Copyright (c) 2023, 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 diff
     5  
     6  import (
     7  	"fmt"
     8  
     9  	"github.com/spf13/cobra"
    10  	vcmhelpers "github.com/verrazzano/verrazzano/tools/charts-manager/vcm/cmd/helpers"
    11  	"github.com/verrazzano/verrazzano/tools/charts-manager/vcm/pkg/constants"
    12  	"github.com/verrazzano/verrazzano/tools/charts-manager/vcm/pkg/fs"
    13  	cmdhelpers "github.com/verrazzano/verrazzano/tools/vz/cmd/helpers"
    14  	"github.com/verrazzano/verrazzano/tools/vz/pkg/helpers"
    15  )
    16  
    17  const (
    18  	CommandName = "diff"
    19  	helpShort   = "Diffs a chart against a given directory"
    20  	helpLong    = `The command 'diff' diffs the contents of a chart against a directory by executing the shell diff utility and generates a patch file.`
    21  )
    22  
    23  func buildExample() string {
    24  	return fmt.Sprintf(constants.CommandWithFlagExampleFormat+" "+
    25  		constants.FlagExampleFormat+" "+
    26  		constants.FlagExampleFormat+" "+
    27  		constants.FlagExampleFormat,
    28  		CommandName, constants.FlagChartName, constants.FlagChartShorthand, constants.FlagChartExampleKeycloak,
    29  		constants.FlagVersionName, constants.FlagPatchVersionShorthand, constants.FlagVersionExample210,
    30  		constants.FlagDirName, constants.FlagDirShorthand, constants.FlagDirExampleLocal,
    31  		constants.FlagDiffSourceName, constants.FlagDiffSourceShorthand, constants.FlagDiffSourceExample)
    32  }
    33  
    34  // NewCmdDiff creates a new instance of diff cmd
    35  func NewCmdDiff(vzHelper helpers.VZHelper, inHfs fs.ChartFileSystem) *cobra.Command {
    36  	cmd := cmdhelpers.NewCommand(vzHelper, CommandName, helpShort, helpLong)
    37  	cmd.RunE = func(cmd *cobra.Command, args []string) error {
    38  		var hfs fs.ChartFileSystem
    39  		if inHfs == nil {
    40  			hfs = fs.HelmChartFileSystem{}
    41  		} else {
    42  			hfs = inHfs
    43  		}
    44  
    45  		return runCmdDiff(cmd, vzHelper, hfs)
    46  	}
    47  	cmd.Example = buildExample()
    48  	cmd.PersistentFlags().StringP(constants.FlagChartName, constants.FlagChartShorthand, "", constants.FlagChartUsage)
    49  	cmd.PersistentFlags().StringP(constants.FlagVersionName, constants.FlagVersionShorthand, "", constants.FlagVersionUsage)
    50  	cmd.PersistentFlags().StringP(constants.FlagDirName, constants.FlagDirShorthand, "", constants.FlagDirUsage)
    51  	cmd.PersistentFlags().StringP(constants.FlagDiffSourceName, constants.FlagDiffSourceShorthand, "", constants.FlagDiffSourceUsage)
    52  	return cmd
    53  }
    54  
    55  // runCmdDiff - run the "vcm diff" command to create a file containing diff of a chart with a given directory.
    56  func runCmdDiff(cmd *cobra.Command, vzHelper helpers.VZHelper, hfs fs.ChartFileSystem) error {
    57  	chart, err := vcmhelpers.GetMandatoryStringFlagValueOrError(cmd, constants.FlagChartName, constants.FlagChartShorthand)
    58  	if err != nil {
    59  		return err
    60  	}
    61  
    62  	version, err := vcmhelpers.GetMandatoryStringFlagValueOrError(cmd, constants.FlagVersionName, constants.FlagVersionShorthand)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	chartsDir, err := vcmhelpers.GetMandatoryStringFlagValueOrError(cmd, constants.FlagDirName, constants.FlagDirShorthand)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	sourceDir, err := vcmhelpers.GetMandatoryStringFlagValueOrError(cmd, constants.FlagDiffSourceName, constants.FlagDiffSourceShorthand)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	patchFile, err := hfs.GeneratePatchWithSourceDir(chartsDir, chart, version, sourceDir)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	if patchFile == "" {
    83  		fmt.Fprint(vzHelper.GetOutputStream(), "Nothing to patch.\n")
    84  		return nil
    85  	}
    86  
    87  	fmt.Fprintf(vzHelper.GetOutputStream(), "patch file generated at %s.\n", patchFile)
    88  	return nil
    89  }