2022-01-13 13:14:53 +01:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2022-02-23 12:55:46 +01:00
|
|
|
"path"
|
|
|
|
"strings"
|
2022-01-13 13:14:53 +01:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/drone/drone-go/drone"
|
|
|
|
|
|
|
|
"github.com/inhies/go-bytesize"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p Plugin) writeCard() error {
|
|
|
|
cmd := exec.Command("docker", "inspect", p.Build.Name)
|
|
|
|
data, err := cmd.CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-11 11:49:42 +01:00
|
|
|
out := Card{}
|
2022-01-13 13:14:53 +01:00
|
|
|
if err := json.Unmarshal(data, &out); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
inspect := out[0]
|
|
|
|
inspect.SizeString = fmt.Sprint(bytesize.New(float64(inspect.Size)))
|
|
|
|
inspect.VirtualSizeString = fmt.Sprint(bytesize.New(float64(inspect.VirtualSize)))
|
|
|
|
inspect.Time = fmt.Sprint(inspect.Metadata.LastTagTime.Format(time.RFC3339))
|
2022-02-11 11:49:42 +01:00
|
|
|
// change slice of tags to slice of TagStruct
|
|
|
|
var sliceTagStruct []TagStruct
|
|
|
|
for _, tag := range inspect.RepoTags {
|
|
|
|
sliceTagStruct = append(sliceTagStruct, TagStruct{Tag: tag})
|
|
|
|
}
|
2022-02-23 12:55:46 +01:00
|
|
|
inspect.ParsedRepoTags = sliceTagStruct[1:] // remove the first tag which is always "hash:latest"
|
|
|
|
// create the url from repo and registry
|
|
|
|
inspect.URL = mapRegistryToURL(p.Daemon.Registry, p.Build.Repo)
|
2022-01-13 13:14:53 +01:00
|
|
|
cardData, _ := json.Marshal(inspect)
|
|
|
|
|
|
|
|
card := drone.CardInput{
|
|
|
|
Schema: "https://drone-plugins.github.io/drone-docker/card.json",
|
|
|
|
Data: cardData,
|
|
|
|
}
|
|
|
|
|
|
|
|
writeCard(p.CardPath, &card)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeCard(path string, card interface{}) {
|
|
|
|
data, _ := json.Marshal(card)
|
|
|
|
switch {
|
|
|
|
case path == "/dev/stdout":
|
|
|
|
writeCardTo(os.Stdout, data)
|
|
|
|
case path == "/dev/stderr":
|
|
|
|
writeCardTo(os.Stderr, data)
|
|
|
|
case path != "":
|
|
|
|
ioutil.WriteFile(path, data, 0644)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeCardTo(out io.Writer, data []byte) {
|
|
|
|
encoded := base64.StdEncoding.EncodeToString(data)
|
|
|
|
io.WriteString(out, "\u001B]1338;")
|
|
|
|
io.WriteString(out, encoded)
|
|
|
|
io.WriteString(out, "\u001B]0m")
|
|
|
|
io.WriteString(out, "\n")
|
|
|
|
}
|
2022-02-23 12:55:46 +01:00
|
|
|
|
|
|
|
func mapRegistryToURL(registry, repo string) (url string) {
|
|
|
|
url = "https://"
|
|
|
|
var domain string
|
|
|
|
if strings.Contains(registry, "amazonaws.com") {
|
|
|
|
domain = "gallery.ecr.aws/"
|
|
|
|
} else if strings.Contains(registry, "gcr.io") {
|
|
|
|
domain = "console.cloud.google.com/gcr/images"
|
|
|
|
} else {
|
|
|
|
// default to docker hub
|
|
|
|
domain = "hub.docker.com/r/"
|
|
|
|
}
|
|
|
|
url = path.Join(url, domain, repo)
|
|
|
|
return url
|
|
|
|
}
|