github.com/jfrog/jfrog-cli-go@v1.22.1-0.20200318093948-4826ef344ffd/artifactory/commands/buildinfo/collectenv.go (about)

     1  package buildinfo
     2  
     3  import (
     4  	"github.com/jfrog/jfrog-cli-go/artifactory/utils"
     5  	"github.com/jfrog/jfrog-cli-go/utils/config"
     6  	"github.com/jfrog/jfrog-client-go/artifactory/buildinfo"
     7  	"github.com/jfrog/jfrog-client-go/utils/log"
     8  	"os"
     9  	"strings"
    10  )
    11  
    12  type BuildCollectEnvCommand struct {
    13  	buildConfiguration *utils.BuildConfiguration
    14  }
    15  
    16  func NewBuildCollectEnvCommand() *BuildCollectEnvCommand {
    17  	return &BuildCollectEnvCommand{}
    18  }
    19  
    20  func (bcec *BuildCollectEnvCommand) SetBuildConfiguration(buildConfiguration *utils.BuildConfiguration) *BuildCollectEnvCommand {
    21  	bcec.buildConfiguration = buildConfiguration
    22  	return bcec
    23  }
    24  
    25  func (bcec *BuildCollectEnvCommand) Run() error {
    26  	log.Info("Collecting environment variables...")
    27  	err := utils.SaveBuildGeneralDetails(bcec.buildConfiguration.BuildName, bcec.buildConfiguration.BuildNumber)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	populateFunc := func(partial *buildinfo.Partial) {
    32  		partial.Env = getEnvVariables()
    33  	}
    34  	err = utils.SavePartialBuildInfo(bcec.buildConfiguration.BuildName, bcec.buildConfiguration.BuildNumber, populateFunc)
    35  	if err != nil {
    36  		return err
    37  	}
    38  	log.Info("Collected environment variables for", bcec.buildConfiguration.BuildName+"/"+bcec.buildConfiguration.BuildNumber+".")
    39  	return nil
    40  }
    41  
    42  // Returns the default configured Artifactory server
    43  func (bcec *BuildCollectEnvCommand) RtDetails() (*config.ArtifactoryDetails, error) {
    44  	return config.GetDefaultArtifactoryConf()
    45  }
    46  
    47  func (bcec *BuildCollectEnvCommand) CommandName() string {
    48  	return "rt_build_collect_env"
    49  }
    50  
    51  func getEnvVariables() buildinfo.Env {
    52  	m := make(map[string]string)
    53  	for _, e := range os.Environ() {
    54  		pair := strings.Split(e, "=")
    55  		if len(pair[0]) != 0 {
    56  			m["buildInfo.env."+pair[0]] = pair[1]
    57  		}
    58  	}
    59  	return m
    60  }