Esempio n. 1
0
// Print prints the images to standard output.
func (i Images) Print(mode utils.OutputMode) error {
	if len(i) == 0 {
		return errors.New("no images found")
	}

	switch mode {
	case utils.JSON:
		out, err := i.outputJSON()
		if err != nil {
			return err
		}

		fmt.Println(out)
		return nil
	case utils.Simplified:

		green := color.New(color.FgGreen).SprintfFunc()
		output := ansicolor.NewAnsiColorWriter(os.Stdout)
		w := utils.NewImagesTabWriter(output)
		defer w.Flush()

		for region, images := range i {
			if len(images) == 0 {
				continue
			}

			fmt.Fprintln(w, green("AWS Region: %s (%d images):", region, len(images)))
			fmt.Fprintln(w, "    Name\tID\tState\tTags")

			for ix, image := range images {
				tags := make([]string, len(image.Tags))
				for i, tag := range image.Tags {
					tags[i] = *tag.Key + ":" + *tag.Value
				}

				name := ""
				if image.Name != nil {
					name = *image.Name
				}

				state := *image.State
				if *image.State == "failed" {
					state += " (" + *image.StateReason.Message + ")"
				}

				fmt.Fprintf(w, "[%d] %s\t%s\t%s\t%+v\n",
					ix+1, name, *image.ImageID, state, tags)
			}

			fmt.Fprintln(w, "")
		}
		return nil
	default:
		return fmt.Errorf("output mode '%s' is not valid", mode)
	}
}
Esempio n. 2
0
// Print prints the stored images to standard output.
func (i Images) Print(mode utils.OutputMode) error {
	if len(i.Items) == 0 {
		return errors.New("no images found")
	}

	switch mode {
	case utils.JSON:
		out, err := i.outputJSON()
		if err != nil {
			return err
		}

		fmt.Println(out)
		return nil
	case utils.Simplified:
		green := color.New(color.FgGreen).SprintfFunc()
		output := ansicolor.NewAnsiColorWriter(os.Stdout)
		w := utils.NewImagesTabWriter(output)
		defer w.Flush()

		imageDesc := "image"
		if len(i.Items) > 1 {
			imageDesc = "images"
		}

		fmt.Fprintln(w, green("GCE (%d %s):", len(i.Items), imageDesc))
		fmt.Fprintln(w, "    Name\tID\tStatus\tType\tDeprecated\tCreation Timestamp")

		for ix, image := range i.Items {
			deprecatedState := ""
			if image.Deprecated != nil {
				deprecatedState = image.Deprecated.State
			}

			fmt.Fprintf(w, "[%d] %s (%s)\t%d\t%s\t%s (%d)\t%s\t%s\n",
				ix+1, image.Name, image.Description, image.Id,
				image.Status, image.SourceType, image.DiskSizeGb,
				deprecatedState, image.CreationTimestamp,
			)
		}

		return nil
	default:
		return fmt.Errorf("output mode '%s' is not valid", mode)
	}
}
Esempio n. 3
0
// Print prints the stored images to standard output.
func (i Images) Print(mode utils.OutputMode) error {
	if len(i) == 0 {
		return errors.New("no images found")
	}

	switch mode {
	case utils.JSON:
		out, err := i.outputJSON()
		if err != nil {
			return err
		}

		fmt.Println(out)
		return nil
	case utils.Simplified:
		green := color.New(color.FgGreen).SprintfFunc()
		output := ansicolor.NewAnsiColorWriter(os.Stdout)
		w := utils.NewImagesTabWriter(output)
		defer w.Flush()

		imageDesc := "image"
		if len(i) > 1 {
			imageDesc = "images"
		}

		fmt.Fprintln(w, green("DO (%d %s):", len(i), imageDesc))
		fmt.Fprintln(w, "    Name\tID\tDistribution\tType\tRegions")

		for ix, image := range i {
			regions := make([]string, len(image.Regions))
			for i, region := range image.Regions {
				regions[i] = region
			}

			fmt.Fprintf(w, "[%d] %s\t%d\t%s\t%s (%d)\t%+v\n",
				ix+1, image.Name, image.ID, image.Distribution, image.Type, image.MinDiskSize, regions)
		}
		return nil
	default:
		return fmt.Errorf("output mode '%s' is not valid", mode)
	}

}