-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/dev'
- Loading branch information
Showing
20 changed files
with
512 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package yarn | ||
|
||
import ( | ||
gofrogcmd "github.com/jfrog/gofrog/io" | ||
"github.com/jfrog/gofrog/version" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"strings" | ||
) | ||
|
||
const unsupportedYarnVersion = "4.0.0" | ||
|
||
func IsInstalledYarnVersionSupported(executablePath string) error { | ||
versionGetCmdConfig := getVersionCmdConfig(executablePath) | ||
output, err := gofrogcmd.RunCmdOutput(versionGetCmdConfig) | ||
if err != nil { | ||
return errorutils.CheckError(err) | ||
} | ||
yarnVersion := strings.TrimSpace(output) | ||
return IsVersionSupported(yarnVersion) | ||
} | ||
|
||
func IsVersionSupported(versionStr string) error { | ||
yarnVersion := version.NewVersion(versionStr) | ||
if yarnVersion.Compare(unsupportedYarnVersion) <= 0 { | ||
return errorutils.CheckErrorf("Yarn version 4 is not supported. The current version is: " + versionStr + | ||
". Please downgrade to a compatible version to continue") | ||
} | ||
return nil | ||
} | ||
|
||
func getVersionCmdConfig(executablePath string) *YarnConfig { | ||
return &YarnConfig{ | ||
Executable: executablePath, | ||
Command: []string{"--version"}, | ||
CommandFlags: nil, | ||
StrWriter: nil, | ||
ErrWriter: nil, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package yarn | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestIsVersionSupported(t *testing.T) { | ||
tests := []struct { | ||
versionStr string | ||
expectErr bool | ||
}{ | ||
{"3.9.0", false}, | ||
{"4.0.0", true}, | ||
{"4.1.0", true}, | ||
} | ||
|
||
for _, test := range tests { | ||
err := IsVersionSupported(test.versionStr) | ||
if test.expectErr { | ||
assert.Error(t, err, "Expected an error for version: %s", test.versionStr) | ||
} else { | ||
assert.NoError(t, err, "Did not expect an error for version: %s", test.versionStr) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package cliutils | ||
|
||
import ( | ||
speccore "github.com/jfrog/jfrog-cli-core/v2/common/spec" | ||
"github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" | ||
"strings" | ||
) | ||
|
||
func GetSpec(c *components.Context, isDownload, overrideFieldsIfSet bool) (specFiles *speccore.SpecFiles, err error) { | ||
specFiles, err = speccore.CreateSpecFromFile(c.GetStringFlagValue("spec"), coreutils.SpecVarsStringToMap(c.GetStringFlagValue("spec-vars"))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if isDownload { | ||
trimPatternPrefix(specFiles) | ||
} | ||
if overrideFieldsIfSet { | ||
overrideSpecFields(c, specFiles) | ||
} | ||
return | ||
} | ||
|
||
func overrideSpecFields(c *components.Context, specFiles *speccore.SpecFiles) { | ||
for i := 0; i < len(specFiles.Files); i++ { | ||
OverrideFieldsIfSet(specFiles.Get(i), c) | ||
} | ||
} | ||
|
||
func trimPatternPrefix(specFiles *speccore.SpecFiles) { | ||
for i := 0; i < len(specFiles.Files); i++ { | ||
specFiles.Get(i).Pattern = strings.TrimPrefix(specFiles.Get(i).Pattern, "/") | ||
} | ||
} | ||
|
||
func OverrideFieldsIfSet(spec *speccore.File, c *components.Context) { | ||
overrideArrayIfSet(&spec.Exclusions, c, "exclusions") | ||
overrideArrayIfSet(&spec.SortBy, c, "sort-by") | ||
overrideIntIfSet(&spec.Offset, c, "offset") | ||
overrideIntIfSet(&spec.Limit, c, "limit") | ||
overrideStringIfSet(&spec.SortOrder, c, "sort-order") | ||
overrideStringIfSet(&spec.Props, c, "props") | ||
overrideStringIfSet(&spec.TargetProps, c, "target-props") | ||
overrideStringIfSet(&spec.ExcludeProps, c, "exclude-props") | ||
overrideStringIfSet(&spec.Build, c, "build") | ||
overrideStringIfSet(&spec.Project, c, "project") | ||
overrideStringIfSet(&spec.ExcludeArtifacts, c, "exclude-artifacts") | ||
overrideStringIfSet(&spec.IncludeDeps, c, "include-deps") | ||
overrideStringIfSet(&spec.Bundle, c, "bundle") | ||
overrideStringIfSet(&spec.Recursive, c, "recursive") | ||
overrideStringIfSet(&spec.Flat, c, "flat") | ||
overrideStringIfSet(&spec.Explode, c, "explode") | ||
overrideStringIfSet(&spec.BypassArchiveInspection, c, "bypass-archive-inspection") | ||
overrideStringIfSet(&spec.Regexp, c, "regexp") | ||
overrideStringIfSet(&spec.IncludeDirs, c, "include-dirs") | ||
overrideStringIfSet(&spec.ValidateSymlinks, c, "validate-symlinks") | ||
overrideStringIfSet(&spec.Symlinks, c, "symlinks") | ||
overrideStringIfSet(&spec.Transitive, c, "transitive") | ||
overrideStringIfSet(&spec.PublicGpgKey, c, "gpg-key") | ||
} | ||
|
||
// 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) | ||
} | ||
} | ||
|
||
// 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) { | ||
*field, _ = c.GetIntFlagValue(fieldName) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package summary | ||
|
||
import ( | ||
"encoding/json" | ||
clientutils "github.com/jfrog/jfrog-client-go/utils" | ||
"github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
"github.com/jfrog/jfrog-client-go/utils/log" | ||
"strings" | ||
) | ||
|
||
type StatusType int | ||
|
||
const ( | ||
Success StatusType = iota | ||
Failure | ||
) | ||
|
||
var StatusTypes = []string{ | ||
"success", | ||
"failure", | ||
} | ||
|
||
func (statusType StatusType) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(StatusTypes[statusType]) | ||
} | ||
|
||
func (statusType *StatusType) UnmarshalJSON(b []byte) error { | ||
var s string | ||
if err := json.Unmarshal(b, &s); err != nil { | ||
return err | ||
} | ||
switch strings.ToLower(s) { | ||
default: | ||
*statusType = Failure | ||
case "success": | ||
*statusType = Success | ||
|
||
} | ||
return nil | ||
} | ||
|
||
func NewBuildInfoSummary(success, failed int, sha256 string, err error) *BuildInfoSummary { | ||
summaryReport := GetSummaryReport(success, failed, false, err) | ||
buildInfoSummary := BuildInfoSummary{Summary: *summaryReport, Sha256Array: []Sha256{}} | ||
if success == 1 { | ||
buildInfoSummary.AddSha256(sha256) | ||
} | ||
return &buildInfoSummary | ||
} | ||
|
||
func (summary *Summary) Marshal() ([]byte, error) { | ||
return json.Marshal(summary) | ||
} | ||
|
||
func (bis *BuildInfoSummary) Marshal() ([]byte, error) { | ||
return json.Marshal(bis) | ||
} | ||
|
||
type Summary struct { | ||
Status StatusType `json:"status"` | ||
Totals *Totals `json:"totals"` | ||
} | ||
|
||
type Totals struct { | ||
Success int `json:"success"` | ||
Failure int `json:"failure"` | ||
} | ||
|
||
type BuildInfoSummary struct { | ||
Summary | ||
Sha256Array []Sha256 `json:"files"` | ||
} | ||
|
||
type Sha256 struct { | ||
Sha256Str string `json:"sha256"` | ||
} | ||
|
||
func (bis *BuildInfoSummary) AddSha256(sha256Str string) { | ||
sha256 := Sha256{Sha256Str: sha256Str} | ||
bis.Sha256Array = append(bis.Sha256Array, sha256) | ||
} | ||
|
||
func GetSummaryReport(success, failed int, failNoOp bool, err error) *Summary { | ||
summary := &Summary{Totals: &Totals{}} | ||
if err != nil || failed > 0 || (success == 0 && failNoOp) { | ||
summary.Status = Failure | ||
} else { | ||
summary.Status = Success | ||
} | ||
summary.Totals.Success = success | ||
summary.Totals.Failure = failed | ||
return summary | ||
} | ||
|
||
func PrintBuildInfoSummaryReport(succeeded bool, sha256 string, originalErr error) error { | ||
success, failed := 1, 0 | ||
if !succeeded { | ||
success, failed = 0, 1 | ||
} | ||
buildInfoSummary, mErr := CreateBuildInfoSummaryReportString(success, failed, sha256, originalErr) | ||
if mErr != nil { | ||
return summaryPrintError(mErr, originalErr) | ||
} | ||
log.Output(buildInfoSummary) | ||
return summaryPrintError(mErr, originalErr) | ||
} | ||
|
||
func CreateBuildInfoSummaryReportString(success, failed int, sha256 string, err error) (string, error) { | ||
buildInfoSummary := NewBuildInfoSummary(success, failed, sha256, err) | ||
buildInfoSummaryContent, mErr := buildInfoSummary.Marshal() | ||
if errorutils.CheckError(mErr) != nil { | ||
return "", mErr | ||
} | ||
return clientutils.IndentJson(buildInfoSummaryContent), mErr | ||
} | ||
|
||
// 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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package commands | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
// This test demonstrates an existing bug in the urfave/cli library. | ||
// It highlights that both BoolT and Bool flags have their default values set to false. | ||
// Ideally, the BoolT flag should have a default value of true. | ||
func TestBoolVsBoolTFlag(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args []string | ||
shouldUseBoolT bool | ||
expectValue bool | ||
}{ | ||
{"Resolving flag value using Bool (default false)", []string{"cmd"}, false, false}, | ||
{"Resolving flag value using Bool (default false)", []string{"cmd"}, true, false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
app := &cli.App{ | ||
Flags: []cli.Flag{ | ||
&cli.BoolFlag{ | ||
Name: "myflag", | ||
Usage: "Test boolean flag", | ||
}, | ||
}, | ||
Action: func(c *cli.Context) error { | ||
if tt.shouldUseBoolT { | ||
assert.Equal(t, tt.expectValue, c.BoolT("myflag"), "Expected %v, got %v", tt.expectValue, c.BoolT("myflag")) | ||
} else { | ||
assert.Equal(t, tt.expectValue, c.Bool("myflag"), "Expected %v, got %v", tt.expectValue, c.Bool("myflag")) | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
_ = app.Run(tt.args) | ||
}) | ||
} | ||
} |
Oops, something went wrong.