/
ecr.go
59 lines (45 loc) 路 1.68 KB
/
ecr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package awscloud
import (
"bytes"
"fmt"
"github.com/Excoriate/dagger-python-ecs/internal/common"
"github.com/Excoriate/dagger-python-ecs/internal/errors"
"github.com/Excoriate/dagger-python-ecs/internal/tui"
"os/exec"
)
func GetImageURL(repository, tag string) string {
repoNormalised := common.NormaliseNoSpaces(repository)
tagNormalised := common.NormaliseNoSpaces(tag)
if tagNormalised == "" {
tagNormalised = "latest"
}
return fmt.Sprintf("%s:%s", repoNormalised, tagNormalised)
}
func GetECRPublishAddress(registry, repository string) string {
repoNormalised := common.NormaliseNoSpaces(repository)
registryNormalised := common.NormaliseNoSpaces(registry)
return fmt.Sprintf("%s/%s", registryNormalised, repoNormalised)
}
func AWSECRLogin(registry string, credentials AWSCredentials) error {
var out bytes.Buffer
uxLog := tui.NewTUIMessage()
uxLogPrefix := "ECR-LOGIN"
cmd := exec.Command("aws", "ecr", "get-login-password", "--region", credentials.Region)
cmd.Stdout = &out
uxLog.ShowInfo(uxLogPrefix, fmt.Sprintf("Getting ECR login password for %s", registry))
if err := cmd.Run(); err != nil {
errMsg := fmt.Sprintf("Failed to get ECR login password, with region %s", credentials.Region)
uxLog.ShowError(uxLogPrefix, errMsg, err)
return errors.NewInternalPipelineError(errMsg)
}
token := out.String()
out = bytes.Buffer{}
cmd = exec.Command("docker", "login", "--username", "AWS", "--password", token, registry)
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
errMsg := fmt.Sprintf("Failed to login to ECR, with region %s", credentials.Region)
uxLog.ShowError(uxLogPrefix, errMsg, err)
return errors.NewInternalPipelineError(errMsg)
}
return nil
}