packages

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 10 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

Overview

Package packages is the package behavior: the packages installed on the host.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"package", "The packages installed on the host",
	behavior.Node,
	behavior.API,
	behavior.Area(area.Software),
	behavior.CLI,
	behavior.Platform(behavior.PlatformDebian, newDebian),
	behavior.Platform(behavior.PlatformLinux, newReader),
	behavior.Platform(behavior.PlatformDarwin, newReader),
	behavior.RunsOnPlatform(behavior.PlatformDebian, "dpkg-query"),
	behavior.RunsOnPlatform(behavior.PlatformDarwin, "brew"),
	behavior.RunsIfPresent("rpm"),
	behavior.Read("list", List).
		Describe("List the packages installed on the host").
		Explain("Lists each installed package with its version: dpkg on the Debian "+
			"family, rpm on the other Linux families, and Homebrew on macOS.").
		Example("A Debian host", ListInput{}, []Installed{curl},
			behavior.OnPlatform(behavior.PlatformDebian),
			behavior.Prints(dpkg, installed+"tree\t2.1.1-2\tamd64\tdeinstall\tdisplays an indented directory tree\t100\n"),
		).
		Example("A Fedora host", ListInput{}, []Installed{{
			Name: "curl", Version: "8.6.0-7.fc40", Architecture: "x86_64", Manager: "rpm",
			Description: "A utility for getting files from remote servers", Size: 790000,
		}},
			behavior.OnPlatform(behavior.PlatformLinux),
			behavior.Prints(rpm, "curl\t8.6.0-7.fc40\tx86_64\tA utility for getting files from remote servers\t790000\n"),
		).
		Example("A Mac", ListInput{}, []Installed{{Name: "jq", Version: "1.7.1", Manager: "brew"}},
			behavior.OnPlatform(behavior.PlatformDarwin),
			behavior.Prints(brew, "jq 1.7 1.7.1\n"),
		),
	behavior.Read("get", Get).Key("name").
		Describe("Get one installed package").
		Explain("Reads one package from the same list. A package that is not "+
			"installed is not found.").
		Example("An installed package", GetInput{Name: "curl"}, curl,
			behavior.OnPlatform(behavior.PlatformDebian),
			behavior.Prints(dpkg, installed),
		).
		Example("An installed formula", GetInput{Name: "jq"},
			Installed{Name: "jq", Version: "1.7.1", Manager: "brew"},
			behavior.OnPlatform(behavior.PlatformDarwin),
			behavior.Prints(brew, "jq 1.7.1\n"),
		).
		Example("An installed RPM", GetInput{Name: "curl"},
			Installed{
				Name:         "curl",
				Version:      "8.6.0-7.fc40",
				Architecture: "x86_64",
				Manager:      "rpm",
			},
			behavior.OnPlatform(behavior.PlatformLinux),
			behavior.Prints(rpm, "curl\t8.6.0-7.fc40\tx86_64\n"),
		).
		Fails("A package that is not installed", GetInput{Name: "tree"}, "not found",
			behavior.NotLive("the suite installs tree on a live host, for the package examples")).
		Fails("A name that is an option", GetInput{Name: "-oAPT::Get=x"}, "not a valid package name").
		Fails("A name longer than 255 characters",
			GetInput{Name: strings.Repeat("a", 256)}, "not a valid package name"),
	behavior.Write("create", Create).
		On(behavior.PlatformDebian).
		Sudo("apt-get").
		Describe("Install a package").
		Explain("Installs the package with apt-get install, at the version when one is "+
			"given. A package that is installed, at that version when one is given, "+
			"does not change and reports changed: false. It runs on the Debian family "+
			"only, and elsewhere reports skipped.").
		Example("Install a package", CreateInput{Name: "tree"},
			Result{Name: "tree", Changed: true},
			behavior.PrintsAfter("apt-get install -y -- tree", dpkg, tree),
			behavior.Ran("apt-get", "install", "-y", "--", "tree"),
			behavior.Reads(GetInput{Name: "tree"}, Installed{Name: "tree"}),
		).
		Fails("A name that is an option", CreateInput{Name: "-oAPT::Get=x"}, "not a valid package name").
		Fails("A name longer than 255 characters",
			CreateInput{Name: strings.Repeat("a", 256)}, "not a valid package name").
		Fails("A version that is an option", CreateInput{Name: "tree", Version: "-x"},
			"not a valid package version").
		Fails("A version longer than 255 characters",
			CreateInput{
				Name:    "tree",
				Version: strings.Repeat("1", 256),
			}, "not a valid package version"),
	behavior.Write("delete", Delete).Key("name").
		On(behavior.PlatformDebian).
		Sudo("apt-get").
		Describe("Remove a package").
		Explain("Removes the package with apt-get remove. A package that is not "+
			"installed does not change and reports changed: false. It runs on the "+
			"Debian family only, and elsewhere reports skipped.").
		Example("Remove a package", DeleteInput{Name: "tree"},
			Result{Name: "tree", Changed: true},
			behavior.After(CreateInput{Name: "tree"}),
			behavior.PrintsAfter("apt-get install -y -- tree", dpkg, tree),
			behavior.PrintsAfter("apt-get remove -y -- tree", dpkg, ""),
			behavior.Ran("apt-get", "remove", "-y", "--", "tree"),
			behavior.ReadsNone(GetInput{Name: "tree"}, Installed{Name: "tree"}, "name"),
		).
		Example("A package that is not installed", DeleteInput{Name: "tree"},
			Result{Name: "tree"},
			behavior.ReadsNone(GetInput{Name: "tree"}, Installed{Name: "tree"}, "name"),
		).
		Fails("A name that is an option", DeleteInput{Name: "-oAPT::Get=x"}, "not a valid package name").
		Fails("A name longer than 255 characters",
			DeleteInput{Name: strings.Repeat("a", 256)}, "not a valid package name"),
))

Behavior is the packages installed on the host. gohai reads them on every platform; apt-get installs and removes them on the Debian family.

Functions

This section is empty.

Types

type CreateInput

type CreateInput struct {
	Name    string `` /* 163-byte string literal not displayed */
	Version string `` /* 146-byte string literal not displayed */
}

CreateInput is the package to install.

type DeleteInput

type DeleteInput struct {
	Name string `json:"name" validate:"required,max=255,package_name" doc:"The package to remove, such as curl"`
}

DeleteInput is the package to remove.

type GetInput

type GetInput struct {
	Name string `json:"name" validate:"required,max=255,package_name" doc:"The package, such as curl"`
}

GetInput names one installed package.

type Installed

type Installed struct {
	Name         string `json:"name"                   doc:"The package"`
	Version      string `json:"version"                doc:"The installed version"`
	Architecture string `json:"architecture,omitempty" doc:"The architecture it is built for, such as amd64"`
	Manager      string `json:"manager"                doc:"The package database that has it: dpkg, rpm or brew"`
	Description  string `json:"description,omitempty"  doc:"Its one-line summary"`
	Size         int64  `json:"size,omitempty"         doc:"Its installed size in bytes"                         render:"bytes"`
}

Installed is one installed package.

func Get

func Get(
	ctx context.Context,
	p Provider,
	in GetInput,
) (Installed, error)

Get is one installed package.

func List

func List(
	ctx context.Context,
	p Provider,
	_ ListInput,
) ([]Installed, error)

List is every installed package.

type ListInput

type ListInput struct{}

ListInput takes nothing: it lists every installed package.

type Provider

type Provider interface {
	// List is every installed package.
	List(ctx context.Context) ([]Installed, error)
	// Get is one installed package, or behavior.ErrNotFound.
	Get(ctx context.Context, name string) (Installed, error)
	// Install installs a package, at version when it is not empty, and
	// reports whether it changed the host.
	Install(ctx context.Context, name string, version string) (bool, error)
	// Remove removes a package, and reports whether it changed the host.
	Remove(ctx context.Context, name string) (bool, error)
}

Provider reads the installed packages and installs and removes them.

type Result

type Result struct {
	Name    string `json:"name"    doc:"The package"`
	Changed bool   `json:"changed" doc:"Whether the host changed"`
}

Result is the package after an install or a removal.

func Create

func Create(
	ctx context.Context,
	p Provider,
	in CreateInput,
) (Result, error)

Create installs a package.

func Delete

func Delete(
	ctx context.Context,
	p Provider,
	in DeleteInput,
) (Result, error)

Delete removes a package.

Directories

Path Synopsis
Package mocks holds the generated double of the package provider, for the methods' tests.
Package mocks holds the generated double of the package provider, for the methods' tests.

Jump to

Keyboard shortcuts

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