console

package
v0.1.0-alpha.1 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package console is the interactive terminal console of Regulus and the plain-text and JSON output that takes its place when no terminal UI is wanted: what the hub is doing, its settings, its logs and health checks.

Index

Constants

View Source
const (
	MinWidth  = 60
	MinHeight = 16
)

Minimum size for the full-screen console; below it the plain output is used.

View Source
const (
	OK   = "ok"
	Warn = "warn"
	Fail = "fail"
	Info = "info"
)

Check statuses.

View Source
const ServiceUnit = "regulus"

ServiceUnit is the systemd unit of the hub.

Variables

View Source
var T = en

T is the language in use.

Functions

func FormatBytes

func FormatBytes(n int64) string

FormatBytes writes a size for people: 1.5 MiB.

func Run

func Run(opt Options) error

Run opens the full-screen console and returns when the person quits.

func UnicodeTerminal

func UnicodeTerminal(getenv func(string) string) bool

UnicodeTerminal guesses whether the terminal can show characters beyond ASCII (✓, ●, box lines) from the locale; without it the console draws with plain characters.

func UseTUI

func UseTUI(c Choice, configTUI bool, getenv func(string) string, term Terminal) (use bool, reason string, err error)

UseTUI decides whether to open the full-screen console and says why not when it does not. The order is: command-line flags, then environment variables, then the configuration file (console.tui), then what the terminal looks like.

func WriteSettings

func WriteSettings(w io.Writer, entries []config.Entry, f Format) error

WriteSettings prints the settings with their values and sources.

func WriteStatus

func WriteStatus(w io.Writer, snap Snapshot, f Format, now time.Time) error

WriteStatus prints the snapshot.

Types

type Agent

type Agent = localstatus.Agent

Agent is a connected agent as the hub reports it.

type Check

type Check struct {
	Name   string `json:"name"`
	Status string `json:"status"`
	Detail string `json:"detail"`
}

Check is one health check.

type Choice

type Choice struct {
	// TUI is true for --tui, false for --no-tui, nil when neither was given.
	TUI *bool
	// NoInput is --no-input: never ask anything, so no interactive screen.
	NoInput bool
	// Plain and JSON are the other output formats; they print instead of
	// opening the screen.
	Plain, JSON bool
}

Choice is what the person asked on the command line.

type Format

type Format int

Format is how a command prints.

const (
	// FormatText is for people: aligned, with headings.
	FormatText Format = iota
	// FormatPlain is stable, one fact per line (key=value), for grep and awk.
	FormatPlain
	// FormatJSON is for programs.
	FormatJSON
)

type Hooks

type Hooks struct {
	// ResetAdmin makes the one-time password reset link of the administrator.
	ResetAdmin func(ctx context.Context) (link string, err error)
	// Restart restarts the hub's service.
	Restart func(ctx context.Context) error
}

Hooks are the actions that need more than the console knows how to do; the command supplies them. A nil hook hides its key.

type Model

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

Model is the Bubble Tea model of the console.

func New

func New(opt Options) Model

New returns the console model.

func (Model) Init

func (m Model) Init() tea.Cmd

Init loads the first snapshot.

func (Model) Update

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd)

Update handles input and results.

func (Model) View

func (m Model) View() string

View draws the screen: header, section tabs, the section, then the help.

type Options

type Options struct {
	System     System
	ConfigFile string
	Version    string
	Hooks      Hooks
	// Unicode is false for terminals that cannot draw ✓ and the like.
	Unicode bool
	// Refresh is how often the screen reloads; zero turns it off (tests).
	Refresh time.Duration
}

Options configures the console.

type Snapshot

type Snapshot struct {
	Version string `json:"version"`
	// ConfigFile is the file the settings are read from and written to, or
	// "" when there is none.
	ConfigFile string `json:"config_file"`
	// Problem is set when the configuration cannot be loaded.
	Problem string `json:"problem,omitempty"`

	// Service is the state of the systemd unit: active, inactive, failed,
	// or unknown when systemd does not know it or is not there.
	Service string `json:"service"`
	// Running is true when the hub answered on its local socket.
	Running bool `json:"running"`

	Listen       string   `json:"listen"`
	Port         string   `json:"port"`
	URL          string   `json:"url"`
	LANURLs      []string `json:"lan_urls,omitempty"`
	AgentListen  string   `json:"agent_listen,omitempty"`
	DataDir      string   `json:"data_dir"`
	DatabaseSize int64    `json:"database_bytes"`
	BuiltinAgent bool     `json:"builtin_agent"`
	// LogLevel and Format are shown in the header of the logs.
	LogLevel string `json:"log_level"`

	StartedAt  time.Time `json:"started_at,omitzero"`
	Uptime     string    `json:"uptime,omitempty"`
	SetupToken string    `json:"setup_token,omitempty"`
	Agents     []Agent   `json:"agents"`

	Settings []config.Entry `json:"-"`
	Health   []Check        `json:"health"`
}

Snapshot is everything the console shows about the hub at one moment.

func Collect

func Collect(ctx context.Context, sys System, configFile, version string) Snapshot

Collect gathers the snapshot: the configuration (from configFile, the environment and the defaults), the state of the service and, when the hub runs, what it says about itself.

type System

type System struct {
	Getenv func(string) string
	Now    func() time.Time
	// ServiceState returns the systemd state of the hub's unit.
	ServiceState func(ctx context.Context) string
	// Fetch asks the running hub for its status.
	Fetch func(ctx context.Context, dataDir string) (localstatus.Status, error)
	// Addrs lists the IPv4 addresses of this machine, besides loopback.
	Addrs func() []string
	// Logs returns the last lines of the hub's log.
	Logs func(ctx context.Context, n int) ([]string, error)
}

System is how the console looks at the machine. Tests replace it.

func HostSystem

func HostSystem() System

HostSystem is the real machine.

type Terminal

type Terminal struct {
	// Interactive is true when both stdin and stdout are terminals.
	Interactive   bool
	Width, Height int
}

Terminal describes the terminal the command runs in.

Jump to

Keyboard shortcuts

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