user

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 17 Imported by: ? 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 service defines portable per-user OS service definitions and their registration lifecycle. Rendering is pure; publication never replaces an existing definition; registration, start, stop, and status are delegated to a caller-selected manager.

The package owns no product name, installation layout, data directory, port, key provider, endpoint, or readiness protocol. The consumer supplies every value that identifies its service.

Index

Constants

View Source
const MaxCommandOutput = 64 << 10

MaxCommandOutput bounds captured manager output. Manager output is diagnostic text, never a protocol, and is truncated rather than streamed to the host.

Variables

View Source
var (
	// ErrInvalid reports a malformed specification or definition.
	ErrInvalid = errors.New("invalid service definition")
	// ErrExists reports a different definition already at the target path.
	ErrExists = errors.New("a different service definition already exists")
	// ErrStale reports that the published definition no longer matches the
	// specification the caller asked to act on.
	ErrStale = errors.New("published service definition changed")
	// ErrUnsafe reports a link, non-directory, or broadly writable location.
	ErrUnsafe = errors.New("unsafe service path")
	// ErrUnsupported reports a platform or capability the backend cannot honor.
	ErrUnsupported = errors.New("unsupported service platform")
	// ErrRegistered reports a service the manager already knows.
	ErrRegistered = errors.New("service is already registered")
	// ErrNotInstalled reports lifecycle work on a definition nobody published.
	ErrNotInstalled = errors.New("service definition is not installed")
)

Functions

func ExecRunner

func ExecRunner(ctx context.Context, directory, executable string, args ...string) ([]byte, error)

ExecRunner runs one native service command with captured, bounded output. It is the default CommandRunner for the platform backends.

func OutputText

func OutputText(output []byte) string

OutputText returns trimmed command output for a status detail.

func Remove

func Remove(def Definition, directory string) error

Remove deletes only the regular definition file this specification names.

Types

type CommandRunner

type CommandRunner func(ctx context.Context, directory, executable string, args ...string) ([]byte, error)

CommandRunner is the native command boundary. Output is captured, not inherited, so a manager cannot write into the host application's streams.

type Definition

type Definition struct {
	Platform Platform
	Identity string
	FileName string
	Content  string
	Digest   string
	Path     string
}

Definition is a rendered native service definition. Path is empty until the definition is published.

func Render

func Render(spec Spec, platform Platform) (Definition, error)

Render produces the native definition for a platform without touching the filesystem.

type Inspection

type Inspection struct {
	Path      string
	Installed bool
	Matches   bool
	Digest    string
}

Inspection is the on-disk state of one expected definition.

func CheckDefinition

func CheckDefinition(def Definition, directory string) (Inspection, error)

CheckDefinition rejects a status operation when the on-disk definition is missing or no longer the one the caller rendered.

func Inspect

func Inspect(def Definition, directory string) (Inspection, error)

Inspect reports whether the expected definition is installed and unchanged.

type Manager

type Manager interface {
	Platform() Platform
	Name() string
	Register(ctx context.Context, spec Spec) (Registration, error)
	Unregister(ctx context.Context, spec Spec) error
	Start(ctx context.Context, spec Spec) error
	Stop(ctx context.Context, spec Spec) error
	Status(ctx context.Context, spec Spec) (Status, error)
}

Manager is implemented by a per-user service backend. It never selects its own identity, directory, or executable.

type Platform

type Platform string

Platform selects the native definition format. It is never inferred from the runtime inside this package.

const (
	PlatformDarwin  Platform = "darwin"
	PlatformLinux   Platform = "linux"
	PlatformWindows Platform = "windows"
)

type Publication

type Publication struct {
	Path     string
	Created  bool
	Existing bool
}

Publication reports a bounded, non-overwriting publish attempt.

func Publish

func Publish(def Definition, directory string) (Publication, error)

Publish writes a complete definition into an existing directory without replacing anything. A hard link makes publication atomic and fails closed on filesystems that cannot link. An identical existing file is reported rather than rewritten.

type Readiness

type Readiness func(context.Context) error

Readiness is an optional caller probe. The library has no protocol, port, or endpoint of its own.

type Registration

type Registration struct {
	Definition  Definition
	Publication Publication
	Registered  bool
}

Registration reports publication and manager registration separately so a caller can recover when the manager rejects an otherwise valid definition.

type RestartPolicy

type RestartPolicy string

RestartPolicy is the caller's restart choice. Platforms that cannot express a policy exactly fail instead of silently substituting another behavior.

const (
	RestartNever     RestartPolicy = "never"
	RestartOnFailure RestartPolicy = "on-failure"
	RestartAlways    RestartPolicy = "always"
)

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service binds one specification to one manager and an optional readiness probe.

func New

func New(manager Manager, spec Spec, readiness Readiness) (*Service, error)

func (*Service) Register

func (s *Service) Register(ctx context.Context) (Registration, error)

func (*Service) Spec

func (s *Service) Spec() Spec

func (*Service) Start

func (s *Service) Start(ctx context.Context) error

func (*Service) Status

func (s *Service) Status(ctx context.Context) (Status, error)

Status inspects the manager and then, when configured, the caller's readiness probe. A probe error reports not ready and is included in the detail.

func (*Service) Stop

func (s *Service) Stop(ctx context.Context) error

func (*Service) Unregister

func (s *Service) Unregister(ctx context.Context) error

type Spec

type Spec struct {
	// Name is the service identity: a launchd label, a systemd unit name, or a
	// Task Scheduler task name.
	Name string
	// Description is human-readable metadata; it defaults to Name.
	Description string
	// Executable is an absolute local path.
	Executable string
	// Arguments are passed verbatim to the executable.
	Arguments []string
	// Environment is rendered only on platforms that support it. Windows Task
	// Scheduler has no environment field, so a non-empty map fails there.
	Environment map[string]string
	// WorkingDirectory is an optional absolute local path.
	WorkingDirectory string
	// Restart selects the manager's restart behavior.
	Restart RestartPolicy
	// RestartSeconds delays a restart. Zero selects five seconds.
	RestartSeconds int
	// UserID is the explicit Windows task account name or SID.
	UserID string
}

Spec is the complete caller-supplied service definition.

func ValidateSpec

func ValidateSpec(spec Spec, platform Platform) (Spec, error)

ValidateSpec resolves defaults and checks every value the renderer uses. It is exported so a backend can validate before touching the filesystem.

type Status

type Status struct {
	Name             string            `json:"name"`
	Manager          string            `json:"manager"`
	DefinitionPath   string            `json:"definition_path,omitempty"`
	DefinitionDigest string            `json:"definition_digest,omitempty"`
	Installed        bool              `json:"installed"`
	Stale            bool              `json:"stale"`
	Registered       bool              `json:"registered"`
	Active           bool              `json:"active"`
	Detail           string            `json:"detail"`
	CheckedAt        time.Time         `json:"checked_at"`
	Ready            *bool             `json:"ready,omitempty"`
	Attributes       map[string]string `json:"attributes,omitempty"`
}

Status is a point-in-time projection. Ready is filled by a Service wrapper's readiness callback; a manager never infers readiness from being active.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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