-
Notifications
You must be signed in to change notification settings - Fork 787
/
create_spring.go
135 lines (111 loc) · 4.42 KB
/
create_spring.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cmd
import (
"fmt"
"io"
"os"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/spring"
"github.com/jenkins-x/jx/pkg/util"
)
var (
create_spring_long = templates.LongDesc(`
Creates a new Spring Boot application and then optionally setups CI/CD pipelines and GitOps promotion.
You can see a demo of this command here: [https://jenkins-x.io/demos/create_spring/](https://jenkins-x.io/demos/create_spring/)
For more documentation see: [https://jenkins-x.io/developing/create-spring/](https://jenkins-x.io/developing/create-spring/)
`)
create_spring_example = templates.Examples(`
# Create a Spring Boot application where you use the terminal to pick the values
jx create spring
# Creates a Spring Boot application passing in the required dependencies
jx create spring -d web -d actuator
# To pick the advanced options (such as what package type maven-project/gradle-project) etc then use
jx create spring -x
# To create a gradle project use:
jx create spring --type gradle-project
`)
)
// CreateSpringOptions the options for the create spring command
type CreateSpringOptions struct {
CreateProjectOptions
Advanced bool
SpringForm spring.SpringBootForm
}
// NewCmdCreateSpring creates a command object for the "create" command
func NewCmdCreateSpring(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &CreateSpringOptions{
CreateProjectOptions: CreateProjectOptions{
ImportOptions: ImportOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
},
},
}
cmd := &cobra.Command{
Use: "spring",
Short: "Create a new Spring Boot application and import the generated code into Git and Jenkins for CI/CD",
Long: create_spring_long,
Example: create_spring_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCreateAppFlags(cmd)
cmd.Flags().BoolVarP(&options.Advanced, "advanced", "x", false, "Advanced mode can show more detailed forms for some resource kinds like springboot")
cmd.Flags().StringArrayVarP(&options.SpringForm.DependencyKinds, spring.OptionDependencyKind, "k", spring.DefaultDependencyKinds, "Default dependency kinds to choose from")
cmd.Flags().StringArrayVarP(&options.SpringForm.Dependencies, spring.OptionDependency, "d", []string{}, "Spring Boot dependencies")
cmd.Flags().StringVarP(&options.SpringForm.GroupId, spring.OptionGroupId, "g", "", "Group ID to generate")
cmd.Flags().StringVarP(&options.SpringForm.ArtifactId, spring.OptionArtifactId, "a", "", "Artifact ID to generate")
cmd.Flags().StringVarP(&options.SpringForm.Language, spring.OptionLanguage, "l", "", "Language to generate")
cmd.Flags().StringVarP(&options.SpringForm.BootVersion, spring.OptionBootVersion, "t", "", "Spring Boot version")
cmd.Flags().StringVarP(&options.SpringForm.JavaVersion, spring.OptionJavaVersion, "j", "", "Java version")
cmd.Flags().StringVarP(&options.SpringForm.Packaging, spring.OptionPackaging, "p", "", "Packaging")
cmd.Flags().StringVarP(&options.SpringForm.Type, spring.OptionType, "", "", "Project Type (such as maven-project or gradle-project)")
return cmd
}
// Run implements the command
func (o *CreateSpringOptions) Run() error {
cacheDir, err := util.CacheDir()
if err != nil {
return err
}
model, err := spring.LoadSpringBoot(cacheDir)
if err != nil {
return fmt.Errorf("Failed to load Spring Boot model %s", err)
}
data := &o.SpringForm
err = model.CreateSurvey(&o.SpringForm, o.Advanced, o.BatchMode)
if err != nil {
return err
}
// always add in actuator as its required for health checking
if !util.Contains(o.SpringForm.Dependencies, "actuator") {
o.SpringForm.Dependencies = append(o.SpringForm.Dependencies, "actuator")
}
// always add web as the JVM tends to terminate if its not added
if !util.Contains(o.SpringForm.Dependencies, "web") {
o.SpringForm.Dependencies = append(o.SpringForm.Dependencies, "web")
}
dir := o.OutDir
if dir == "" {
dir, err = os.Getwd()
if err != nil {
return err
}
}
outDir, err := data.CreateProject(dir)
if err != nil {
return err
}
log.Infof("Created Spring Boot project at %s\n", util.ColorInfo(outDir))
return o.ImportCreatedProject(outDir)
}