github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/step/buildpack/step_buildpack_apply.go (about) 1 package buildpack 2 3 import ( 4 "os" 5 "path/filepath" 6 7 "github.com/jenkins-x/jx/v2/pkg/cmd/opts/step" 8 9 "github.com/jenkins-x/jx/v2/pkg/cmd/helper" 10 11 "github.com/jenkins-x/jx-logging/pkg/log" 12 "github.com/jenkins-x/jx/v2/pkg/cmd/opts" 13 "github.com/jenkins-x/jx/v2/pkg/cmd/templates" 14 "github.com/jenkins-x/jx/v2/pkg/jenkinsfile" 15 "github.com/spf13/cobra" 16 ) 17 18 var ( 19 createJenkinsfileLong = templates.LongDesc(` 20 Applies the build pack for a project to add any missing files like a Jenkinsfile 21 `) 22 23 createJenkinsfileExample = templates.Examples(` 24 # applies the current build pack for the current team adding any missing files like Jenkinsfile 25 jx step buildpack apply 26 27 # applies the 'maven' build pack to the current project 28 jx step buildpack apply --pack maven 29 30 `) 31 ) 32 33 // StepBuildPackApplyOptions contains the command line flags 34 type StepBuildPackApplyOptions struct { 35 step.StepOptions 36 37 Dir string 38 Jenkinsfile string 39 DraftPack string 40 DisableJenkinsfileCheck bool 41 } 42 43 // NewCmdStepBuildPackApply Creates a new Command object 44 func NewCmdStepBuildPackApply(commonOpts *opts.CommonOptions) *cobra.Command { 45 options := &StepBuildPackApplyOptions{ 46 StepOptions: step.StepOptions{ 47 CommonOptions: commonOpts, 48 }, 49 } 50 51 cmd := &cobra.Command{ 52 Use: "apply", 53 Short: "Applies the current teams build pack to the project to add any missing resources like a Jenkinsfile", 54 Long: createJenkinsfileLong, 55 Example: createJenkinsfileExample, 56 Run: func(cmd *cobra.Command, args []string) { 57 options.Cmd = cmd 58 options.Args = args 59 err := options.Run() 60 helper.CheckErr(err) 61 }, 62 } 63 64 cmd.Flags().StringVarP(&options.Dir, "dir", "d", "", "The directory to query to find the projects .git directory") 65 cmd.Flags().StringVarP(&options.Jenkinsfile, "jenkinsfile", "", "", "The name of the Jenkinsfile to use. If not specified then 'Jenkinsfile' will be used") 66 cmd.Flags().StringVarP(&options.DraftPack, "pack", "", "", "The name of the pack to use") 67 cmd.Flags().BoolVarP(&options.DisableJenkinsfileCheck, "no-jenkinsfile", "", false, "Disable defaulting a Jenkinsfile if its missing") 68 return cmd 69 } 70 71 // Run implements this command 72 func (o *StepBuildPackApplyOptions) Run() error { 73 var err error 74 dir := o.Dir 75 if dir == "" { 76 dir, err = os.Getwd() 77 if err != nil { 78 return err 79 } 80 } 81 82 settings, err := o.CommonOptions.TeamSettings() 83 if err != nil { 84 return err 85 } 86 log.Logger().Infof("build pack is %s", settings.BuildPackURL) 87 88 defaultJenkinsfile := filepath.Join(dir, jenkinsfile.Name) 89 jenkinsfile := jenkinsfile.Name 90 withRename := false 91 if o.Jenkinsfile != "" { 92 jenkinsfile = o.Jenkinsfile 93 withRename = true 94 } 95 if !filepath.IsAbs(jenkinsfile) { 96 jenkinsfile = filepath.Join(dir, jenkinsfile) 97 } 98 99 args := &opts.InvokeDraftPack{ 100 Dir: dir, 101 CustomDraftPack: o.DraftPack, 102 Jenkinsfile: jenkinsfile, 103 DefaultJenkinsfile: defaultJenkinsfile, 104 WithRename: withRename, 105 InitialisedGit: true, 106 DisableJenkinsfileCheck: o.DisableJenkinsfileCheck, 107 } 108 _, err = o.InvokeDraftPack(args) 109 if err != nil { 110 return err 111 } 112 return nil 113 }