mirror of
https://github.com/clearlinux/docker.git
synced 2026-09-06 13:41:36 +00:00
Port 'docker images' to the engine API
Docker-DCO-1.1-Signed-off-by: Solomon Hykes <solomon@docker.com> (github: shykes)
This commit is contained in:
committed by
Victor Vieux
parent
6652416f4c
commit
17a806c8a0
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -232,3 +233,76 @@ func (env *Env) Map() map[string]string {
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
type Table struct {
|
||||
Data []*Env
|
||||
sortKey string
|
||||
}
|
||||
|
||||
func NewTable(sortKey string, sizeHint int) *Table {
|
||||
return &Table{
|
||||
make([]*Env, 0, sizeHint),
|
||||
sortKey,
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Table) Add(env *Env) {
|
||||
t.Data = append(t.Data, env)
|
||||
}
|
||||
|
||||
func (t *Table) Len() int {
|
||||
return len(t.Data)
|
||||
}
|
||||
|
||||
func (t *Table) Less(a, b int) bool {
|
||||
return t.lessBy(a, b, t.sortKey)
|
||||
}
|
||||
|
||||
func (t *Table) lessBy(a, b int, by string) bool {
|
||||
keyA := t.Data[a].Get(by)
|
||||
keyB := t.Data[b].Get(by)
|
||||
intA, errA := strconv.ParseInt(keyA, 10, 64)
|
||||
intB, errB := strconv.ParseInt(keyB, 10, 64)
|
||||
if errA == nil && errB == nil {
|
||||
return intA < intB
|
||||
}
|
||||
return keyA < keyB
|
||||
}
|
||||
|
||||
func (t *Table) Swap(a, b int) {
|
||||
tmp := t.Data[a]
|
||||
t.Data[a] = t.Data[b]
|
||||
t.Data[b] = tmp
|
||||
}
|
||||
|
||||
func (t *Table) Sort() {
|
||||
sort.Sort(t)
|
||||
}
|
||||
|
||||
func (t *Table) WriteTo(dst io.Writer) (n int64, err error) {
|
||||
for _, env := range t.Data {
|
||||
bytes, err := env.WriteTo(dst)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
if _, err := dst.Write([]byte{'\n'}); err != nil {
|
||||
return -1, err
|
||||
}
|
||||
n += bytes + 1
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (t *Table) ReadFrom(src io.Reader) (n int64, err error) {
|
||||
decoder := NewDecoder(src)
|
||||
for {
|
||||
env, err := decoder.Decode()
|
||||
if err == io.EOF {
|
||||
return 0, nil
|
||||
} else if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
t.Add(env)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
@@ -190,3 +190,19 @@ func (o *Output) AddEnv() (dst *Env, err error) {
|
||||
}()
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
func (o *Output) AddTable() (dst *Table, err error) {
|
||||
src, err := o.AddPipe()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dst = NewTable("", 0)
|
||||
o.tasks.Add(1)
|
||||
go func() {
|
||||
defer o.tasks.Done()
|
||||
if _, err := dst.ReadFrom(src); err != nil {
|
||||
return
|
||||
}
|
||||
}()
|
||||
return dst, nil
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package engine
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
)
|
||||
|
||||
func TestTableWriteTo(t *testing.T) {
|
||||
table := NewTable("", 0)
|
||||
e := &Env{}
|
||||
e.Set("foo", "bar")
|
||||
table.Add(e)
|
||||
var buf bytes.Buffer
|
||||
if _, err := table.WriteTo(&buf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
output := make(map[string]string)
|
||||
if err := json.Unmarshal(buf.Bytes(), &output); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(output) != 1 {
|
||||
t.Fatalf("Incorrect output: %v", output)
|
||||
}
|
||||
if val, exists := output["foo"]; !exists || val != "bar" {
|
||||
t.Fatalf("Inccorect output: %v", output)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user