-
Notifications
You must be signed in to change notification settings - Fork 785
/
create_project.go
157 lines (135 loc) · 3.98 KB
/
create_project.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package create
import (
"fmt"
"os"
"github.com/jenkins-x/jx/v2/pkg/cmd/create/options"
"github.com/jenkins-x/jx/v2/pkg/spring"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/importcmd"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/spf13/cobra"
)
const (
createQuickstartName = "Create new application from a Quickstart"
createSpringName = "Create new Spring Boot microservice"
importDirName = "Import existing code from a directory"
importGitName = "Import code from a git repository"
importGitHubName = "Import code from a github repository"
)
var (
createProjectNames = []string{
createQuickstartName,
createSpringName,
importDirName,
importGitName,
importGitHubName,
}
createProjectLong = templates.LongDesc(`
Create a new Project by importing code, using a Quickstart or custom wizard for Spring.
` + helper.SeeAlsoText("jx create quickstart", "jx create spring", "jx import"))
createProjectExample = templates.Examples(`
# Create a project
jx create project
`)
)
// CreateProjectWizardOptions the options for the command
type CreateProjectWizardOptions struct {
options.CreateOptions
}
// SpringOptions the options for the create spring command
type SpringOptions struct {
CreateProjectOptions
Advanced bool
SpringForm spring.SpringBootForm
}
// NewCmdCreateProject creates a command object for the "create" command
func NewCmdCreateProject(commonOpts *opts.CommonOptions) *cobra.Command {
options := &CreateProjectWizardOptions{
CreateOptions: options.CreateOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "project",
Short: "Create a new Project by importing code, using a Quickstart or custom wizard for Spring",
Long: createProjectLong,
Example: createProjectExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
return cmd
}
// Run implements the command
func (o *CreateProjectWizardOptions) Run() error {
name, err := util.PickName(createProjectNames, "Which kind of project you want to create: ",
"Jenkins X supports a number of different wizards for creating or importing new projects.",
o.GetIOFileHandles())
if err != nil {
return err
}
switch name {
case createQuickstartName:
return o.createQuickstart()
case createSpringName:
return o.createSpring()
case importDirName:
return o.importDir()
case importGitName:
return o.importGit()
case importGitHubName:
return o.importGithubProject()
default:
return fmt.Errorf("Unknown selection: %s\n", name)
}
}
func (o *CreateProjectWizardOptions) createQuickstart() error {
w := &CreateQuickstartOptions{}
w.CommonOptions = o.CommonOptions
return w.Run()
}
func (o *CreateProjectWizardOptions) createSpring() error {
w := &SpringOptions{}
w.CommonOptions = o.CommonOptions
return w.Run()
}
func (o *CreateProjectWizardOptions) importDir() error {
wd, err := os.Getwd()
if err != nil {
return err
}
dir, err := util.PickValue("Which directory contains the source code: ", wd, true,
"Please specify the directory which contains the source code you want to use for your new project", o.GetIOFileHandles())
if err != nil {
return err
}
w := &importcmd.ImportOptions{
Dir: dir,
}
w.CommonOptions = o.CommonOptions
return w.Run()
}
func (o *CreateProjectWizardOptions) importGit() error {
repoUrl, err := util.PickValue("Which git repository URL to import: ", "", true,
"Please specify the git URL which contains the source code you want to use for your new project", o.GetIOFileHandles())
if err != nil {
return err
}
w := &importcmd.ImportOptions{
RepoURL: repoUrl,
}
w.CommonOptions = o.CommonOptions
return w.Run()
}
func (o *CreateProjectWizardOptions) importGithubProject() error {
w := &importcmd.ImportOptions{
GitHub: true,
}
w.CommonOptions = o.CommonOptions
return w.Run()
}