Skip to content

Commit e5223ab

Browse files
authored
Migrate distribution V1 commands to cli-artifactory (#1342)
* Moved cli utils to plugins common for migrating ds commands from cli to artifactory
1 parent 284c79c commit e5223ab

File tree

4 files changed

+259
-0
lines changed

4 files changed

+259
-0
lines changed

common/cliutils/spec.go

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cliutils
2+
3+
import (
4+
speccore "github.com/jfrog/jfrog-cli-core/v2/common/spec"
5+
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
6+
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
7+
"strings"
8+
)
9+
10+
func GetSpec(c *components.Context, isDownload, overrideFieldsIfSet bool) (specFiles *speccore.SpecFiles, err error) {
11+
specFiles, err = speccore.CreateSpecFromFile(c.GetStringFlagValue("spec"), coreutils.SpecVarsStringToMap(c.GetStringFlagValue("spec-vars")))
12+
if err != nil {
13+
return nil, err
14+
}
15+
if isDownload {
16+
trimPatternPrefix(specFiles)
17+
}
18+
if overrideFieldsIfSet {
19+
overrideSpecFields(c, specFiles)
20+
}
21+
return
22+
}
23+
24+
func overrideSpecFields(c *components.Context, specFiles *speccore.SpecFiles) {
25+
for i := 0; i < len(specFiles.Files); i++ {
26+
OverrideFieldsIfSet(specFiles.Get(i), c)
27+
}
28+
}
29+
30+
func trimPatternPrefix(specFiles *speccore.SpecFiles) {
31+
for i := 0; i < len(specFiles.Files); i++ {
32+
specFiles.Get(i).Pattern = strings.TrimPrefix(specFiles.Get(i).Pattern, "/")
33+
}
34+
}
35+
36+
func OverrideFieldsIfSet(spec *speccore.File, c *components.Context) {
37+
overrideArrayIfSet(&spec.Exclusions, c, "exclusions")
38+
overrideArrayIfSet(&spec.SortBy, c, "sort-by")
39+
overrideIntIfSet(&spec.Offset, c, "offset")
40+
overrideIntIfSet(&spec.Limit, c, "limit")
41+
overrideStringIfSet(&spec.SortOrder, c, "sort-order")
42+
overrideStringIfSet(&spec.Props, c, "props")
43+
overrideStringIfSet(&spec.TargetProps, c, "target-props")
44+
overrideStringIfSet(&spec.ExcludeProps, c, "exclude-props")
45+
overrideStringIfSet(&spec.Build, c, "build")
46+
overrideStringIfSet(&spec.Project, c, "project")
47+
overrideStringIfSet(&spec.ExcludeArtifacts, c, "exclude-artifacts")
48+
overrideStringIfSet(&spec.IncludeDeps, c, "include-deps")
49+
overrideStringIfSet(&spec.Bundle, c, "bundle")
50+
overrideStringIfSet(&spec.Recursive, c, "recursive")
51+
overrideStringIfSet(&spec.Flat, c, "flat")
52+
overrideStringIfSet(&spec.Explode, c, "explode")
53+
overrideStringIfSet(&spec.BypassArchiveInspection, c, "bypass-archive-inspection")
54+
overrideStringIfSet(&spec.Regexp, c, "regexp")
55+
overrideStringIfSet(&spec.IncludeDirs, c, "include-dirs")
56+
overrideStringIfSet(&spec.ValidateSymlinks, c, "validate-symlinks")
57+
overrideStringIfSet(&spec.Symlinks, c, "symlinks")
58+
overrideStringIfSet(&spec.Transitive, c, "transitive")
59+
overrideStringIfSet(&spec.PublicGpgKey, c, "gpg-key")
60+
}
61+
62+
// If `fieldName` exist in the cli args, read it to `field` as a string.
63+
func overrideStringIfSet(field *string, c *components.Context, fieldName string) {
64+
if c.IsFlagSet(fieldName) {
65+
*field = c.GetStringFlagValue(fieldName)
66+
}
67+
}
68+
69+
// If `fieldName` exist in the cli args, read it to `field` as an array split by `;`.
70+
func overrideArrayIfSet(field *[]string, c *components.Context, fieldName string) {
71+
if c.IsFlagSet(fieldName) {
72+
*field = append([]string{}, strings.Split(c.GetStringFlagValue(fieldName), ";")...)
73+
}
74+
}
75+
76+
// If `fieldName` exist in the cli args, read it to `field` as a int.
77+
func overrideIntIfSet(field *int, c *components.Context, fieldName string) {
78+
if c.IsFlagSet(fieldName) {
79+
*field, _ = c.GetIntFlagValue(fieldName)
80+
}
81+
}

common/cliutils/summary/summary.go

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package summary
2+
3+
import (
4+
"encoding/json"
5+
clientutils "github.com/jfrog/jfrog-client-go/utils"
6+
"github.com/jfrog/jfrog-client-go/utils/errorutils"
7+
"github.com/jfrog/jfrog-client-go/utils/log"
8+
"strings"
9+
)
10+
11+
type StatusType int
12+
13+
const (
14+
Success StatusType = iota
15+
Failure
16+
)
17+
18+
var StatusTypes = []string{
19+
"success",
20+
"failure",
21+
}
22+
23+
func (statusType StatusType) MarshalJSON() ([]byte, error) {
24+
return json.Marshal(StatusTypes[statusType])
25+
}
26+
27+
func (statusType *StatusType) UnmarshalJSON(b []byte) error {
28+
var s string
29+
if err := json.Unmarshal(b, &s); err != nil {
30+
return err
31+
}
32+
switch strings.ToLower(s) {
33+
default:
34+
*statusType = Failure
35+
case "success":
36+
*statusType = Success
37+
38+
}
39+
return nil
40+
}
41+
42+
func NewBuildInfoSummary(success, failed int, sha256 string, err error) *BuildInfoSummary {
43+
summaryReport := GetSummaryReport(success, failed, false, err)
44+
buildInfoSummary := BuildInfoSummary{Summary: *summaryReport, Sha256Array: []Sha256{}}
45+
if success == 1 {
46+
buildInfoSummary.AddSha256(sha256)
47+
}
48+
return &buildInfoSummary
49+
}
50+
51+
func (summary *Summary) Marshal() ([]byte, error) {
52+
return json.Marshal(summary)
53+
}
54+
55+
func (bis *BuildInfoSummary) Marshal() ([]byte, error) {
56+
return json.Marshal(bis)
57+
}
58+
59+
type Summary struct {
60+
Status StatusType `json:"status"`
61+
Totals *Totals `json:"totals"`
62+
}
63+
64+
type Totals struct {
65+
Success int `json:"success"`
66+
Failure int `json:"failure"`
67+
}
68+
69+
type BuildInfoSummary struct {
70+
Summary
71+
Sha256Array []Sha256 `json:"files"`
72+
}
73+
74+
type Sha256 struct {
75+
Sha256Str string `json:"sha256"`
76+
}
77+
78+
func (bis *BuildInfoSummary) AddSha256(sha256Str string) {
79+
sha256 := Sha256{Sha256Str: sha256Str}
80+
bis.Sha256Array = append(bis.Sha256Array, sha256)
81+
}
82+
83+
func GetSummaryReport(success, failed int, failNoOp bool, err error) *Summary {
84+
summary := &Summary{Totals: &Totals{}}
85+
if err != nil || failed > 0 || (success == 0 && failNoOp) {
86+
summary.Status = Failure
87+
} else {
88+
summary.Status = Success
89+
}
90+
summary.Totals.Success = success
91+
summary.Totals.Failure = failed
92+
return summary
93+
}
94+
95+
func PrintBuildInfoSummaryReport(succeeded bool, sha256 string, originalErr error) error {
96+
success, failed := 1, 0
97+
if !succeeded {
98+
success, failed = 0, 1
99+
}
100+
buildInfoSummary, mErr := CreateBuildInfoSummaryReportString(success, failed, sha256, originalErr)
101+
if mErr != nil {
102+
return summaryPrintError(mErr, originalErr)
103+
}
104+
log.Output(buildInfoSummary)
105+
return summaryPrintError(mErr, originalErr)
106+
}
107+
108+
func CreateBuildInfoSummaryReportString(success, failed int, sha256 string, err error) (string, error) {
109+
buildInfoSummary := NewBuildInfoSummary(success, failed, sha256, err)
110+
buildInfoSummaryContent, mErr := buildInfoSummary.Marshal()
111+
if errorutils.CheckError(mErr) != nil {
112+
return "", mErr
113+
}
114+
return clientutils.IndentJson(buildInfoSummaryContent), mErr
115+
}
116+
117+
// Print summary report.
118+
// a given non-nil error will pass through and be returned as is if no other errors are raised.
119+
// In case of a nil error, the current function error will be returned.
120+
func summaryPrintError(summaryError, originalError error) error {
121+
if originalError != nil {
122+
if summaryError != nil {
123+
log.Error(summaryError)
124+
}
125+
return originalError
126+
}
127+
return summaryError
128+
}

plugins/common/utils.go

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package common
22

33
import (
4+
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
5+
clientutils "github.com/jfrog/jfrog-client-go/utils"
46
"sort"
57
"strconv"
68
"strings"
@@ -100,3 +102,25 @@ func buildAndSortFlags(keys []string, flagsMap map[string]components.Flag) (flag
100102
sort.Slice(flags, func(i, j int) bool { return flags[i].GetName() < flags[j].GetName() })
101103
return
102104
}
105+
106+
// This function indicates whether the command should be executed without
107+
// confirmation warning or not.
108+
// If the --quiet option was sent, it is used to determine whether to prompt the confirmation or not.
109+
// If not, the command will prompt the confirmation, unless the CI environment variable was set to true.
110+
func GetQuietValue(c *components.Context) bool {
111+
if c.IsFlagSet("quiet") {
112+
return c.GetBoolFlagValue("quiet")
113+
}
114+
115+
return getCiValue()
116+
}
117+
118+
// Return true if the CI environment variable was set to true.
119+
func getCiValue() bool {
120+
var ci bool
121+
var err error
122+
if ci, err = clientutils.GetBoolEnvValue(coreutils.CI, false); err != nil {
123+
return false
124+
}
125+
return ci
126+
}

plugins/components/commandcomp.go

+26
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@ func SetHiddenStrFlag() StringFlagOption {
171171
}
172172
}
173173

174+
func SetMandatoryFalse() StringFlagOption {
175+
return func(f *StringFlag) {
176+
f.Mandatory = false
177+
}
178+
}
179+
180+
func WithBoolDefaultValueFalse() BoolFlagOption {
181+
return func(f *BoolFlag) {
182+
f.DefaultValue = false
183+
}
184+
}
185+
174186
type BoolFlag struct {
175187
BaseFlag
176188
DefaultValue bool
@@ -201,3 +213,17 @@ func SetHiddenBoolFlag() BoolFlagOption {
201213
f.Hidden = true
202214
}
203215
}
216+
217+
func (c *Context) WithDefaultIntFlagValue(flagName string, defValue int) (value int, err error) {
218+
value = defValue
219+
if c.IsFlagSet(flagName) {
220+
var parsed int64
221+
parsed, err = strconv.ParseInt(c.GetStringFlagValue(flagName), 0, 64)
222+
if err != nil {
223+
err = fmt.Errorf("can't parse int flag '%s': %w", flagName, err)
224+
return
225+
}
226+
value = int(parsed)
227+
}
228+
return
229+
}

0 commit comments

Comments
 (0)