docker-ex: something like docker images with emoji digest

Signed-off-by: Vincent Batts <vbatts@hashbangbash.com>
This commit is contained in:
Vincent Batts 2018-10-18 14:45:50 -04:00
commit 1ea1a5749e
Signed by: vbatts
GPG key ID: 10937E57733F1362

110
docker-ex/main.go Normal file
View file

@ -0,0 +1,110 @@
package main
import (
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io"
"os"
"os/exec"
"strings"
esum "github.com/emojisum/emojisum/emoji"
)
var (
flJson = flag.Bool("json", false, "output json (no emoji)")
formatOutput = `
{
"repo": "{{.Repository}}",
"id": "{{.ID}}",
"digest": "{{.Digest}}",
"tag": "{{.Tag}}",
"createdSince": "{{.CreatedSince}}",
"createdAt": "{{.CreatedAt}}",
"size": "{{.Size}}"
}
`
)
func main() {
flag.Parse()
args := []string{"images", "--digests", fmt.Sprintf("--format=%s", formatOutput)}
args = append(args, flag.Args()...)
cmd := exec.Command("docker", args...)
if !*flJson {
pR, pW := io.Pipe()
cmd.Stdout = pW
jR := json.NewDecoder(pR)
go func() {
for {
it := ImageThing{}
err := jR.Decode(&it)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
var digest, d, h string
var name string
if it.Tag == "" {
name = it.Repo
} else {
name = fmt.Sprintf("%s:%s", it.Repo, it.Tag)
}
if strings.Contains(it.Digest, ":") {
chunks := strings.SplitN(it.Digest, ":", 2)
h = chunks[0]
d, err = emojiFromHexString(chunks[1])
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
digest = fmt.Sprintf("%s:%s", h, d)
} else {
digest = "<none>"
}
fmt.Printf("%s (id %s) %s\n", name, it.ID, digest)
}
}()
} else {
cmd.Stdout = os.Stdout
}
cmd.Stdin = os.Stdin
cmd.Stderr = os.Stderr
cmd.Run()
}
func emojiFromHexString(s string) (string, error) {
d, err := hex.DecodeString(s)
if err != nil {
return "", err
}
var ret string
for _, b := range d {
for _, e := range esum.Map(b) {
// use the first colon notation word and continue
if esum.IsCodepoint(e) {
ret = ret + esum.CodepointToUnicode(e)
break
}
}
}
return ret, nil
}
type ImageThing struct {
Repo string
ID string
Digest string
Tag string
CreateSince string
CreateAt string
Size string
}