Documentation
¶
Overview ¶
Package cli is the Cobra presentation layer: it turns arguments into use case input, and use case results into output. It never speaks HTTP.
Index ¶
Constants ¶
const EnvHostPassphrase = "MOODLE_HOST_PASSPHRASE"
EnvHostPassphrase supplies the host passphrase without a file, for a container whose environment is the only configuration it has.
const EnvReadOnly = "MOODLE_CLI_READ_ONLY"
EnvReadOnly turns on read-only mode for the whole process.
It is an environment variable as well as a flag because the restriction is usually imposed by whoever starts the process — a CI job, or a harness running an agent — and they do not control every argv the tool is given.
const EnvSession = "MOODLE_SESSION"
EnvSession supplies a browser session for a single run.
It is the only way in on a site that issues no token at all, and like the token variable it exists so CI and headless machines are not required to have a keychain.
const EnvWSToken = "MOODLE_WS_TOKEN"
EnvWSToken supplies a token for a single run, bypassing both the configuration and the keychain.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the assembled command tree.
type BuildInfo ¶
BuildInfo describes the running binary. It is injected by the composition root so this package does not reach for globals.
type CallbackHandler ¶
type CallbackHandler interface {
Scheme() string
Register() (HandlerRegistration, error)
Unregister() error
Status() HandlerRegistration
Serve(context.Context, string) error
}
CallbackHandler is the CLI-facing port for desktop callback management. The concrete Linux implementation is wired by bootstrap.
type Deps ¶
type Deps struct {
ConfigPath string
Auth *auth.Manager
// Handler owns the platform-specific registration and callback process for
// browser login. It is injected so the presentation layer never imports a
// desktop adapter directly.
Handler CallbackHandler
// CredentialStore selects where credentials are kept. Like Backend it is
// a pointer: the composition root builds the credential store before the
// flag has been parsed, and reads the value when one is used.
CredentialStore *secret.Backend
// Verbose turns on redacted HTTP diagnostics on stderr. Like Backend it
// is a pointer: the composition root builds the transport before the flag
// has been parsed, and reads the value when a request is actually made.
Verbose *bool
// Backend overrides the site's own setting for this run: "" leaves the
// site's choice alone, "ws-only" rules out the page and AJAX routes.
//
// It is a pointer because the flag is parsed after every command has been
// built with its copy of these dependencies — the same reason the safety
// mode is shared rather than copied.
Backend *string
// Login decides which sign-in method to use. The order lives in the
// coordinator, not in the methods.
Login *auth.Coordinator
// Courses assembles the course use case for a session. The composition
// root supplies it because deciding which backends exist, and in which
// order, is a wiring decision — and because building them means naming
// the transport, which this layer may not do.
Courses func(*auth.Session, *site.Capabilities) *course.Service
// Assignments assembles the assignment use case. The safety mode is passed
// in per invocation because --dry-run is a property of the command line,
// and the guard that enforces it has to be built with it.
Assignments func(*auth.Session, *site.Capabilities, safety.Mode) *assignment.Service
// Grades assembles the grade use case.
Grades func(*auth.Session, *site.Capabilities) *grade.Service
// Calendar assembles the deadline use case.
Calendar func(*auth.Session, *site.Capabilities) *calendar.Service
// Forums assembles the discussion use case.
Forums func(*auth.Session, *site.Capabilities) *forum.Service
// Quizzes assembles the quiz use case.
Quizzes func(*auth.Session, *site.Capabilities) *quiz.Service
// Files assembles the download use case.
Files func(*auth.Session, *site.Capabilities) *file.Downloader
// API assembles the direct-call escape hatch. The safety mode and the
// caller's acceptance of a write are both per invocation.
API func(session *auth.Session, mode safety.Mode, allowWrite bool) *api.Service
// WSRegistry is the generated union of supported core external functions.
// WS binds that contract to one authenticated Moodle session.
WSRegistry *wsregistry.Registry
WS func(session *auth.Session, mode safety.Mode, allowWrite bool) *wsregistry.Service
Workload func(session *auth.Session, capabilities *site.Capabilities) *workload.Service
// ServeMCP runs the agent server. It is injected rather than built here
// because the server speaks a protocol, and this layer does not.
ServeMCP func(context.Context, MCPSession) error
// ServeMCPHost runs the same server for remote clients. It is injected for
// the same reason: HTTP is a transport, and this layer builds none.
ServeMCPHost func(context.Context, MCPSession, MCPHost) error
// Interactive reports whether there is a person at the other end to
// answer a confirmation prompt. It is injected because deciding that means
// inspecting the real process streams, which this layer does not own.
Interactive func() bool
// NewerRelease reports a release newer than this build. Nil turns the check
// off, which is how a build that is not a release, an opt-out, and a run
// with no person at the terminal are all expressed. It is injected because
// answering means asking the network, which this layer does not do.
NewerRelease func(context.Context) (update.Release, bool)
}
Deps are the collaborators the composition root injects. Commands reach for these instead of constructing their own, so tests can swap any of them.
There is deliberately no Moodle client here: the command layer must not be able to speak the protocol itself.
type HandlerRegistration ¶
type HandlerRegistration struct {
Scheme string
DesktopFile string
ServiceFile string
Executable string
MIMEDefault string
Installed bool
}
HandlerRegistration is the presentation layer's view of an installed browser callback handler.
type MCPHost ¶ added in v0.4.0
type MCPHost struct {
// Addr is where to listen.
Addr string
// PublicURL is the address clients connect to.
PublicURL string
// Passphrase admits a client. Empty asks the host to generate one.
Passphrase string
}
MCPHost is how the host reaches its clients.
type MCPSession ¶
type MCPSession struct {
Session *auth.Session
Capabilities *site.Capabilities
SiteName string
AccountName string
// AllowWrite decides whether the writing tools exist at all.
AllowWrite bool
}
MCPSession is everything the agent server needs for one session. It is bound to one site and one account: letting a tool call choose a different site would make every answer's provenance a question.
type Renderer ¶
type Renderer struct {
Streams Streams
Format Format
Pretty bool
// Fields narrows the JSON data to these fields; empty keeps them all.
Fields []string
// NoInput means nobody may be asked anything: a command that would wait
// for an answer fails instead, so an unattended caller never hangs.
NoInput bool
}
Renderer writes results and errors in the selected format.
func (Renderer) Render ¶
Render writes a successful result.
A partial answer is announced on stderr after the data. Moodle filters a successful reply rather than refusing it — a course the account cannot read, an activity a restriction withholds — and the contract has carried that as meta.partial all along, where only a --json reader ever saw it. A person reading a short list had no way to tell it was short. Stderr keeps the notice out of the data, so a pipe still receives exactly the rows.
func (Renderer) RenderError ¶
RenderError writes a failed result and returns the process exit code.
With --json the error envelope goes to stdout, so a consumer parses one stream and one document whatever the outcome. Without it, the message goes to stderr where humans expect it.
type Result ¶
type Result struct {
Envelope v1.Envelope
Human func(w io.Writer) error
// PartialReason explains a partial answer whose gap is not a field: rows
// the site filtered out of an otherwise successful reply. It reaches only
// the person reading, never the JSON, which says the answer is partial and
// keeps meta.missing to field names.
PartialReason string
}
Result is what a command produces: an envelope plus the human rendering of the same data. Human is only called for FormatTable.
type Streams ¶
type Streams struct {
// In is where a command reads piped input, such as a token or password.
In io.Reader
Out io.Writer
Err io.Writer
}
Streams are the process streams, injected so tests can capture them.
The split is a contract rule, not a convenience: stdout carries only the requested data, stderr only progress and diagnostics.