-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathcli.go
197 lines (180 loc) · 6.86 KB
/
cli.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package pipelines
import (
"errors"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
corecommon "github.com/jfrog/jfrog-cli-core/v2/docs/common"
pipelines "github.com/jfrog/jfrog-cli-core/v2/pipelines/commands"
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli/docs/common"
"github.com/jfrog/jfrog-cli/docs/pipelines/status"
"github.com/jfrog/jfrog-cli/docs/pipelines/sync"
"github.com/jfrog/jfrog-cli/docs/pipelines/syncstatus"
"github.com/jfrog/jfrog-cli/docs/pipelines/trigger"
"github.com/jfrog/jfrog-cli/docs/pipelines/version"
"github.com/jfrog/jfrog-cli/utils/cliutils"
clientlog "github.com/jfrog/jfrog-client-go/utils/log"
"github.com/urfave/cli"
)
func GetCommands() []cli.Command {
return cliutils.GetSortedCommands(cli.CommandsByName{
{
Name: "status",
Flags: cliutils.GetCommandFlags(cliutils.Status),
Aliases: []string{"s"},
Usage: status.GetDescription(),
HelpName: corecommon.CreateUsage("pl status", status.GetDescription(), status.Usage),
ArgsUsage: common.CreateEnvVars(),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: fetchLatestPipelineRunStatus,
},
{
Name: "trigger",
Flags: cliutils.GetCommandFlags(cliutils.Trigger),
Aliases: []string{"t"},
Usage: trigger.GetDescription(),
HelpName: corecommon.CreateUsage("pl trigger", trigger.GetDescription(), trigger.Usage),
UsageText: trigger.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: triggerNewRun,
},
{
Name: "version",
Flags: cliutils.GetCommandFlags(cliutils.Version),
Aliases: []string{"v"},
Usage: version.GetDescription(),
HelpName: corecommon.CreateUsage("pl version", version.GetDescription(), version.Usage),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: getVersion,
},
{
Name: "sync",
Flags: cliutils.GetCommandFlags(cliutils.Sync),
Aliases: []string{"sy"},
Usage: sync.GetDescription(),
HelpName: corecommon.CreateUsage("pl sync", sync.GetDescription(), sync.Usage),
UsageText: sync.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: syncPipelineResources,
},
{
Name: "sync-status",
Flags: cliutils.GetCommandFlags(cliutils.SyncStatus),
Aliases: []string{"ss"},
Usage: syncstatus.GetDescription(),
HelpName: corecommon.CreateUsage("pl sync-status", syncstatus.GetDescription(), syncstatus.Usage),
ArgsUsage: common.CreateEnvVars(),
BashComplete: corecommon.CreateBashCompletionFunc(),
Action: getSyncPipelineResourcesStatus,
},
})
}
// getMultiBranch parses singleBranch flag and computes whether multiBranch is set to true/false
func getMultiBranch(c *cli.Context) bool {
return !c.Bool("single-branch")
}
// createPipelinesDetailsByFlags creates pipelines configuration details
func createPipelinesDetailsByFlags(c *cli.Context) (*coreConfig.ServerDetails, error) {
plDetails, err := cliutils.CreateServerDetailsWithConfigOffer(c, true, cliutils.CmdPipelines)
if err != nil {
return nil, err
}
if plDetails.PipelinesUrl == "" {
return nil, errors.New("no JFrog Pipelines URL specified as part of the server configuration")
}
return plDetails, nil
}
// fetchLatestPipelineRunStatus fetches pipeline run status and filters from pipeline-name and branch flags
func fetchLatestPipelineRunStatus(c *cli.Context) error {
clientlog.Info(coreutils.PrintTitle("Fetching pipeline run status"))
// Read flags for status command
pipName := c.String("pipeline-name")
notify := c.Bool("monitor")
branch := c.String("branch")
multiBranch := getMultiBranch(c)
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}
statusCommand := pipelines.NewStatusCommand()
statusCommand.SetBranch(branch).
SetPipeline(pipName).
SetNotify(notify).
SetMultiBranch(multiBranch)
// Set server details
statusCommand.SetServerDetails(serviceDetails)
return commands.Exec(statusCommand)
}
// syncPipelineResources sync pipelines resource
func syncPipelineResources(c *cli.Context) error {
// Get arguments repository name and branch name
repository := c.Args().Get(0)
branch := c.Args().Get(1)
clientlog.Info("Triggering pipeline sync on repository:", repository, "branch:", branch)
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}
// Create new sync command and add filters
syncCommand := pipelines.NewSyncCommand()
syncCommand.SetBranch(branch)
syncCommand.SetRepositoryFullName(repository)
syncCommand.SetServerDetails(serviceDetails)
return commands.Exec(syncCommand)
}
// getSyncPipelineResourcesStatus fetch sync status for a given repository path and branch name
func getSyncPipelineResourcesStatus(c *cli.Context) error {
branch := c.String("branch")
if branch == "" {
return cliutils.PrintHelpAndReturnError("The --branch option is mandatory.", c)
}
repository := c.String("repository")
if repository == "" {
return cliutils.PrintHelpAndReturnError("The --repository option is mandatory.", c)
}
clientlog.Info("Fetching pipeline sync status on repository:", repository, "branch:", branch)
// Fetch service details for authentication
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}
// Create sync status command and add filter params
syncStatusCommand := pipelines.NewSyncStatusCommand()
syncStatusCommand.SetBranch(branch)
syncStatusCommand.SetRepoPath(repository)
syncStatusCommand.SetServerDetails(serviceDetails)
return commands.Exec(syncStatusCommand)
}
// getVersion version command handler
func getVersion(c *cli.Context) error {
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}
versionCommand := pipelines.NewVersionCommand()
versionCommand.SetServerDetails(serviceDetails)
return commands.Exec(versionCommand)
}
// triggerNewRun triggers a new run for supplied flag values
func triggerNewRun(c *cli.Context) error {
// Read arguments pipeline name and branch to trigger pipeline run
pipelineName := c.Args().Get(0)
branch := c.Args().Get(1)
multiBranch := getMultiBranch(c)
coreutils.PrintTitle("Triggering pipeline run ")
clientlog.Info("Triggering on pipeline:", pipelineName, "for branch:", branch)
// Get service config details
serviceDetails, err := createPipelinesDetailsByFlags(c)
if err != nil {
return err
}
// Trigger a pipeline run using branch name and pipeline name
triggerCommand := pipelines.NewTriggerCommand()
triggerCommand.SetBranch(branch).
SetPipelineName(pipelineName).
SetServerDetails(serviceDetails).
SetMultiBranch(multiBranch)
return commands.Exec(triggerCommand)
}