hostname

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: 9 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 hostname is the hostname behavior: the name of the host, and on Debian, setting it.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"hostname", "Get or set the hostname of target hosts",
	behavior.Node,
	behavior.API,
	behavior.Area(area.Config),
	behavior.CLI,

	behavior.Platform(behavior.PlatformLinux, newLinux),
	behavior.Platform(behavior.PlatformDarwin, newDarwin),
	behavior.Runs("hostname"),
	behavior.Read("get", Get).
		Describe("Get the hostname of the host").
		Explain("Reads the host's name as the hostname command prints it, on Linux and "+
			"on macOS. It makes no DNS lookup.").
		Example("A host named web-01", GetInput{}, Entry{Name: "web-01"},
			behavior.Prints("hostname", "web-01\n"),
			behavior.Prints("hostname -s", "web-01\n"),
		),
	behavior.Write("update", Update).
		On(behavior.PlatformDebian).
		Sudo("hostnamectl").
		Describe("Set the hostname of the host").
		Explain("Sets the static hostname with hostnamectl set-hostname, which keeps it "+
			"after a reboot. A host that already has the name does not change and "+
			"reports changed: false. It runs on the Debian family only, and elsewhere "+
			"reports skipped. In a container the runtime owns the hostname, so there "+
			"it also reports skipped.").
		Example("Rename a host", UpdateInput{Name: "web-02"},
			Result{Name: "web-02", Changed: true},
			append(
				named("web-01"),
				behavior.PrintsAfter(
					"hostnamectl set-hostname web-02",
					"hostnamectl hostname",
					"web-02\n",
				),
				behavior.Ran("hostnamectl", "set-hostname", "web-02"),
				behavior.Reads(GetInput{}, Entry{Name: "web-02"}),
			)...,
		).
		Example("A host that already has the name", UpdateInput{Name: "web-01"},
			Result{Name: "web-01"},
			append(named("web-01"),
				behavior.Reads(GetInput{}, Entry{Name: "web-01"}),
			)...,
		).
		Fails("A name that is not a hostname", UpdateInput{Name: "web 01;reboot"},
			"invalid hostname").
		Fails("A name longer than 253 characters", UpdateInput{Name: strings.Repeat("a", 254)},
			"invalid hostname").
		Fails("No name", UpdateInput{}, "invalid hostname"),
))

Behavior is the hostname definition.

Functions

This section is empty.

Types

type Entry

type Entry struct {
	Name string `json:"name" doc:"The hostname of the host"`
}

Entry is the host's name.

func Get

func Get(
	ctx context.Context,
	p Provider,
	_ GetInput,
) (Entry, error)

Get is the host's name.

type GetInput

type GetInput struct{}

GetInput is get's input, which has none.

type Provider

type Provider interface {
	// Hostname is the kernel's hostname.
	Hostname(ctx context.Context) (string, error)
	// SetHostname sets the static hostname, and reports whether it changed.
	SetHostname(ctx context.Context, name string) (bool, error)
}

Provider is what each platform implements.

type Result

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

Result is the name after an update, and whether the host changed.

func Update

func Update(
	ctx context.Context,
	p Provider,
	in UpdateInput,
) (Result, error)

Update sets the host's name.

type UpdateInput

type UpdateInput struct {
	Name string `json:"name" validate:"required,max=253,hostname" doc:"The hostname to set, such as web-01" telemetry:"attr"`
}

UpdateInput is the name to set.

Directories

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

Jump to

Keyboard shortcuts

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