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

     1  package commands
     2  
     3  import (
     4  	"github.com/jfrog/jfrog-cli-go/artifactory/utils"
     5  	"github.com/jfrog/jfrog-cli-go/utils/cliutils"
     6  	"github.com/jfrog/jfrog-cli-go/utils/config"
     7  	"github.com/jfrog/jfrog-client-go/artifactory/usage"
     8  	clientutils "github.com/jfrog/jfrog-client-go/utils"
     9  	"github.com/jfrog/jfrog-client-go/utils/log"
    10  )
    11  
    12  type Command interface {
    13  	// Runs the command
    14  	Run() error
    15  	// Returns the Artifactory details. The usage report is sent to this Artifactory server.
    16  	RtDetails() (*config.ArtifactoryDetails, error)
    17  	// The command name for the usage report.
    18  	CommandName() string
    19  }
    20  
    21  func Exec(command Command) error {
    22  	channel := make(chan bool)
    23  	// Triggers the report usage.
    24  	go reportUsage(command, channel)
    25  	// Invoke the command interface
    26  	err := command.Run()
    27  	// Waits for the signal from the report usage to be done.
    28  	<-channel
    29  	return err
    30  }
    31  
    32  func reportUsage(command Command, channel chan<- bool) {
    33  	defer signalReportUsageFinished(channel)
    34  	reportUsage, err := clientutils.GetBoolEnvValue(cliutils.ReportUsage, true)
    35  	if err != nil {
    36  		log.Debug(err)
    37  		return
    38  	}
    39  	if reportUsage {
    40  		rtDetails, err := command.RtDetails()
    41  		if err != nil {
    42  			log.Debug(err)
    43  			return
    44  		}
    45  		if rtDetails != nil {
    46  			log.Debug("Sending usage info...")
    47  			serviceManager, err := utils.CreateServiceManager(rtDetails, false)
    48  			if err != nil {
    49  				log.Debug(err)
    50  				return
    51  			}
    52  			err = usage.SendReportUsage(cliutils.GetUserAgent(), command.CommandName(), serviceManager)
    53  			if err != nil {
    54  				log.Debug(err)
    55  				return
    56  			}
    57  		}
    58  	} else {
    59  		log.Debug("Usage info is disabled.")
    60  	}
    61  }
    62  
    63  // Set to true when the report usage func exits
    64  func signalReportUsageFinished(ch chan<- bool) {
    65  	ch <- true
    66  }