github.com/jfrog/jfrog-cli-core/v2@v2.51.0/common/commands/command.go (about)

     1  package commands
     2  
     3  import (
     4  	"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
     5  	"github.com/jfrog/jfrog-cli-core/v2/utils/config"
     6  	"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
     7  	usageReporter "github.com/jfrog/jfrog-cli-core/v2/utils/usage"
     8  	"github.com/jfrog/jfrog-client-go/artifactory/usage"
     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 Server details. The usage report is sent to this server.
    16  	ServerDetails() (*config.ServerDetails, 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 := usageReporter.ShouldReportUsage()
    35  	if reportUsage {
    36  		serverDetails, err := command.ServerDetails()
    37  		if err != nil {
    38  			log.Debug(usageReporter.ReportUsagePrefix, err.Error())
    39  			return
    40  		}
    41  		if serverDetails != nil && serverDetails.ArtifactoryUrl != "" {
    42  			log.Debug(usageReporter.ReportUsagePrefix, "Sending info...")
    43  			serviceManager, err := utils.CreateServiceManager(serverDetails, -1, 0, false)
    44  			if err != nil {
    45  				log.Debug(usageReporter.ReportUsagePrefix, err.Error())
    46  				return
    47  			}
    48  			err = usage.SendReportUsage(coreutils.GetCliUserAgent(), command.CommandName(), serviceManager)
    49  			if err != nil {
    50  				log.Debug(err.Error())
    51  				return
    52  			}
    53  		}
    54  	} else {
    55  		log.Debug("Usage info is disabled.")
    56  	}
    57  }
    58  
    59  // Set to true when the report usage func exits
    60  func signalReportUsageFinished(ch chan<- bool) {
    61  	ch <- true
    62  }