github.com/osievert/jfrog-cli-core@v1.2.7/artifactory/commands/container/push.go (about)

     1  package container
     2  
     3  import (
     4  	"github.com/jfrog/jfrog-cli-core/artifactory/utils"
     5  	"github.com/jfrog/jfrog-cli-core/artifactory/utils/container"
     6  	"github.com/jfrog/jfrog-cli-core/utils/config"
     7  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
     8  )
     9  
    10  type PushCommand struct {
    11  	ContainerManagerCommand
    12  	threads              int
    13  	containerManagerType container.ContainerManagerType
    14  }
    15  
    16  func NewPushCommand(containerManager container.ContainerManagerType) *PushCommand {
    17  	return &PushCommand{containerManagerType: containerManager}
    18  }
    19  
    20  func (pc *PushCommand) Threads() int {
    21  	return pc.threads
    22  }
    23  
    24  func (pc *PushCommand) SetThreads(threads int) *PushCommand {
    25  	pc.threads = threads
    26  	return pc
    27  }
    28  
    29  // Push image and create build info if needed
    30  func (pc *PushCommand) Run() error {
    31  	if pc.containerManagerType == container.DockerClient {
    32  		err := container.ValidateClientApiVersion()
    33  		if err != nil {
    34  			return err
    35  		}
    36  	}
    37  	rtDetails, err := pc.RtDetails()
    38  	if errorutils.CheckError(err) != nil {
    39  		return err
    40  	}
    41  	// Perform login
    42  	if err := pc.PerformLogin(rtDetails, pc.containerManagerType); err != nil {
    43  		return err
    44  	}
    45  	// Perform push.
    46  	cm := container.NewManager(pc.containerManagerType)
    47  	image := container.NewImage(pc.imageTag)
    48  	err = cm.Push(image)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	// Return if no build name and number was provided
    53  	if pc.buildConfiguration.BuildName == "" || pc.buildConfiguration.BuildNumber == "" {
    54  		return nil
    55  	}
    56  	if err := utils.SaveBuildGeneralDetails(pc.buildConfiguration.BuildName, pc.buildConfiguration.BuildNumber); err != nil {
    57  		return err
    58  	}
    59  	serviceManager, err := utils.CreateServiceManagerWithThreads(rtDetails, false, pc.threads)
    60  	if err != nil {
    61  		return err
    62  	}
    63  	builder, err := container.NewBuildInfoBuilder(image, pc.Repo(), pc.BuildConfiguration().BuildName, pc.BuildConfiguration().BuildNumber, serviceManager, container.Push, cm)
    64  	if err != nil {
    65  		return err
    66  	}
    67  	buildInfo, err := builder.Build(pc.BuildConfiguration().Module)
    68  	if err != nil {
    69  		return err
    70  	}
    71  	return utils.SaveBuildInfo(pc.BuildConfiguration().BuildName, pc.BuildConfiguration().BuildNumber, buildInfo)
    72  }
    73  
    74  func (pc *PushCommand) CommandName() string {
    75  	return "rt_docker_push"
    76  }
    77  
    78  func (pc *PushCommand) RtDetails() (*config.ArtifactoryDetails, error) {
    79  	return pc.rtDetails, nil
    80  }