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

     1  package docker
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/jfrog/jfrog-cli-go/artifactory/utils"
     7  	"github.com/jfrog/jfrog-cli-go/artifactory/utils/docker"
     8  	"github.com/jfrog/jfrog-cli-go/utils/config"
     9  	"github.com/jfrog/jfrog-client-go/utils/errorutils"
    10  )
    11  
    12  type DockerPullCommand struct {
    13  	DockerCommand
    14  }
    15  
    16  func NewDockerPullCommand() *DockerPullCommand {
    17  	return &DockerPullCommand{}
    18  }
    19  
    20  // Pull docker image and create build info if needed
    21  func (dpc *DockerPullCommand) Run() error {
    22  	err := docker.ValidateClientApiVersion()
    23  	if err != nil {
    24  		return err
    25  	}
    26  
    27  	// Perform login
    28  	rtDetails, err := dpc.RtDetails()
    29  	if errorutils.CheckError(err) != nil {
    30  		return err
    31  	}
    32  
    33  	if !dpc.skipLogin {
    34  		loginConfig := &docker.DockerLoginConfig{ArtifactoryDetails: rtDetails}
    35  		err = docker.DockerLogin(dpc.imageTag, loginConfig)
    36  		if err != nil {
    37  			return err
    38  		}
    39  	}
    40  
    41  	// Perform pull
    42  	if strings.LastIndex(dpc.imageTag, ":") == -1 {
    43  		dpc.imageTag = dpc.imageTag + ":latest"
    44  	}
    45  	image := docker.New(dpc.imageTag)
    46  	err = image.Pull()
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	buildName := dpc.BuildConfiguration().BuildName
    52  	buildNumber := dpc.BuildConfiguration().BuildNumber
    53  	// Return if no build name and number was provided
    54  	if buildName == "" || buildNumber == "" {
    55  		return nil
    56  	}
    57  
    58  	if err := utils.SaveBuildGeneralDetails(buildName, buildNumber); err != nil {
    59  		return err
    60  	}
    61  
    62  	serviceManager, err := docker.CreateServiceManager(rtDetails, 0)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	builder := docker.BuildInfoBuilder(image, dpc.Repo(), buildName, buildNumber, serviceManager, docker.Pull)
    68  	buildInfo, err := builder.Build(dpc.BuildConfiguration().Module)
    69  	if err != nil {
    70  		return err
    71  	}
    72  	return utils.SaveBuildInfo(buildName, buildNumber, buildInfo)
    73  }
    74  
    75  func (dpc *DockerPullCommand) CommandName() string {
    76  	return "rt_docker_pull"
    77  }
    78  
    79  func (dpc *DockerPullCommand) RtDetails() (*config.ArtifactoryDetails, error) {
    80  	return dpc.rtDetails, nil
    81  }