github.com/awslabs/fargatecli@v0.3.2-0.20210502061925-aa1c09cd404e/docker/main.go (about)

     1  package docker
     2  
     3  import (
     4  	"os"
     5  	"os/exec"
     6  	"time"
     7  
     8  	"github.com/awslabs/fargatecli/console"
     9  )
    10  
    11  const timestampFormat = "20060102150405"
    12  
    13  func GenerateTag() string {
    14  	return time.Now().UTC().Format(timestampFormat)
    15  }
    16  
    17  type Repository struct {
    18  	Uri string
    19  }
    20  
    21  func NewRepository(repositoryUri string) Repository {
    22  	return Repository{
    23  		Uri: repositoryUri,
    24  	}
    25  }
    26  
    27  func (repository *Repository) Login(username, password string) {
    28  	console.Debug("Logging into Docker repository [%s]", repository.Uri)
    29  	console.Shell("docker login --username %s --password ******* %s", username, repository.Uri)
    30  
    31  	cmd := exec.Command("docker", "login", "--username", username, "--password", password, repository.Uri)
    32  
    33  	if console.Verbose {
    34  		cmd.Stdout = os.Stdout
    35  		cmd.Stderr = os.Stderr
    36  	}
    37  
    38  	if err := cmd.Start(); err != nil {
    39  		console.ErrorExit(err, "Couldn't login to Docker repository [%s]", repository.Uri)
    40  	}
    41  
    42  	if err := cmd.Wait(); err != nil {
    43  		console.IssueExit("Couldn't login to Docker repository [%s]", repository.Uri)
    44  	}
    45  }
    46  
    47  func (repository *Repository) Build(tag string) {
    48  	console.Debug("Building Docker image [%s]", repository.UriFor(tag))
    49  	console.Shell("docker build --rm=false --tag %s .", repository.UriFor(tag))
    50  
    51  	cmd := exec.Command("docker", "build", "--tag", repository.Uri+":"+tag, ".")
    52  
    53  	cmd.Stdout = os.Stdout
    54  	cmd.Stderr = os.Stderr
    55  
    56  	if err := cmd.Start(); err != nil {
    57  		console.ErrorExit(err, "Couldn't build Docker image [%s]", repository.UriFor(tag))
    58  	}
    59  
    60  	if err := cmd.Wait(); err != nil {
    61  		console.IssueExit("Couldn't build Docker image [%s]", repository.Uri)
    62  	}
    63  }
    64  
    65  func (repository *Repository) Push(tag string) {
    66  	console.Debug("Pushing Docker image [%s]", repository.UriFor(tag))
    67  	console.Shell("docker push %s .", repository.UriFor(tag))
    68  
    69  	cmd := exec.Command("docker", "push", repository.UriFor(tag))
    70  
    71  	cmd.Stdout = os.Stdout
    72  	cmd.Stderr = os.Stderr
    73  
    74  	if err := cmd.Start(); err != nil {
    75  		console.ErrorExit(err, "Couldn't push Docker image [%s]", repository.UriFor(tag))
    76  	}
    77  
    78  	if err := cmd.Wait(); err != nil {
    79  		console.IssueExit("Couldn't push Docker image [%s]", repository.UriFor(tag))
    80  	}
    81  }
    82  
    83  func (repository *Repository) UriFor(tag string) string {
    84  	return repository.Uri + ":" + tag
    85  }