client

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Overview

Package client builds the ConnectRPC clients the CLI talks through and maps failures onto the exit codes an agent branches on.

Index

Constants

View Source
const (
	// ExitOK is success.
	ExitOK = 0
	// ExitUsage is a bad argument or bad local input.
	ExitUsage = 1
	// ExitUnauthenticated means the token is missing, expired or revoked, or
	// that a device login session is gone. The next step is always the same:
	// run `mxd auth login`.
	ExitUnauthenticated = 2
	// ExitNotFound is a missing resource.
	ExitNotFound = 3
	// ExitPermissionDenied is a valid caller without the permission.
	ExitPermissionDenied = 4
	// ExitServerError is anything the server got wrong.
	ExitServerError = 5
	// ExitTimeout is a deadline or a cancellation.
	ExitTimeout = 6
)

Exit codes. They are part of the CLI's contract: an agent decides what to do next from these numbers, so they are documented and tested.

View Source
const (
	// CodeInvalidArgument is a usage problem without a more specific recovery.
	CodeInvalidArgument = "invalid_argument"
	// CodeServerRequired means no server address is configured yet. It names
	// the one command that fixes it, so it gets its own code.
	CodeServerRequired = "server_required"
	// CodeDeviceSessionExpired means the device login has to be started again.
	CodeDeviceSessionExpired = "device_session_expired"
)

Error codes with a recovery of their own.

Variables

This section is empty.

Functions

func DeviceSessionExpired

func DeviceSessionExpired(reason string) error

DeviceSessionExpired builds the error for a login that has to be restarted.

func ExitCode

func ExitCode(err error) int

ExitCode maps an error onto its exit code. A nil error is success.

Types

type Client

type Client struct {
	// Server is the normalized address the client talks to.
	Server string

	Auth     v1connect.AuthServiceClient
	User     v1connect.UserServiceClient
	Instance v1connect.InstanceServiceClient
	Database v1connect.DatabaseServiceClient
	Lineage  v1connect.LineageServiceClient
}

Client holds one connection per service the CLI uses.

func New

func New(server string, options Options) (*Client, error)

New builds the clients for one server address. Every request to that address carries the bearer token, which is also what exempts the CLI from the cookie-based CSRF protection. The token stays bound to that address: a redirect that leaves it is refused rather than followed.

type Options

type Options struct {
	// Token is sent as a bearer credential. Empty means anonymous.
	Token string
	// CACert is an extra PEM file to trust, for a self-signed deployment.
	CACert string
	// Insecure skips certificate verification. It is opt-in and the caller
	// warns about it.
	Insecure bool
	// Timeout bounds one request.
	Timeout time.Duration
}

Options are the transport settings resolved from flags and the environment.

type SessionExpiredError

type SessionExpiredError struct {
	Reason string
}

SessionExpiredError is a device login that is gone: expired, denied, or no longer held by the server. It is reported as an authentication problem, because the only recovery is to start a new login.

It carries both the exit status and the code string, so the two cannot disagree, and its message is the reason that applies to the path that produced it.

func (*SessionExpiredError) Code

func (*SessionExpiredError) Code() string

Code is the value an agent branches on. It is deliberately not one of the Connect codes: a consumed or denied request is a different situation from a rejected token, even though both end in the same recovery.

func (*SessionExpiredError) Error

func (e *SessionExpiredError) Error() string

type UsageError

type UsageError struct {
	Code    string
	Message string
	Hint    string
}

UsageError is a problem with what the caller asked for, rather than with the server. Commands wrap their input validation in it.

Code is the value an agent branches on. Most usage problems share "invalid_argument", but a few have their own code because the recovery is specific: "server_required" and "scope_required" each name the one command or variable that fixes them.

func ServerRequired

func ServerRequired() *UsageError

ServerRequired is the error for an invocation that has no server address.

It is defined once because two paths need it: building the clients, and the command layer's own check before it gets that far. Two copies had already drifted, so one of them reported a different code than the other.

func Usage

func Usage(format string, args ...any) *UsageError

Usage builds a UsageError.

func (*UsageError) Error

func (e *UsageError) Error() string

func (*UsageError) WithCode

func (e *UsageError) WithCode(code string) *UsageError

WithCode overrides the reported code.

func (*UsageError) WithHint

func (e *UsageError) WithHint(format string, args ...any) *UsageError

WithHint attaches an actionable next step.

Jump to

Keyboard shortcuts

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