github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/cmd/step/create/pr/step_create_pr_docker.go (about)

     1  package pr
     2  
     3  import (
     4  	"github.com/jenkins-x/jx-logging/pkg/log"
     5  	"github.com/olli-ai/jx/v2/pkg/cmd/opts/step"
     6  	"github.com/olli-ai/jx/v2/pkg/docker"
     7  	"github.com/olli-ai/jx/v2/pkg/gits"
     8  	"github.com/pkg/errors"
     9  
    10  	"github.com/olli-ai/jx/v2/pkg/cmd/helper"
    11  
    12  	"github.com/olli-ai/jx/v2/pkg/cmd/opts"
    13  	"github.com/olli-ai/jx/v2/pkg/cmd/templates"
    14  	"github.com/olli-ai/jx/v2/pkg/util"
    15  	"github.com/spf13/cobra"
    16  )
    17  
    18  var (
    19  	createPullRequestDockerLong = templates.LongDesc(`
    20  		Creates a Pull Request on a git repository updating any lines in the Dockerfile that start with FROM, ENV or ARG=
    21  `)
    22  
    23  	createPullRequestDockerExample = templates.Examples(`
    24  					`)
    25  )
    26  
    27  // StepCreatePullRequestDockersOptions contains the command line flags
    28  type StepCreatePullRequestDockersOptions struct {
    29  	StepCreatePrOptions
    30  
    31  	Names []string
    32  }
    33  
    34  // NewCmdStepCreatePullRequestDocker Creates a new Command object
    35  func NewCmdStepCreatePullRequestDocker(commonOpts *opts.CommonOptions) *cobra.Command {
    36  	options := &StepCreatePullRequestDockersOptions{
    37  		StepCreatePrOptions: StepCreatePrOptions{
    38  			StepCreateOptions: step.StepCreateOptions{
    39  				StepOptions: step.StepOptions{
    40  					CommonOptions: commonOpts,
    41  				},
    42  			},
    43  		},
    44  	}
    45  
    46  	cmd := &cobra.Command{
    47  		Use:     "docker",
    48  		Short:   "Creates a Pull Request on a git repository updating the docker file",
    49  		Long:    createPullRequestDockerLong,
    50  		Example: createPullRequestDockerExample,
    51  		Aliases: []string{"version pullrequest"},
    52  		Run: func(cmd *cobra.Command, args []string) {
    53  			options.Cmd = cmd
    54  			options.Args = args
    55  			err := options.Run()
    56  			helper.CheckErr(err)
    57  		},
    58  	}
    59  	AddStepCreatePrFlags(cmd, &options.StepCreatePrOptions)
    60  	cmd.Flags().StringArrayVarP(&options.Names, "name", "n", make([]string, 0), "The name of the property to update")
    61  	return cmd
    62  }
    63  
    64  // ValidateDockersOptions validates the common options for docker pr steps
    65  func (o *StepCreatePullRequestDockersOptions) ValidateDockersOptions() error {
    66  	if err := o.ValidateOptions(false); err != nil {
    67  		return errors.WithStack(err)
    68  	}
    69  	if len(o.Names) == 0 {
    70  		return util.MissingOption("name")
    71  	}
    72  	if o.SrcGitURL == "" {
    73  		log.Logger().Warnf("srcRepo is not provided so generated PR will not be correctly linked in release notesPR")
    74  	}
    75  
    76  	return nil
    77  }
    78  
    79  // Run implements this command
    80  func (o *StepCreatePullRequestDockersOptions) Run() error {
    81  	if err := o.ValidateDockersOptions(); err != nil {
    82  		return errors.WithStack(err)
    83  	}
    84  	err := o.CreatePullRequest("docker",
    85  		func(dir string, gitInfo *gits.GitRepository) ([]string, error) {
    86  			var oldVersions []string
    87  			for _, name := range o.Names {
    88  				oldVersionsforName, err := docker.UpdateVersions(dir, o.Version, name)
    89  				if err != nil {
    90  					return nil, errors.Wrapf(err, "updating %s to %s", name, o.Version)
    91  				}
    92  				oldVersions = append(oldVersions, oldVersionsforName...)
    93  			}
    94  			return oldVersions, nil
    95  		})
    96  	if err != nil {
    97  		return errors.WithStack(err)
    98  	}
    99  	return nil
   100  }