runner

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 25 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 runner executes parsed requests: it resolves variables, sends the request, evaluates captures and assertions, and persists captured values to the session.

Index

Constants

View Source
const (
	ExitOK        = 0
	ExitAssert    = 1 // an assertion or capture failed
	ExitUsage     = 2 // parse error, unknown request, missing variable
	ExitTransport = 3 // could not reach the server, timeout, TLS failure
)

Exit codes returned by the CLI.

View Source
const Masked = "***"

Masked is what a hidden value is replaced with in output.

Variables

View Source
var Version = "dev"

Version is stamped by the CLI for the User-Agent header.

Functions

func ExitCode

func ExitCode(err error) int

ExitCode maps an error to the CLI exit code.

The exit codes are a public contract, so this unwraps rather than switching on the concrete type: wrapping a TransportError with %w anywhere on the way up would otherwise turn a documented 3 into a 2.

func RedactExpr

func RedactExpr(expr string) string

RedactExpr keeps the selector and operator of an assertion expression and hides the expected value.

Types

type Description

type Description struct {
	Name        string            `json:"name,omitempty"`
	ID          string            `json:"id"`
	File        string            `json:"file"`
	Line        int               `json:"line"`
	Description string            `json:"description,omitempty"`
	Method      string            `json:"method"`
	URLTemplate string            `json:"url_template"`
	URL         string            `json:"url"` // resolved as far as possible
	Headers     map[string]string `json:"headers"`
	Body        string            `json:"body,omitempty"`
	BodyFile    string            `json:"body_file,omitempty"`
	Variables   []VarInfo         `json:"variables"`
	Captures    []string          `json:"captures,omitempty"`
	Asserts     []string          `json:"asserts,omitempty"`
	Steps       []string          `json:"steps,omitempty"`       // # @step phrases
	Auth        string            `json:"auth,omitempty"`        // auth spec template
	AuthSource  string            `json:"auth_source,omitempty"` // "request" or "apic.yaml"
	Ready       bool              `json:"ready"`                 // every variable resolves
}

Description is what `apic describe` shows.

type Options

type Options struct {
	Env       string            // environment name from http-client.env.json
	Vars      map[string]string // --var overrides
	NoSession bool              // do not read or write .apic/session.json
	Timeout   time.Duration     // default per-request timeout
	Insecure  bool              // skip TLS verification
	KeepGoing bool              // in a flow, continue after a failure
	Redact    bool              // mask every request value and capture in output (for CI logs)
	Session   *session.Store    // use this store instead of opening .apic/session.json (tests use session.NewMemory())
}

Options controls a Runner.

type Resolved

type Resolved struct {
	Name     string            `json:"name,omitempty"`
	File     string            `json:"file"`
	Line     int               `json:"line"`
	Method   string            `json:"method"`
	URL      string            `json:"url"`
	Headers  []httpfile.Header `json:"-"`
	Body     string            `json:"-"`
	Auth     string            `json:"auth,omitempty"` // auth type applied, e.g. "aws"
	AuthSpec *auth.Spec        `json:"-"`              // rendered spec (contains secrets)
	// SecretHeaders names headers whose value came from a secret source
	// (private env file, .env, session or a capture).
	SecretHeaders map[string]bool `json:"-"`
	// contains filtered or unexported fields
}

Resolved is a request with every placeholder substituted.

func (Resolved) DisplayBody

func (r Resolved) DisplayBody(redact bool) string

DisplayBody returns the body, or the mask when redacting.

func (Resolved) DisplayHeaders

func (r Resolved) DisplayHeaders(redact bool) []httpfile.Header

DisplayHeaders returns the request headers with sensitive values masked; with redact set every value is masked.

func (Resolved) DisplayURL

func (r Resolved) DisplayURL(redact bool) string

DisplayURL returns the URL with query values masked when redacting.

func (Resolved) MarshalJSON

func (r Resolved) MarshalJSON() ([]byte, error)

MarshalJSON renders headers as an object (sensitive values masked) and the body as a string. Result.MarshalJSON applies --redact on top.

type Response

type Response struct {
	Status     int               `json:"status"`
	StatusText string            `json:"status_text"`
	Headers    map[string]string `json:"headers"`
	Body       any               `json:"body"` // parsed JSON when the body is JSON, else a string
	DurationMs int64             `json:"duration_ms"`
	Size       int               `json:"size"`
}

Response is the JSON-friendly view of a response.

func (*Response) DisplayHeaders

func (r *Response) DisplayHeaders(redact bool) map[string]string

DisplayHeaders returns the response headers with every value masked when redacting, and sensitive values masked either way.

type Result

type Result struct {
	OK       bool              `json:"ok"`
	Request  Resolved          `json:"request"`
	Response *Response         `json:"response,omitempty"`
	Captures map[string]string `json:"captures,omitempty"`
	Asserts  []assert.Result   `json:"asserts,omitempty"`
	Errors   []string          `json:"errors,omitempty"`
	Redact   bool              `json:"-"` // set from Options.Redact
	// contains filtered or unexported fields
}

Result is the outcome of running one request.

func (Result) DisplayAsserts

func (r Result) DisplayAsserts() []assert.Result

DisplayAsserts returns the assertion results with the values they carry masked when redacting. An assertion like "# @assert body.$.token == {{token}}" bakes the rendered secret into Expr, Expected and Actual, so all three are hidden; Pass and Error are structural and survive.

func (Result) DisplayCaptures

func (r Result) DisplayCaptures() map[string]string

DisplayCaptures returns captures, masked when redacting.

func (*Result) DisplayRawBody

func (r *Result) DisplayRawBody() []byte

DisplayRawBody returns the raw response body for renderers, masked when redacting.

func (Result) DisplayResponse

func (r Result) DisplayResponse() *Response

DisplayResponse returns the response as it should be shown: status, timing and size survive because they are not secrets and CI needs them, while the body and header values are masked when redacting.

func (Result) MarshalJSON

func (r Result) MarshalJSON() ([]byte, error)

MarshalJSON masks sensitive request and response headers always, and everything (headers, bodies, query values, captures and assertion values) when Redact is set.

The shadowing fields sit at a shallower depth than the embedded alias, so encoding/json picks them over the originals.

func (*Result) Raw

func (r *Result) Raw() *selector.Response

Raw returns the underlying response for renderers.

type Runner

type Runner struct {
	Project *project.Project
	Envs    *env.Environments
	Session *session.Store
	Opts    Options
	Stderr  io.Writer // interactive prompts such as device-code sign-in; nil means os.Stderr
	// contains filtered or unexported fields
}

Runner executes requests for one project.

func New

func New(p *project.Project, opts Options) (*Runner, error)

New builds a Runner, loading env files and the session.

func (*Runner) AuthSource

func (r *Runner) AuthSource(req *httpfile.Request) (raw, source string)

AuthSource reports the auth spec template for describe: the raw text and where it was declared.

func (*Runner) Capture

func (r *Runner) Capture(name, value string)

Capture stores a value in the capture layer, below --var and shell overrides and above the environment files, exactly like `# @capture`.

func (*Runner) Captured

func (r *Runner) Captured() map[string]string

Captured returns a copy of the values captured during this run.

func (*Runner) Describe

func (r *Runner) Describe(req *httpfile.Request) *Description

Describe reports a request's variables and where each comes from.

func (*Runner) EnvVars

func (r *Runner) EnvVars() []VarInfo

EnvVar lists the effective variables for the current environment, for `apic env`.

func (*Runner) MissingError

func (r *Runner) MissingError(req *httpfile.Request, missing []string) error

MissingError explains unresolved variables with a hint on how to provide them.

func (*Runner) Render

func (r *Runner) Render(s string) (string, error)

Render substitutes {{placeholders}} in arbitrary text using the runner's variables (no file-level @vars, since no request is in scope).

func (*Runner) Resolve

func (r *Runner) Resolve(req *httpfile.Request) (*Resolved, error)

Resolve substitutes variables in a request without sending it.

func (*Runner) Results

func (r *Runner) Results() map[string]*Result

Results returns the named responses of this run, for `{{name.response...}}` references.

func (*Runner) Run

func (r *Runner) Run(ctx context.Context, req *httpfile.Request) (*Result, error)

Run resolves, sends and evaluates a single request. A non-nil error is a usage or transport problem; assertion failures are reported in Result.OK.

func (*Runner) RunAll

func (r *Runner) RunAll(ctx context.Context, reqs []*httpfile.Request) ([]*Result, error)

RunAll runs requests in order as a flow, stopping at the first failure unless KeepGoing is set. Results for requests that ran are always returned.

func (*Runner) SetResult

func (r *Runner) SetResult(name string, res *Result)

SetResult registers a named response, e.g. one carried over from another runner.

func (*Runner) SetVar

func (r *Runner) SetVar(name, value string)

SetVar adds or overrides a variable at --var precedence.

type TransportError

type TransportError struct{ Err error }

TransportError is a network-level failure.

func (*TransportError) Error

func (e *TransportError) Error() string

func (*TransportError) Unwrap

func (e *TransportError) Unwrap() error

type UsageError

type UsageError struct{ Msg string }

UsageError is a problem with the request definition or invocation.

func (*UsageError) Error

func (e *UsageError) Error() string

type VarInfo

type VarInfo struct {
	Name       string `json:"name"`
	Value      string `json:"value,omitempty"`
	Source     string `json:"source"`
	Secret     bool   `json:"secret,omitempty"`
	Missing    bool   `json:"missing,omitempty"`
	CapturedBy string `json:"captured_by,omitempty"` // request that would provide it
}

VarInfo describes where a variable's value came from.

Jump to

Keyboard shortcuts

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