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