<commit_before>package main

import (
	"fmt"
	"os"
	"sort"
	"text/tabwriter"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/docker/docker/pkg/units"
)

var cmdImages = &Command{
	Exec:        runImages,
	UsageLine:   "images [OPTIONS]",
	Description: "List images",
	Help:        "List images.",
}

// ScalewayImageInterface is an interface to multiple Scaleway items
type ScalewayImageInterface struct {
	CreationDate time.Time
	Identifier   string
	Name         string
	Tag          string
	VirtualSize  float64
	Public       bool
	Type         string
}

// ByCreationDate sorts images by CreationDate field
type ByCreationDate []ScalewayImageInterface

func (a ByCreationDate) Len() int           { return len(a) }
func (a ByCreationDate) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByCreationDate) Less(i, j int) bool { return a[j].CreationDate.Before(a[i].CreationDate) }

func init() {
	cmdImages.Flag.BoolVar(&imagesA, []string{"a", "-all"}, false, "Show all iamges")
	cmdImages.Flag.BoolVar(&imagesNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
	cmdImages.Flag.BoolVar(&imagesQ, []string{"q", "-quiet"}, false, "Only show numeric IDs")
	cmdImages.Flag.BoolVar(&imagesHelp, []string{"h", "-help"}, false, "Print usage")
}

// Flags
var imagesA bool       // -a flag
var imagesQ bool       // -q flag
var imagesNoTrunc bool // -no-trunc flag
var imagesHelp bool    // -h, --help flag

func runImages(cmd *Command, args []string) {
	if imagesHelp {
		cmd.PrintUsage()
	}
	if len(args) != 0 {
		cmd.PrintShortUsage()
	}

	var entries = []ScalewayImageInterface{}

	images, err := cmd.API.GetImages()
	if err != nil {
		log.Fatalf("unable to fetch images from the Scaleway API: %v", err)
	}
	for _, val := range *images {
		creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", val.CreationDate)
		if err != nil {
			log.Fatalf("unable to parse creation date from the Scaleway API: %v", err)
		}
		entries = append(entries, ScalewayImageInterface{
			Type:         "image",
			CreationDate: creationDate,
			Identifier:   val.Identifier,
			Name:         val.Name,
			Public:       val.Public,
			Tag:          "latest",
			VirtualSize:  float64(val.RootVolume.Size),
		})
	}

	if imagesA {
		snapshots, err := cmd.API.GetSnapshots()
		if err != nil {
			log.Fatalf("unable to fetch snapshots from the Scaleway API: %v", err)
		}
		for _, val := range *snapshots {
			creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", val.CreationDate)
			if err != nil {
				log.Fatalf("unable to parse creation date from the Scaleway API: %v", err)
			}
			entries = append(entries, ScalewayImageInterface{
				Type:         "snapshot",
				CreationDate: creationDate,
				Identifier:   val.Identifier,
				Name:         val.Name,
				Tag:          "<none>",
				VirtualSize:  float64(val.Size),
				Public:       false,
			})
		}

		bootscripts, err := cmd.API.GetBootscripts()
		if err != nil {
			log.Fatalf("unable to fetch bootscripts from the Scaleway API: %v", err)
		}
		for _, val := range *bootscripts {
			entries = append(entries, ScalewayImageInterface{
				Type:       "bootscript",
				Identifier: val.Identifier,
				Name:       val.Title,
				Tag:        "bootscript",
				Public:     false,
			})
		}
	}

	w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
	defer w.Flush()
	if !imagesQ {<commit_msg>Listing volumes in images -a (#49)<commit_after>package main

import (
	"fmt"
	"os"
	"sort"
	"text/tabwriter"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/docker/docker/pkg/units"
)

var cmdImages = &Command{
	Exec:        runImages,
	UsageLine:   "images [OPTIONS]",
	Description: "List images",
	Help:        "List images.",
}

// ScalewayImageInterface is an interface to multiple Scaleway items
type ScalewayImageInterface struct {
	CreationDate time.Time
	Identifier   string
	Name         string
	Tag          string
	VirtualSize  float64
	Public       bool
	Type         string
}

// ByCreationDate sorts images by CreationDate field
type ByCreationDate []ScalewayImageInterface

func (a ByCreationDate) Len() int           { return len(a) }
func (a ByCreationDate) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByCreationDate) Less(i, j int) bool { return a[j].CreationDate.Before(a[i].CreationDate) }

func init() {
	cmdImages.Flag.BoolVar(&imagesA, []string{"a", "-all"}, false, "Show all iamges")
	cmdImages.Flag.BoolVar(&imagesNoTrunc, []string{"-no-trunc"}, false, "Don't truncate output")
	cmdImages.Flag.BoolVar(&imagesQ, []string{"q", "-quiet"}, false, "Only show numeric IDs")
	cmdImages.Flag.BoolVar(&imagesHelp, []string{"h", "-help"}, false, "Print usage")
}

// Flags
var imagesA bool       // -a flag
var imagesQ bool       // -q flag
var imagesNoTrunc bool // -no-trunc flag
var imagesHelp bool    // -h, --help flag

func runImages(cmd *Command, args []string) {
	if imagesHelp {
		cmd.PrintUsage()
	}
	if len(args) != 0 {
		cmd.PrintShortUsage()
	}

	var entries = []ScalewayImageInterface{}

	images, err := cmd.API.GetImages()
	if err != nil {
		log.Fatalf("unable to fetch images from the Scaleway API: %v", err)
	}
	for _, val := range *images {
		creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", val.CreationDate)
		if err != nil {
			log.Fatalf("unable to parse creation date from the Scaleway API: %v", err)
		}
		entries = append(entries, ScalewayImageInterface{
			Type:         "image",
			CreationDate: creationDate,
			Identifier:   val.Identifier,
			Name:         val.Name,
			Public:       val.Public,
			Tag:          "latest",
			VirtualSize:  float64(val.RootVolume.Size),
		})
	}

	if imagesA {
		snapshots, err := cmd.API.GetSnapshots()
		if err != nil {
			log.Fatalf("unable to fetch snapshots from the Scaleway API: %v", err)
		}
		for _, val := range *snapshots {
			creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", val.CreationDate)
			if err != nil {
				log.Fatalf("unable to parse creation date from the Scaleway API: %v", err)
			}
			entries = append(entries, ScalewayImageInterface{
				Type:         "snapshot",
				CreationDate: creationDate,
				Identifier:   val.Identifier,
				Name:         val.Name,
				Tag:          "<none>",
				VirtualSize:  float64(val.Size),
				Public:       false,
			})
		}

		bootscripts, err := cmd.API.GetBootscripts()
		if err != nil {
			log.Fatalf("unable to fetch bootscripts from the Scaleway API: %v", err)
		}
		for _, val := range *bootscripts {
			entries = append(entries, ScalewayImageInterface{
				Type:       "bootscript",
				Identifier: val.Identifier,
				Name:       val.Title,
				Tag:        "bootscript",
				Public:     false,
			})
		}

		volumes, err := cmd.API.GetVolumes()
		if err != nil {
			log.Fatalf("unable to fetch volumes from the Scaleway API: %v", err)
		}
		for _, val := range *volumes {
			creationDate, err := time.Parse("2006-01-02T15:04:05.000000+00:00", val.CreationDate)
			if err != nil {
				log.Fatalf("unable to parse creation date from the Scaleway API: %v", err)
			}
			entries = append(entries, ScalewayImageInterface{
				Type:         "volume",
				CreationDate: creationDate,
				Identifier:   val.Identifier,
				Name:         val.Name,
				Tag:          "<none>",
				VirtualSize:  float64(val.Size),
				Public:       false,
			})
		}
	}

	w := tabwriter.NewWriter(os.Stdout, 20, 1, 3, ' ', 0)
	defer w.Flush()
	if !imagesQ {
{'fraction_non_alphanumeric': 0.13719142047753946, 'fraction_numerical': 0.019964926480507218, 'mean_word_length': 3.0535811919081466, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3124647', 'n_tokens_mistral': 2844, 'n_tokens_neox': 2508, 'n_words': 672}