image

package
v1.61.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 23, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ChangeProtectionCmds = base.ChangeProtectionCmds[*hcloud.Image, hcloud.ImageChangeProtectionOpts]{
	ResourceNameSingular:    "Image",
	ShortEnableDescription:  "Enable resource protection for an Image",
	ShortDisableDescription: "Disable resource protection for an Image",

	NameSuggestions: func(client hcapi2.Client) func() []string {
		return client.Image().Names
	},

	ProtectionLevels: map[string]func(opts *hcloud.ImageChangeProtectionOpts, value bool){
		"delete": func(opts *hcloud.ImageChangeProtectionOpts, value bool) {
			opts.Delete = &value
		},
	},

	Fetch: func(_ state.State, _ *cobra.Command, idStr string) (*hcloud.Image, *hcloud.Response, error) {
		imageID, err := strconv.ParseInt(idStr, 10, 64)
		if err != nil {
			return nil, nil, errors.New("invalid Image ID")
		}
		return &hcloud.Image{ID: imageID}, nil, nil
	},

	ChangeProtectionFunction: func(s state.State, image *hcloud.Image, opts hcloud.ImageChangeProtectionOpts) (*hcloud.Action, *hcloud.Response, error) {
		return s.Client().Image().ChangeProtection(s, image, opts)
	},

	IDOrName: func(image *hcloud.Image) string {
		return fmt.Sprint(image.ID)
	},
}
View Source
var DeleteCmd = base.DeleteCmd[*hcloud.Image]{
	ResourceNameSingular: "Image",
	ResourceNamePlural:   "Images",
	ShortDescription:     "Delete an Image",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.Image().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.Image, *hcloud.Response, error) {
		id, err := strconv.ParseInt(idOrName, 10, 64)
		if err != nil {
			return nil, nil, fmt.Errorf("invalid snapshot or backup ID %q", idOrName)
		}
		return s.Client().Image().GetByID(s, id)
	},
	Delete: func(s state.State, _ *cobra.Command, image *hcloud.Image) (*hcloud.Action, error) {
		_, err := s.Client().Image().Delete(s, image)
		return nil, err
	},
}
View Source
var DescribeCmd = base.DescribeCmd[*hcloud.Image]{
	ResourceNameSingular: "Image",
	ShortDescription:     "Describe an Image",
	AdditionalFlags: func(cmd *cobra.Command) {
		cmd.Flags().StringP("architecture", "a", string(hcloud.ArchitectureX86), "architecture of the Image, default is x86")
		_ = cmd.RegisterFlagCompletionFunc("architecture", cmpl.SuggestCandidates(string(hcloud.ArchitectureX86), string(hcloud.ArchitectureARM)))
	},
	NameSuggestions: func(c hcapi2.Client) func() []string { return c.Image().Names },
	Fetch: func(s state.State, cmd *cobra.Command, idOrName string) (*hcloud.Image, any, error) {
		_, err := strconv.ParseInt(idOrName, 10, 64)
		isID := err == nil

		arch, err := cmd.Flags().GetString("architecture")
		if err != nil {
			return nil, nil, err
		}

		if !isID && !cmd.Flags().Changed("architecture") {
			fmt.Fprintln(os.Stderr, "INFO: This command only returns x86 images by default. Explicitly set the --architecture=x86|arm flag to hide this message.")
		}

		img, _, err := s.Client().Image().GetForArchitecture(s, idOrName, hcloud.Architecture(arch))
		if err != nil {
			return nil, nil, err
		}
		return img, hcloud.SchemaFromImage(img), nil
	},
	PrintText: func(_ state.State, _ *cobra.Command, out io.Writer, image *hcloud.Image) error {
		fmt.Fprint(out, DescribeImage(image))
		return nil
	},
}
View Source
var LabelCmds = base.LabelCmds[*hcloud.Image]{
	ResourceNameSingular:   "Image",
	ShortDescriptionAdd:    "Add a label to an Image",
	ShortDescriptionRemove: "Remove a label from an Image",
	NameSuggestions:        func(c hcapi2.Client) func() []string { return c.Image().Names },
	LabelKeySuggestions:    func(c hcapi2.Client) func(idOrName string) []string { return c.Image().LabelKeys },
	Fetch: func(s state.State, idOrName string) (*hcloud.Image, error) {
		id, err := strconv.ParseInt(idOrName, 10, 64)
		if err != nil {
			return nil, fmt.Errorf("invalid snapshot or backup ID %q", idOrName)
		}
		image, _, err := s.Client().Image().GetByID(s, id)
		if err != nil {
			return nil, err
		}
		if image == nil {
			return nil, fmt.Errorf("Image not found: %s", idOrName)
		}
		return image, nil
	},
	SetLabels: func(s state.State, image *hcloud.Image, labels map[string]string) error {
		opts := hcloud.ImageUpdateOpts{
			Labels: labels,
		}
		_, _, err := s.Client().Image().Update(s, image, opts)
		return err
	},
	GetLabels: func(image *hcloud.Image) map[string]string {
		return image.Labels
	},
	GetIDOrName: func(image *hcloud.Image) string {
		return strconv.FormatInt(image.ID, 10)
	},
}
View Source
var ListCmd = &base.ListCmd[*hcloud.Image, schema.Image]{
	ResourceNamePlural: "Images",
	JSONKeyGetByName:   "images",
	DefaultColumns:     []string{"id", "type", "name", "description", "architecture", "image_size", "disk_size", "created", "deprecated"},
	SortOption:         config.OptionSortImage,

	AdditionalFlags: func(cmd *cobra.Command) {
		cmd.Flags().StringSliceP("type", "t", []string{}, "Only show Images of given type: system|app|snapshot|backup")
		_ = cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("backup", "snapshot", "system", "app"))

		cmd.Flags().StringSliceP("architecture", "a", []string{}, "Only show Images of given architecture: x86|arm")
		_ = cmd.RegisterFlagCompletionFunc("architecture", cmpl.SuggestCandidates(string(hcloud.ArchitectureX86), string(hcloud.ArchitectureARM)))
	},
	Fetch: func(s state.State, flags *pflag.FlagSet, listOpts hcloud.ListOpts, sorts []string) ([]*hcloud.Image, error) {
		opts := hcloud.ImageListOpts{ListOpts: listOpts, IncludeDeprecated: true}

		types, _ := flags.GetStringSlice("type")
		var (
			unknown []string
		)
		for _, imageType := range types {
			switch imageType {
			case string(hcloud.ImageTypeBackup), string(hcloud.ImageTypeSnapshot), string(hcloud.ImageTypeSystem), string(hcloud.ImageTypeApp):
				opts.Type = append(opts.Type, hcloud.ImageType(imageType))
			default:
				unknown = append(unknown, imageType)
			}
		}
		if len(unknown) > 0 {
			return nil, fmt.Errorf("unknown Image type: %s", strings.Join(unknown, ", "))
		}

		architecture, _ := flags.GetStringSlice("architecture")
		if len(architecture) > 0 {
			for _, arch := range architecture {
				opts.Architecture = append(opts.Architecture, hcloud.Architecture(arch))
			}
		}

		if len(sorts) > 0 {
			opts.Sort = sorts
		}

		return s.Client().Image().AllWithOpts(s, opts)
	},

	OutputTable: func(t *output.Table[*hcloud.Image], client hcapi2.Client) {
		t.
			AddAllowedFields(&hcloud.Image{}).
			AddFieldAlias("imagesize", "image size").
			AddFieldAlias("disksize", "disk size").
			AddFieldAlias("osflavor", "os flavor").
			AddFieldAlias("osversion", "os version").
			AddFieldAlias("rapiddeploy", "rapid deploy").
			AddFieldAlias("createdfrom", "created from").
			AddFieldAlias("boundto", "bound to").
			AddFieldFn("name", func(image *hcloud.Image) string {
				return util.NA(image.Name)
			}).
			AddFieldFn("image_size", func(image *hcloud.Image) string {
				if image.ImageSize == 0 {
					return util.NA("")
				}
				return fmt.Sprintf("%.2f GB", image.ImageSize)
			}).
			AddFieldFn("disk_size", func(image *hcloud.Image) string {
				return fmt.Sprintf("%.0f GB", image.DiskSize)
			}).
			AddFieldFn("created", func(image *hcloud.Image) string {
				return humanize.Time(image.Created)
			}).
			AddFieldFn("bound_to", func(image *hcloud.Image) string {
				if image.BoundTo != nil {
					return client.Server().ServerName(image.BoundTo.ID)
				}
				return util.NA("")
			}).
			AddFieldFn("created_from", func(image *hcloud.Image) string {
				if image.CreatedFrom != nil {
					return client.Server().ServerName(image.CreatedFrom.ID)
				}
				return util.NA("")
			}).
			AddFieldFn("protection", func(image *hcloud.Image) string {
				var protection []string
				if image.Protection.Delete {
					protection = append(protection, "delete")
				}
				return strings.Join(protection, ", ")
			}).
			AddFieldFn("labels", func(image *hcloud.Image) string {
				return util.LabelsToString(image.Labels)
			}).
			AddFieldFn("created", func(image *hcloud.Image) string {
				return util.Datetime(image.Created)
			}).
			AddFieldFn("age", func(image *hcloud.Image) string {
				return util.Age(image.Created, time.Now())
			}).
			AddFieldFn("deprecated", func(image *hcloud.Image) string {
				if image.Deprecated.IsZero() {
					return "-"
				}
				return util.Datetime(image.Deprecated)
			})
	},

	Schema: hcloud.SchemaFromImage,
}
View Source
var UpdateCmd = base.UpdateCmd[*hcloud.Image]{
	ResourceNameSingular: "Image",
	ShortDescription:     "Update an Image",
	NameSuggestions:      func(c hcapi2.Client) func() []string { return c.Image().Names },
	Fetch: func(s state.State, _ *cobra.Command, idOrName string) (*hcloud.Image, *hcloud.Response, error) {
		id, err := strconv.ParseInt(idOrName, 10, 64)
		if err != nil {
			return nil, nil, fmt.Errorf("invalid snapshot or backup ID %q", idOrName)
		}
		return s.Client().Image().GetByID(s, id)
	},
	DefineFlags: func(cmd *cobra.Command) {
		cmd.Flags().String("description", "", "Image description")
		cmd.Flags().String("type", "", "Image type")
		_ = cmd.RegisterFlagCompletionFunc("type", cmpl.SuggestCandidates("snapshot"))
	},
	Update: func(s state.State, _ *cobra.Command, image *hcloud.Image, flags map[string]pflag.Value) error {
		updOpts := hcloud.ImageUpdateOpts{
			Description: hcloud.Ptr(flags["description"].String()),
			Type:        hcloud.ImageType(flags["type"].String()),
		}
		_, _, err := s.Client().Image().Update(s, image, updOpts)
		if err != nil {
			return err
		}
		return nil
	},
}

Functions

func DescribeImage added in v1.56.0

func DescribeImage(image *hcloud.Image) string

func NewCommand

func NewCommand(s state.State) *cobra.Command

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL