progress

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: Apache-2.0 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 progress writes and reads the progress stream that a long-running ocfp command produces, one JSON object per line.

Index

Constants

View Source
const WireContract = 3

WireContract is the version of the progress stream format and of the job request that asks for it. A reader compares the header's wireContract with this value before it trusts the rest of the stream. Contract 2 added the org and the bloc to the job request. Contract 3 added the request's action, the cancel budget in the header, and the cancel outcome in the result.

Variables

View Source
var (
	// ErrHeaderFirst is returned when Encode runs before Header.
	ErrHeaderFirst = errors.New("progress: the header must be written first")

	// ErrNoHeader is returned when a stream does not begin with a header record.
	ErrNoHeader = errors.New("progress: stream does not begin with a header record")
)
View Source
var ErrAnswerTooLong = errors.New("progress: answer line is too long")

ErrAnswerTooLong is returned when an answer line passes the length limit.

View Source
var ErrNoAnswerNonInteractive = errors.New("progress: question has no default and the run is not interactive")

ErrNoAnswerNonInteractive is returned when a run is not interactive and a question has no default, so nothing could answer it.

Functions

This section is empty.

Types

type AnswerSource added in v0.5.4

type AnswerSource interface {
	Answer(id string) (string, error)
}

AnswerSource supplies the answer for a question id, blocking until one is available.

type Artifact added in v0.5.4

type Artifact struct {
	Name   string `json:"name"`
	Path   string `json:"path"`
	Digest string `json:"digest"`
}

Artifact describes a file that a step produced.

type CancelOutcome added in v0.5.8

type CancelOutcome string

CancelOutcome says how a canceled job ended.

const (
	// CancelStopped means the verb reached a safe point and stopped.
	CancelStopped CancelOutcome = "stopped"

	// CancelKilled means the cancel budget passed and the verb was killed.
	CancelKilled CancelOutcome = "killed"

	// CancelAlreadyFinished means the job had ended before the cancel took
	// effect.
	CancelAlreadyFinished CancelOutcome = "already_finished"
)

The cancel outcomes. A run stores only stopped and killed. A requester derives already_finished from a stream that carries no cancel.

func CancelOutcomeOf added in v0.5.8

func CancelOutcomeOf(lastError *ErrorBody, res ResultBody) (CancelOutcome, bool)

CancelOutcomeOf is the outcome that a cancel requester reads from the stream it got back, given the stream's last error, if any, and its result. A result whose cancel is stopped or killed gives that outcome. A complete stream with no error and no cancel in its result means the job had already finished. A stream that ends in JOB_ORPHANED or in a refusal, or whose result names a cancel this CLI does not know, has no outcome, and CancelOutcomeOf then reports false.

type Clock

type Clock interface {
	Now() time.Time
}

Clock supplies the time stamped on each record.

func SystemClock

func SystemClock() Clock

SystemClock returns a Clock backed by the wall clock.

type Code

type Code string

Code is a stable, machine-readable name for a refusal or failure.

const (
	// CodeOwnerRecordMissing reports that the host has no owner record.
	CodeOwnerRecordMissing Code = "OWNER_RECORD_MISSING"

	// CodeOwnerMismatch reports a record that names another installation or
	// epoch.
	CodeOwnerMismatch Code = "OWNER_MISMATCH"

	// CodeRequestUnsigned reports a request that the owner's instance key did
	// not sign.
	CodeRequestUnsigned Code = "REQUEST_UNSIGNED"

	// CodeOwnerHandover reports an owner record that is in handover.
	CodeOwnerHandover Code = "OWNER_HANDOVER"

	// CodeJobRequestInvalid reports a request that does not parse or whose
	// issue time falls outside the accepted window.
	CodeJobRequestInvalid Code = "JOB_REQUEST_INVALID"

	// CodeVerbNotAllowed reports a verb that the wrapper does not accept.
	CodeVerbNotAllowed Code = "VERB_NOT_ALLOWED"

	// CodeWireContractUnsupported reports a wire contract version that the
	// CLI cannot write.
	CodeWireContractUnsupported Code = "WIRE_CONTRACT_UNSUPPORTED"

	// CodeOwnerRecordExists reports a create-only write of an owner record
	// where one already exists.
	CodeOwnerRecordExists Code = "OWNER_RECORD_EXISTS"

	// CodeJobOrphaned reports an attach to a spool whose wrapper died without
	// writing an exit record.
	CodeJobOrphaned Code = "JOB_ORPHANED"

	// CodeOwnerRecordInvalid reports an owner record on the host that does
	// not parse or does not verify, so that a requester never reads it as a
	// fault in its own request.
	CodeOwnerRecordInvalid Code = "OWNER_RECORD_INVALID"

	// CodeOrgBlocMismatch reports a request, a replacement owner record, or a
	// proof that gives up the owner record, for an org or a bloc other than
	// the one the host's owner record names.
	CodeOrgBlocMismatch Code = "ORG_BLOC_MISMATCH"

	// CodeJobCanceled reports a job that ended because a cancel asked it to
	// stop.
	CodeJobCanceled Code = "JOB_CANCELED"

	// CodeJobNotFound reports a cancel of a job that has no spool on the
	// host. A requester whose own run has not yet made the spool can try
	// again.
	CodeJobNotFound Code = "JOB_NOT_FOUND"
)

The refusal codes. A code is never renamed once it ships.

type Decoder

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

Decoder reads records from a stream.

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a Decoder that reads from r.

func (*Decoder) Decode

func (d *Decoder) Decode() (Record, error)

Decode returns the next record after the header. When the header has not been read yet, Decode reads and checks it first, so a stream without a header is refused with ErrNoHeader. At the end of the stream it returns io.EOF.

func (*Decoder) Header

func (d *Decoder) Header() (Header, error)

Header reads the first record and returns its header body. It returns ErrNoHeader when the stream is empty or begins with another kind of record.

type Encoder

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

Encoder writes records as lines of JSON. It is safe for concurrent use, and a line is always written whole.

func NewEncoder

func NewEncoder(w io.Writer, c Clock) *Encoder

NewEncoder returns an Encoder that writes to w and stamps records from c.

func (*Encoder) Encode

func (e *Encoder) Encode(kind Kind, body any) error

Encode writes one record of the given kind. It returns ErrHeaderFirst until Header has been called.

func (*Encoder) Header

func (e *Encoder) Header(h Header) error

Header writes the header record, which carries seq 0. The WireContract field is always set to the current WireContract.

type ErrorBody

type ErrorBody struct {
	Code    Code   `json:"code"`
	Message string `json:"message"`
}

ErrorBody is the body of an error record.

type Event added in v0.5.4

type Event struct {
	Kind     EventKind `json:"kind"`
	Step     string    `json:"step"`
	Message  string    `json:"message,omitempty"`
	Current  int64     `json:"current,omitempty"`
	Total    int64     `json:"total,omitempty"`
	Outcome  string    `json:"outcome,omitempty"`
	Artifact *Artifact `json:"artifact,omitempty"`

	// Undone lists the work that a canceled job left undone, because its
	// undo hook did not run or failed. Only the warning that a canceled
	// job's stream carries sets it.
	Undone []UndoneWork `json:"undone,omitempty"`
}

Event is one structured report of progress from a step.

type EventKind added in v0.5.4

type EventKind string

EventKind names the type of a progress event.

const (
	EventStepStarted      EventKind = "step_started"
	EventStepProgress     EventKind = "step_progress"
	EventStepFinished     EventKind = "step_finished"
	EventWarning          EventKind = "warning"
	EventArtifactProduced EventKind = "artifact_produced"
)

The event kinds.

type Header struct {
	WireContract int    `json:"wireContract"`
	Producer     string `json:"producer"`
	Version      string `json:"version"`
	JobID        string `json:"jobId,omitempty"`

	// CancelBudgetMs is how long, in whole milliseconds, the verb that runs
	// has to stop once a cancel arrives. It is absent from a header that
	// names no verb.
	CancelBudgetMs int64 `json:"cancelBudgetMs,omitempty"`
}

Header is the body of the first record. It names the producer and the wire contract the rest of the stream follows.

type Kind

type Kind string

Kind names the type of a record in the stream.

const (
	KindHeader   Kind = "header"
	KindEvent    Kind = "event"
	KindQuestion Kind = "question"
	KindLog      Kind = "log"
	KindResult   Kind = "result"
	KindError    Kind = "error"
)

The record kinds. The first record of every stream is a header.

type LogBody

type LogBody struct {
	Stream string `json:"stream"`
	Line   string `json:"line"`
}

LogBody is the body of a log record.

type Prompter added in v0.5.4

type Prompter interface {
	Ask(q Question) (string, error)
}

Prompter asks a question and returns the answer.

func NewNDJSONPrompter added in v0.5.4

func NewNDJSONPrompter(enc *Encoder, src AnswerSource, interactive bool) Prompter

NewNDJSONPrompter returns a Prompter that writes each question as a question record and then waits for src to supply the answer. An answer is never written to the stream. When interactive is false it answers from the default without writing anything, and fails when there is none.

func NewTerminalPrompter added in v0.5.4

func NewTerminalPrompter(in io.Reader, out io.Writer, interactive bool) Prompter

NewTerminalPrompter returns a Prompter that writes questions to out and reads answers from in. When interactive is false it never touches either, answers from the default, and fails when there is none. A sensitive question's default is never printed. The reader is used as given, so a caller that wants hidden input passes a reader that provides it. A terminal prompter is for one goroutine at a time.

type Question added in v0.5.4

type Question struct {
	ID        string       `json:"id"`
	Kind      QuestionKind `json:"kind"`
	Text      string       `json:"text"`
	Choices   []string     `json:"choices"`
	Default   *string      `json:"default"`
	Sensitive bool         `json:"sensitive"`
}

Question is one thing a step needs answered. A confirm answer is "yes" or "no", and a choose answer is one of Choices. Default is nil when the question has none.

type QuestionKind added in v0.5.4

type QuestionKind string

QuestionKind names the shape of the answer a question expects.

const (
	QuestionConfirm QuestionKind = "confirm"
	QuestionChoose  QuestionKind = "choose"
	QuestionText    QuestionKind = "text"
)

The question kinds.

type Record

type Record struct {
	Kind Kind            `json:"kind"`
	Seq  int64           `json:"seq"`
	Time time.Time       `json:"time"`
	Body json.RawMessage `json:"body"`
}

Record is one line of the stream. Seq is 0 for the header and counts up by one for every record after it.

type Refusal added in v0.5.4

type Refusal struct {
	Code    Code
	Message string
}

Refusal is an error that carries a Code, so a caller can act on the reason without matching message text.

func AsRefusal added in v0.5.4

func AsRefusal(err error) (*Refusal, bool)

AsRefusal finds a *Refusal anywhere in err's chain. A typed-nil *Refusal in the chain is not a refusal, so it answers false.

func (*Refusal) Error added in v0.5.4

func (r *Refusal) Error() string

Error returns the code followed by the message.

type ResultBody

type ResultBody struct {
	ExitCode int           `json:"exitCode"`
	Cancel   CancelOutcome `json:"cancel,omitempty"`
}

ResultBody is the body of the final record of a stream. Cancel is set only when a cancel ended the job.

type Sink added in v0.5.4

type Sink interface {
	Emit(ev Event) error
}

Sink receives progress events. An adapter decides how to show them.

func NewNDJSONSink added in v0.5.4

func NewNDJSONSink(enc *Encoder) Sink

NewNDJSONSink returns a Sink that writes each event as an event record.

func NewTerminalSink added in v0.5.4

func NewTerminalSink(w io.Writer) Sink

NewTerminalSink returns a Sink that prints events as readable lines to w.

type UndoState added in v0.5.8

type UndoState string

UndoState says why a piece of work was left undone.

const (
	// UndoNotRun is work whose undo hook never ran.
	UndoNotRun UndoState = "not_run"

	// UndoFailed is work whose undo hook ran and failed.
	UndoFailed UndoState = "failed"
)

The reasons a piece of work can be left undone.

type UndoneWork added in v0.5.8

type UndoneWork struct {
	WorkRef

	State UndoState `json:"state"`
}

UndoneWork is one piece of work that a job left undone, and why. Its JSON holds the work's kind and id beside the state.

type WorkRef added in v0.5.8

type WorkRef struct {
	Kind string `json:"kind"`
	ID   string `json:"id"`
}

WorkRef names one piece of work that a job started outside itself, by its kind and its id. A warning that undo hooks did not run lists the work they would have undone this way.

func (WorkRef) String added in v0.5.8

func (w WorkRef) String() string

String names the work as its kind and then its id, or as its kind alone when it has no id.

Directories

Path Synopsis
Package progresstest holds golden progress streams for tests.
Package progresstest holds golden progress streams for tests.

Jump to

Keyboard shortcuts

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