github.com/jfrog/build-info-go@v1.9.26/utils/dependenciesutils.go (about)

     1  package utils
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"path"
     8  	"path/filepath"
     9  	"time"
    10  )
    11  
    12  const (
    13  	configPropertiesPathTempPrefix = "extractorProperties"
    14  	buildInfoPathKey               = "buildInfo.generated.build.info"
    15  	buildNameKey                   = "buildInfo.build.name"
    16  	buildTimestampKey              = "buildInfo.build.timestamp"
    17  	buildNumberKey                 = "buildInfo.build.number"
    18  	projectKey                     = "buildInfo.build.project"
    19  )
    20  
    21  // Download the relevant build-info-extractor jar, if it does not already exist locally.
    22  // By default, the jar is downloaded directly from https://releases.jfrog.io/artifactory/oss-release-local.
    23  //
    24  // downloadPath: download path in the remote server.
    25  // filename: The local file name.
    26  // targetPath: The local download path (without the file name).
    27  func downloadExtractorIfNeeded(downloadTo, filename, downloadPath string, downloadExtractorFunc func(downloadTo, downloadPath string) error, logger Log) error {
    28  	// If the file exists locally, we're done.
    29  	absFileName := filepath.Join(downloadTo, filename)
    30  	exists, err := IsFileExists(absFileName, true)
    31  	if exists || err != nil {
    32  		return err
    33  	}
    34  	if err := os.MkdirAll(downloadTo, 0777); err != nil {
    35  		return err
    36  	}
    37  	if downloadExtractorFunc != nil {
    38  		// Override default download.
    39  		return downloadExtractorFunc(absFileName, downloadPath)
    40  	}
    41  	extractorUrl := "https://releases.jfrog.io/artifactory/oss-release-local/" + downloadPath
    42  	logger.Info("Downloading build-info-extractor from", extractorUrl, "to", downloadTo)
    43  	return DownloadFile(absFileName, extractorUrl)
    44  }
    45  
    46  // Save all the extractor's properties into a local file.
    47  // extractorConfPath - Path to a file to which all the extractor's properties will be written.
    48  // buildInfoPath - Path to a file to which the build-info data will be written.
    49  // buildName - Build name of the current build.
    50  // buildNumber - Build number of the current build.
    51  // project - JFrog Project key of the current build
    52  // configProperties - Data of the actual extractor's properties.
    53  // Returns the extractor Config file path.
    54  func CreateExtractorPropsFile(extractorConfPath, buildInfoPath, buildName, buildNumber string, buildTimestamp time.Time, project string, configProperties map[string]string) (string, error) {
    55  	if err := os.MkdirAll(extractorConfPath, 0777); err != nil {
    56  		return "", err
    57  	}
    58  	propertiesFile, err := os.CreateTemp(extractorConfPath, configPropertiesPathTempPrefix)
    59  	if err != nil {
    60  		return "", err
    61  	}
    62  	defer func() {
    63  		err = errors.Join(err, propertiesFile.Close())
    64  	}()
    65  	var buildProperties = map[string]string{
    66  		buildInfoPathKey:  buildInfoPath,
    67  		buildNameKey:      buildName,
    68  		buildTimestampKey: fmt.Sprintf("%d", buildTimestamp.UnixMilli()),
    69  		buildNumberKey:    buildNumber,
    70  		projectKey:        project,
    71  	}
    72  	return propertiesFile.Name(), writeProps(propertiesFile, configProperties, buildProperties)
    73  }
    74  
    75  func DownloadDependencies(downloadTo, filename, relativeFilePath string, downloadExtractorFunc func(downloadTo, downloadPath string) error, logger Log) error {
    76  	downloadPath := path.Join(relativeFilePath, filename)
    77  	return downloadExtractorIfNeeded(downloadTo, filename, downloadPath, downloadExtractorFunc, logger)
    78  }
    79  
    80  func writeProps(propertiesFile *os.File, maps ...map[string]string) (err error) {
    81  	for _, props := range maps {
    82  		for key, value := range props {
    83  			if value != "" {
    84  				if _, err = propertiesFile.WriteString(key + "=" + value + "\n"); err != nil {
    85  					return err
    86  				}
    87  			}
    88  		}
    89  	}
    90  	return nil
    91  }