emoji-talk/docker-ex/main.go

91 lines
1.6 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"os"
"os/exec"
"strings"
"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 = emoji.FromHexString(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()
}
type ImageThing struct {
Repo string
ID string
Digest string
Tag string
CreateSince string
CreateAt string
Size string
}