github.com/eikeon/docker@v1.5.0-rc4/graph/list.go (about)

     1  package graph
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"path"
     7  	"strings"
     8  
     9  	"github.com/docker/docker/engine"
    10  	"github.com/docker/docker/image"
    11  	"github.com/docker/docker/pkg/parsers/filters"
    12  )
    13  
    14  var acceptedImageFilterTags = map[string]struct{}{"dangling": {}}
    15  
    16  func (s *TagStore) CmdImages(job *engine.Job) engine.Status {
    17  	var (
    18  		allImages   map[string]*image.Image
    19  		err         error
    20  		filt_tagged = true
    21  	)
    22  
    23  	imageFilters, err := filters.FromParam(job.Getenv("filters"))
    24  	if err != nil {
    25  		return job.Error(err)
    26  	}
    27  	for name := range imageFilters {
    28  		if _, ok := acceptedImageFilterTags[name]; !ok {
    29  			return job.Errorf("Invalid filter '%s'", name)
    30  		}
    31  	}
    32  
    33  	if i, ok := imageFilters["dangling"]; ok {
    34  		for _, value := range i {
    35  			if strings.ToLower(value) == "true" {
    36  				filt_tagged = false
    37  			}
    38  		}
    39  	}
    40  
    41  	if job.GetenvBool("all") && filt_tagged {
    42  		allImages, err = s.graph.Map()
    43  	} else {
    44  		allImages, err = s.graph.Heads()
    45  	}
    46  	if err != nil {
    47  		return job.Error(err)
    48  	}
    49  	lookup := make(map[string]*engine.Env)
    50  	s.Lock()
    51  	for name, repository := range s.Repositories {
    52  		if job.Getenv("filter") != "" {
    53  			if match, _ := path.Match(job.Getenv("filter"), name); !match {
    54  				continue
    55  			}
    56  		}
    57  		for tag, id := range repository {
    58  			image, err := s.graph.Get(id)
    59  			if err != nil {
    60  				log.Printf("Warning: couldn't load %s from %s/%s: %s", id, name, tag, err)
    61  				continue
    62  			}
    63  
    64  			if out, exists := lookup[id]; exists {
    65  				if filt_tagged {
    66  					out.SetList("RepoTags", append(out.GetList("RepoTags"), fmt.Sprintf("%s:%s", name, tag)))
    67  				}
    68  			} else {
    69  				// get the boolean list for if only the untagged images are requested
    70  				delete(allImages, id)
    71  				if filt_tagged {
    72  					out := &engine.Env{}
    73  					out.SetJson("ParentId", image.Parent)
    74  					out.SetList("RepoTags", []string{fmt.Sprintf("%s:%s", name, tag)})
    75  					out.SetJson("Id", image.ID)
    76  					out.SetInt64("Created", image.Created.Unix())
    77  					out.SetInt64("Size", image.Size)
    78  					out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size)
    79  					lookup[id] = out
    80  				}
    81  			}
    82  
    83  		}
    84  	}
    85  	s.Unlock()
    86  
    87  	outs := engine.NewTable("Created", len(lookup))
    88  	for _, value := range lookup {
    89  		outs.Add(value)
    90  	}
    91  
    92  	// Display images which aren't part of a repository/tag
    93  	if job.Getenv("filter") == "" {
    94  		for _, image := range allImages {
    95  			out := &engine.Env{}
    96  			out.SetJson("ParentId", image.Parent)
    97  			out.SetList("RepoTags", []string{"<none>:<none>"})
    98  			out.SetJson("Id", image.ID)
    99  			out.SetInt64("Created", image.Created.Unix())
   100  			out.SetInt64("Size", image.Size)
   101  			out.SetInt64("VirtualSize", image.GetParentsSize(0)+image.Size)
   102  			outs.Add(out)
   103  		}
   104  	}
   105  
   106  	outs.ReverseSort()
   107  	if _, err := outs.WriteListTo(job.Stdout); err != nil {
   108  		return job.Error(err)
   109  	}
   110  	return engine.StatusOK
   111  }