Skip to content

Commit fc62f25

Browse files
cr fixes
1 parent ed55460 commit fc62f25

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

internal/commands/scan.go

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,19 @@ import (
4343
)
4444

4545
const (
46-
failedCreating = "Failed creating a scan"
47-
failedGetting = "Failed showing a scan"
48-
failedGettingTags = "Failed getting tags"
49-
failedDeleting = "Failed deleting a scan"
50-
failedCanceling = "Failed canceling a scan"
51-
failedGettingAll = "Failed listing"
52-
thresholdLog = "%s: Limit = %d, Current = %v"
53-
thresholdMsgLog = "Threshold check finished with status %s : %s"
54-
mbBytes = 1024.0 * 1024.0
55-
notExploitable = "NOT_EXPLOITABLE"
56-
ignored = "IGNORED"
57-
minWindowsPathLength = 3
46+
failedCreating = "Failed creating a scan"
47+
failedGetting = "Failed showing a scan"
48+
failedGettingTags = "Failed getting tags"
49+
failedDeleting = "Failed deleting a scan"
50+
failedCanceling = "Failed canceling a scan"
51+
failedGettingAll = "Failed listing"
52+
thresholdLog = "%s: Limit = %d, Current = %v"
53+
thresholdMsgLog = "Threshold check finished with status %s : %s"
54+
mbBytes = 1024.0 * 1024.0
55+
notExploitable = "NOT_EXPLOITABLE"
56+
ignored = "IGNORED"
57+
minWindowsPathLength = 3
58+
containerImagesFlagError = "--container-images flag error"
5859

5960
git = "git"
6061
invalidSSHSource = "provided source does not need a key. Make sure you are defining the right source or remove the flag --ssh-key"
@@ -1263,7 +1264,7 @@ func addContainersScan(cmd *cobra.Command, resubmitConfig []wrappers.Config) (ma
12631264
continue
12641265
}
12651266
if containerImagesErr := validateContainerImageFormat(containerImageName); containerImagesErr != nil {
1266-
errorMsg := strings.TrimPrefix(containerImagesErr.Error(), "--container-images flag error: ")
1267+
errorMsg := strings.TrimPrefix(containerImagesErr.Error(), containerImagesFlagError+": ")
12671268
validationErrors = append(validationErrors, fmt.Sprintf("User input: '%s' error: %s", containerImageName, errorMsg))
12681269
}
12691270
}
@@ -3662,23 +3663,22 @@ func validateContainerImageFormat(containerImage string) error {
36623663
// It's a tar file - check if it exists locally
36633664
exists, err := osinstaller.FileExists(sanitizedInput)
36643665
if err != nil {
3665-
return errors.Errorf("--container-images flag error: %v", err)
3666+
return errors.Errorf("%s: %v", containerImagesFlagError, err)
36663667
}
36673668
if !exists {
3668-
return errors.Errorf("--container-images flag error: file '%s' does not exist", sanitizedInput)
3669+
return errors.Errorf("%s: file '%s' does not exist", containerImagesFlagError, sanitizedInput)
36693670
}
36703671
return nil // Valid tar file
36713672
}
36723673

36733674
// Check for compressed tar files
3674-
if strings.HasSuffix(lowerInput, ".tar.gz") || strings.HasSuffix(lowerInput, ".tar.bz2") ||
3675-
strings.HasSuffix(lowerInput, ".tar.xz") || strings.HasSuffix(lowerInput, ".tgz") {
3676-
return errors.Errorf("--container-images flag error: file '%s' is compressed, use non-compressed format (tar)", sanitizedInput)
3675+
if isCompressedTarFile(sanitizedInput) {
3676+
return errors.Errorf("%s: file '%s' is compressed, use non-compressed format (tar)", containerImagesFlagError, sanitizedInput)
36773677
}
36783678

36793679
// Check if it looks like a tar file extension (contains ".tar." but not a valid extension)
36803680
if strings.Contains(lowerInput, ".tar.") {
3681-
return errors.Errorf("--container-images flag error: image does not have a tag. Did you try to scan a tar file?")
3681+
return errors.Errorf("%s: image does not have a tag. Did you try to scan a tar file?", containerImagesFlagError)
36823682
}
36833683

36843684
// Step 4: Special handling for prefixes that don't require tags (e.g., oci-dir:)
@@ -3695,7 +3695,14 @@ func validateContainerImageFormat(containerImage string) error {
36953695
}
36963696

36973697
// Step 5: Not a tar file, no special prefix, and no colon - assume user forgot to add tag (error)
3698-
return errors.Errorf("--container-images flag error: image does not have a tag")
3698+
return errors.Errorf("%s: image does not have a tag", containerImagesFlagError)
3699+
}
3700+
3701+
// isCompressedTarFile checks if the given path has a compressed tar file extension.
3702+
func isCompressedTarFile(path string) bool {
3703+
lowerPath := strings.ToLower(path)
3704+
return strings.HasSuffix(lowerPath, ".tar.gz") || strings.HasSuffix(lowerPath, ".tar.bz2") ||
3705+
strings.HasSuffix(lowerPath, ".tar.xz") || strings.HasSuffix(lowerPath, ".tgz")
36993706
}
37003707

37013708
// looksLikeFilePath checks if input looks like a file path rather than image:tag.
@@ -3718,8 +3725,7 @@ func looksLikeFilePath(input string) bool {
37183725
return true
37193726
}
37203727

3721-
if strings.HasSuffix(lowerInput, ".tar.gz") || strings.HasSuffix(lowerInput, ".tar.bz2") ||
3722-
strings.HasSuffix(lowerInput, ".tar.xz") || strings.HasSuffix(lowerInput, ".tgz") {
3728+
if isCompressedTarFile(input) {
37233729
return true
37243730
}
37253731

@@ -3735,21 +3741,20 @@ func looksLikeFilePath(input string) bool {
37353741
func validateFilePath(filePath string) error {
37363742
lowerPath := strings.ToLower(filePath)
37373743

3738-
if strings.HasSuffix(lowerPath, ".tar.gz") || strings.HasSuffix(lowerPath, ".tar.bz2") ||
3739-
strings.HasSuffix(lowerPath, ".tar.xz") || strings.HasSuffix(lowerPath, ".tgz") {
3740-
return errors.Errorf("--container-images flag error: file '%s' is compressed, use non-compressed format (tar)", filePath)
3744+
if isCompressedTarFile(filePath) {
3745+
return errors.Errorf("%s: file '%s' is compressed, use non-compressed format (tar)", containerImagesFlagError, filePath)
37413746
}
37423747

37433748
if !strings.HasSuffix(lowerPath, ".tar") {
3744-
return errors.Errorf("--container-images flag error: file '%s' is not a valid tar file. Expected .tar extension", filePath)
3749+
return errors.Errorf("%s: file '%s' is not a valid tar file. Expected .tar extension", containerImagesFlagError, filePath)
37453750
}
37463751

37473752
exists, err := osinstaller.FileExists(filePath)
37483753
if err != nil {
3749-
return errors.Errorf("--container-images flag error: %v", err)
3754+
return errors.Errorf("%s: %v", containerImagesFlagError, err)
37503755
}
37513756
if !exists {
3752-
return errors.Errorf("--container-images flag error: file '%s' does not exist", filePath)
3757+
return errors.Errorf("%s: file '%s' does not exist", containerImagesFlagError, filePath)
37533758
}
37543759

37553760
return nil
@@ -3802,14 +3807,14 @@ func validatePrefixedContainerImage(containerImage, prefix string) error {
38023807
func validateArchivePrefix(imageRef string) error {
38033808
exists, err := osinstaller.FileExists(imageRef)
38043809
if err != nil {
3805-
return errors.Errorf("--container-images flag error: %v", err)
3810+
return errors.Errorf("%s: %v", containerImagesFlagError, err)
38063811
}
38073812
if !exists {
38083813
// Check if user mistakenly used archive prefix with an image name:tag format
38093814
if strings.Contains(imageRef, ":") && !strings.HasSuffix(strings.ToLower(imageRef), ".tar") {
3810-
return errors.Errorf("--container-images flag error: file '%s' does not exist. Did you try to scan an image using image name and tag?", imageRef)
3815+
return errors.Errorf("%s: file '%s' does not exist. Did you try to scan an image using image name and tag?", containerImagesFlagError, imageRef)
38113816
}
3812-
return errors.Errorf("--container-images flag error: file '%s' does not exist", imageRef)
3817+
return errors.Errorf("%s: file '%s' does not exist", containerImagesFlagError, imageRef)
38133818
}
38143819
return nil
38153820
}
@@ -3833,10 +3838,10 @@ func validateOCIDirPrefix(imageRef string) error {
38333838

38343839
exists, err := osinstaller.FileExists(pathToCheck)
38353840
if err != nil {
3836-
return errors.Errorf("--container-images flag error: path %s does not exist: %v", pathToCheck, err)
3841+
return errors.Errorf("%s: path %s does not exist: %v", containerImagesFlagError, pathToCheck, err)
38373842
}
38383843
if !exists {
3839-
return errors.Errorf("--container-images flag error: path %s does not exist", pathToCheck)
3844+
return errors.Errorf("%s: path %s does not exist", containerImagesFlagError, pathToCheck)
38403845
}
38413846
return nil
38423847
}

0 commit comments

Comments
 (0)