Skip to content

Commit

Permalink
Removing contents after colon in multiple image/project scenario (#4855)
Browse files Browse the repository at this point in the history
* Removing contents after colon in multiple image/project scenario

* removed unused pkgs
  • Loading branch information
vijayanjay authored Mar 5, 2024
1 parent c7fc38c commit a675ed2
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions cmd/whitesourceExecuteScan.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -208,7 +207,7 @@ func runWhitesourceScan(ctx context.Context, config *ScanOptions, scan *ws.Scan,
if len(config.ScanImages) != 0 && config.ActivateMultipleImagesScan {
for _, image := range config.ScanImages {
config.ScanImage = image
err := downloadDockerImageAsTarNew(config, utils)
err := downloadMultipleDockerImageAsTar(config, utils)
if err != nil {
return errors.Wrapf(err, "failed to download docker image")
}
Expand Down Expand Up @@ -1093,7 +1092,7 @@ func createToolRecordWhitesource(utils whitesourceUtils, workspace string, confi
return record.GetFileName(), nil
}

func downloadDockerImageAsTarNew(config *ScanOptions, utils whitesourceUtils) error {
func downloadMultipleDockerImageAsTar(config *ScanOptions, utils whitesourceUtils) error {

imageNameToSave := strings.Replace(config.ScanImage, "/", "-", -1)

Expand All @@ -1116,7 +1115,7 @@ func downloadDockerImageAsTarNew(config *ScanOptions, utils whitesourceUtils) er
}
return errors.Wrapf(err, "failed to download Docker image %v", config.ScanImage)
}
// to remove timestamp and artifact version
// remove contents after : in the image name
if err := renameTarfilePath(tarFilePath); err != nil {
return errors.Wrapf(err, "failed to rename image %v", err)
}
Expand Down Expand Up @@ -1148,19 +1147,18 @@ func downloadDockerImageAsTar(config *ScanOptions, utils whitesourceUtils) error
return nil
}

// rename tarFilepath to remove all contents after :
func renameTarfilePath(tarFilepath string) error {
if _, err := os.Stat(tarFilepath); os.IsNotExist(err) {
return fmt.Errorf("file %s does not exist", tarFilepath)
}
pattern := `-\d{14}_[a-f0-9]{40}\.tar$` //format is -<timestamp>_<commitHash>.tar
regex := regexp.MustCompile(pattern)
if regex.MatchString(tarFilepath) {
newName := regex.ReplaceAllString(tarFilepath, ".tar")
err := os.Rename(tarFilepath, newName)
if err != nil {
return fmt.Errorf("error renaming file %s to %s: %v", tarFilepath, newName, err)
}
log.Entry().Infof("Renamed file %s to %s\n", tarFilepath, newName)
newFileName := ""
if index := strings.Index(tarFilepath, ":"); index != -1 {
newFileName = tarFilepath[:index]
newFileName += ".tar"
}
if err := os.Rename(tarFilepath, newFileName); err != nil {
return fmt.Errorf("error renaming file %s to %s: %v", tarFilepath, newFileName, err)
}
return nil
}

0 comments on commit a675ed2

Please sign in to comment.