forked from jfrog/jfrog-cli-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
377 lines (334 loc) · 12.8 KB
/
utils.go
File metadata and controls
377 lines (334 loc) · 12.8 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package common
import (
"errors"
commandUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
artifactoryUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/commandsummary"
buildUtils "github.com/jfrog/jfrog-cli-core/v2/common/build"
"github.com/jfrog/jfrog-cli-core/v2/common/cliutils/summary"
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
clientutils "github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/io/content"
"os"
"sort"
"strconv"
"strings"
"github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
"github.com/jfrog/jfrog-client-go/utils/log"
"golang.org/x/exp/slices"
)
type DetailedSummaryRecord struct {
Source string `json:"source,omitempty"`
Target string `json:"target"`
}
type ExtendedDetailedSummaryRecord struct {
DetailedSummaryRecord
Sha256 string `json:"sha256"`
}
func GetStringsArrFlagValue(c *components.Context, flagName string) (resultArray []string) {
if c.IsFlagSet(flagName) {
resultArray = append(resultArray, strings.Split(c.GetStringFlagValue(flagName), ";")...)
}
return
}
// If `fieldName` exist in the cli args, read it to `field` as an array split by `;`.
func OverrideArrayIfSet(field *[]string, c *components.Context, fieldName string) {
if c.IsFlagSet(fieldName) {
*field = append([]string{}, strings.Split(c.GetStringFlagValue(fieldName), ";")...)
}
}
// If `fieldName` exist in the cli args, read it to `field` as a int.
func OverrideIntIfSet(field *int, c *components.Context, fieldName string) {
if c.IsFlagSet(fieldName) {
value, err := strconv.ParseInt(c.GetStringFlagValue(fieldName), 0, 64)
if err != nil {
return
}
*field = int(value)
}
}
// If `fieldName` exist in the cli args, read it to `field` as a string.
func OverrideStringIfSet(field *string, c *components.Context, fieldName string) {
if c.IsFlagSet(fieldName) {
*field = c.GetStringFlagValue(fieldName)
}
}
// Get a secret value from a flag or from stdin.
func HandleSecretInput(c *components.Context, stringFlag, stdinFlag string) (secret string, err error) {
return cliutils.HandleSecretInput(stringFlag, c.GetStringFlagValue(stringFlag), stdinFlag, c.GetBoolFlagValue(stdinFlag))
}
func RunCmdWithDeprecationWarning(cmdName, oldSubcommand string, c *components.Context,
cmd func(c *components.Context) error) error {
cliutils.LogNonNativeCommandDeprecation(cmdName, oldSubcommand)
return cmd(c)
}
func GetThreadsCount(c *components.Context) (threads int, err error) {
return cliutils.GetThreadsCount(c.GetStringFlagValue("threads"))
}
func GetPrintCurrentCmdHelp(c *components.Context) func() error {
return func() error {
return c.PrintCommandHelp(c.CommandName)
}
}
// This function checks whether the command received --help as a single option.
// If it did, the command's help is shown and true is returned.
// This function should be used iff the SkipFlagParsing option is used.
func ShowCmdHelpIfNeeded(c *components.Context, args []string) (bool, error) {
return cliutils.ShowCmdHelpIfNeeded(args, GetPrintCurrentCmdHelp(c))
}
func PrintHelpAndReturnError(msg string, context *components.Context) error {
return cliutils.PrintHelpAndReturnError(msg, GetPrintCurrentCmdHelp(context))
}
func WrongNumberOfArgumentsHandler(context *components.Context) error {
return cliutils.WrongNumberOfArgumentsHandler(len(context.Arguments), GetPrintCurrentCmdHelp(context))
}
func ExtractArguments(context *components.Context) []string {
return slices.Clone(context.Arguments)
}
// Return a sorted list of a command's flags by a given command key.
func GetCommandFlags(cmdKey string, commandToFlags map[string][]string, flagsMap map[string]components.Flag) []components.Flag {
flagList, ok := commandToFlags[cmdKey]
if !ok {
log.Error("The command \"", cmdKey, "\" is not found in commands flags map.")
return nil
}
return buildAndSortFlags(flagList, flagsMap)
}
func buildAndSortFlags(keys []string, flagsMap map[string]components.Flag) (flags []components.Flag) {
for _, flag := range keys {
flags = append(flags, flagsMap[flag])
}
sort.Slice(flags, func(i, j int) bool { return flags[i].GetName() < flags[j].GetName() })
return
}
// This function indicates whether the command should be executed without
// confirmation warning or not.
// If the --quiet option was sent, it is used to determine whether to prompt the confirmation or not.
// If not, the command will prompt the confirmation, unless the CI environment variable was set to true.
func GetQuietValue(c *components.Context) bool {
if c.IsFlagSet("quiet") {
return c.GetBoolFlagValue("quiet")
}
return getCiValue()
}
// Return true if the CI environment variable was set to true.
func getCiValue() bool {
var ci bool
var err error
if ci, err = clientutils.GetBoolEnvValue(coreutils.CI, false); err != nil {
return false
}
return ci
}
func CreateArtifactoryDetailsByFlags(c *components.Context) (*coreConfig.ServerDetails, error) {
artDetails, err := CreateServerDetailsWithConfigOffer(c, false, cliutils.Rt)
if err != nil {
return nil, err
}
if artDetails.ArtifactoryUrl == "" {
return nil, errors.New("no JFrog Artifactory URL specified, either via the --url flag or as part of the server configuration")
}
return artDetails, nil
}
// Returns build configuration struct using the options provided by the user.
// Any empty configuration could be later overridden by environment variables if set.
func CreateBuildConfigurationWithModule(c *components.Context) (buildConfigConfiguration *buildUtils.BuildConfiguration, err error) {
buildConfigConfiguration = new(buildUtils.BuildConfiguration)
err = buildConfigConfiguration.SetBuildName(c.GetStringFlagValue("build-name")).SetBuildNumber(c.GetStringFlagValue("build-number")).
SetProject(c.GetStringFlagValue("project")).SetModule(c.GetStringFlagValue("module")).ValidateBuildAndModuleParams()
return
}
func ExtractCommand(c *components.Context) []string {
return slices.Clone(c.Arguments)
}
func IsFailNoOp(context *components.Context) bool {
if isContextFailNoOp(context) {
return true
}
return isEnvFailNoOp()
}
func isContextFailNoOp(context *components.Context) bool {
if context == nil {
return false
}
return context.GetBoolFlagValue("fail-no-op")
}
func isEnvFailNoOp() bool {
return strings.ToLower(os.Getenv(coreutils.FailNoOp)) == "true"
}
// Get project key from flag or environment variable
func GetProject(c *components.Context) string {
projectKey := c.GetStringFlagValue("project")
return getOrDefaultEnv(projectKey, coreutils.Project)
}
// Return argument if not empty or retrieve from environment variable
func getOrDefaultEnv(arg, envKey string) string {
if arg != "" {
return arg
}
return os.Getenv(envKey)
}
func GetBuildName(buildName string) string {
return getOrDefaultEnv(buildName, coreutils.BuildName)
}
func GetBuildUrl(buildUrl string) string {
return getOrDefaultEnv(buildUrl, coreutils.BuildUrl)
}
func GetEnvExclude(envExclude string) string {
return getOrDefaultEnv(envExclude, coreutils.EnvExclude)
}
func GetDocumentationMessage() string {
return "You can read the documentation at " + coreutils.JFrogHelpUrl + "jfrog-cli"
}
func CleanupResult(result *commandUtils.Result, err *error) {
if result != nil && result.Reader() != nil {
*err = errors.Join(*err, result.Reader().Close())
}
}
func PrintCommandSummary(result *commandUtils.Result, detailedSummary, printDeploymentView, failNoOp bool, originalErr error) (err error) {
// We would like to print a basic summary of total failures/successes in the case of an error.
err = originalErr
if result == nil {
// We don't have a total of failures/successes artifacts, so we are done.
return
}
defer func() {
err = GetCliError(err, result.SuccessCount(), result.FailCount(), failNoOp)
}()
basicSummary, err := CreateSummaryReportString(result.SuccessCount(), result.FailCount(), failNoOp, err)
if err != nil {
// Print the basic summary and return the original error.
log.Output(basicSummary)
return
}
if detailedSummary {
err = PrintDetailedSummaryReport(basicSummary, result.Reader(), true, err)
} else {
if printDeploymentView {
err = PrintDeploymentView(result.Reader())
}
log.Output(basicSummary)
}
return
}
func GetCliError(err error, success, failed int, failNoOp bool) error {
switch coreutils.GetExitCode(err, success, failed, failNoOp) {
case coreutils.ExitCodeError:
{
var errorMessage string
if err != nil {
errorMessage = err.Error()
}
return coreutils.CliError{ExitCode: coreutils.ExitCodeError, ErrorMsg: errorMessage}
}
case coreutils.ExitCodeFailNoOp:
return coreutils.CliError{ExitCode: coreutils.ExitCodeFailNoOp, ErrorMsg: "No errors, but also no files affected (fail-no-op flag)."}
default:
return nil
}
}
func CreateSummaryReportString(success, failed int, failNoOp bool, err error) (string, error) {
summaryReport := summary.GetSummaryReport(success, failed, failNoOp, err)
summaryContent, mErr := summaryReport.Marshal()
if errorutils.CheckError(mErr) != nil {
// Don't swallow the original error. Log the marshal error and return the original error.
return "", summaryPrintError(mErr, err)
}
return clientutils.IndentJson(summaryContent), err
}
// Prints a summary report.
// If a resultReader is provided, we will iterate over the result and print a detailed summary including the affected files.
func PrintDetailedSummaryReport(basicSummary string, reader *content.ContentReader, uploaded bool, originalErr error) error {
// A reader wasn't provided, prints the basic summary json and return.
if reader == nil {
log.Output(basicSummary)
return nil
}
writer, mErr := content.NewContentWriter("files", false, true)
if mErr != nil {
log.Output(basicSummary)
return summaryPrintError(mErr, originalErr)
}
// We remove the closing curly bracket in order to append the affected files array using a responseWriter to write directly to stdout.
basicSummary = strings.TrimSuffix(basicSummary, "\n}") + ","
log.Output(basicSummary)
defer log.Output("}")
readerLength, _ := reader.Length()
// If the reader is empty we will print an empty array.
if readerLength == 0 {
log.Output(" \"files\": []")
} else {
for transferDetails := new(clientutils.FileTransferDetails); reader.NextRecord(transferDetails) == nil; transferDetails = new(clientutils.FileTransferDetails) {
writer.Write(getDetailedSummaryRecord(transferDetails, uploaded))
}
reader.Reset()
}
mErr = writer.Close()
if mErr != nil {
return summaryPrintError(mErr, originalErr)
}
rErr := reader.GetError()
if rErr != nil {
return summaryPrintError(rErr, originalErr)
}
return summaryPrintError(reader.GetError(), originalErr)
}
// Print a file tree based on the items' path in the reader's list.
func PrintDeploymentView(reader *content.ContentReader) error {
tree := artifactoryUtils.NewFileTree()
for transferDetails := new(clientutils.FileTransferDetails); reader.NextRecord(transferDetails) == nil; transferDetails = new(clientutils.FileTransferDetails) {
tree.AddFile(transferDetails.TargetPath, "")
}
if err := reader.GetError(); err != nil {
return err
}
reader.Reset()
output := tree.String()
if len(output) > 0 {
log.Info("These files were uploaded:\n\n" + output)
}
return nil
}
// Get the detailed summary record.
// For uploads, we need to print the sha256 of the uploaded file along with the source and target, and prefix the target with the Artifactory URL.
func getDetailedSummaryRecord(transferDetails *clientutils.FileTransferDetails, uploaded bool) interface{} {
record := DetailedSummaryRecord{
Source: transferDetails.SourcePath,
Target: transferDetails.TargetPath,
}
if uploaded {
record.Target = transferDetails.RtUrl + record.Target
extendedRecord := ExtendedDetailedSummaryRecord{
DetailedSummaryRecord: record,
Sha256: transferDetails.Sha256,
}
return extendedRecord
}
record.Source = transferDetails.RtUrl + record.Source
return record
}
// Print summary report.
// a given non-nil error will pass through and be returned as is if no other errors are raised.
// In case of a nil error, the current function error will be returned.
func summaryPrintError(summaryError, originalError error) error {
if originalError != nil {
if summaryError != nil {
log.Error(summaryError)
}
return originalError
}
return summaryError
}
func GetDetailedSummary(c *components.Context) bool {
return c.GetBoolFlagValue("detailed-summary") || commandsummary.ShouldRecordSummary()
}
func PrintBriefSummaryReport(success, failed int, failNoOp bool, originalErr error) error {
basicSummary, mErr := CreateSummaryReportString(success, failed, failNoOp, originalErr)
if mErr == nil {
log.Output(basicSummary)
}
return summaryPrintError(mErr, originalErr)
}