Documentation
¶
Index ¶
- Variables
- func Execute() int
- func ExecuteArgs(args []string, deps Deps) int
- func NewRootCommand(deps Deps) *cobra.Command
- func ReadPassword(fd int, fallback io.Reader, prompt string, out io.Writer) (string, error)
- type AttachFunc
- type BridgeFunc
- type Deps
- type ExecFunc
- type ExitCoder
- type LoginFactory
- type Runtime
- type ServeMCPFunc
- type TransferFunc
- type TransferRequest
Constants ¶
This section is empty.
Variables ¶
var ErrTransferSeamLimit = errors.New(
"requires a transfer seam that carries two resolved sessions (relay) or a directory walk (-R)")
ErrTransferSeamLimit reports a transfer shape the frozen CLI transfer seam cannot carry yet (asset-to-asset relay, recursive copy). The seam is TransferRequest: one resolved session plus one xfer.Task; a transfer needing two sessions or a directory walk needs a wider seam first, so callers get the honest boundary instead of a silent workaround.
var Version = "0.1.0-dev"
Version is the release version. It is normally injected at build time with
go build -ldflags "-X github.com/MiFaZhan/jms-client/internal/cli.Version=1.2.3"
but `go install github.com/MiFaZhan/jms-client/cmd/jms@latest` cannot pass ldflags, so the version falls back to the module version Go stamps into the binary (see effectiveVersion). The literal below is only the last resort, for a build that has neither.
Functions ¶
func Execute ¶
func Execute() int
Execute runs the jms command line and returns the process exit code.
It is the single entry point used by cmd/jms. Errors are rendered as a single line on stderr; a non-zero exit code is returned for any failure.
func ExecuteArgs ¶
ExecuteArgs runs the command line with an explicit argument list (nil means os.Args[1:]) and an explicit dependency set. It exists so tests can drive the full command surface with injected fakes.
Cobra is put in silent mode so that this function owns the single place where a failure becomes "Error: <message>" on stderr plus a non-zero exit code: a library error must never surface as a usage dump, a panic or a raw traceback.
func NewRootCommand ¶
NewRootCommand builds the cobra command tree.
The full DESIGN.md「总体架构」 command surface is registered and implemented. An unrecognised subcommand fails with an explicit error rather than printing help and exiting 0, so a script chaining on one cannot mistake a typo for success.
func ReadPassword ¶
ReadPassword reads a password from fd without echo when fd is a terminal, and from the fallback reader otherwise.
The terminal path uses golang.org/x/term; the fallback exists so that `jms config add` works in scripts and CI.
Types ¶
type AttachFunc ¶
AttachFunc connects to a running host.
type BridgeFunc ¶
BridgeFunc runs the ssh-pipe relay and returns the remote exit code.
It matches xfer.RunBridge so the production default is that function. stdout carries the caller's protocol bytes, so nothing may write diagnostics there.
type Deps ¶
type Deps struct {
// ConfigPath overrides the configuration file path. Empty means the
// platform default (or JMS_CONFIG). When set it wins over JMS_CONFIG.
ConfigPath string
// Creds is the credential store chain. Nil means
// config.Chain(config.NewEnvStore(), config.NewKeyringStore()).
Creds config.CredentialStore
// Probe checks address reachability. Nil means endpoint.ProbeWith with
// the server's configured proxy policy (DESIGN.md「代理策略」), which falls back
// to endpoint.TCPProbe when no proxy is configured.
Probe endpoint.ProbeFunc
// State persists the last-good address. Nil means a file store in
// the configuration directory.
State endpoint.StateStore
// Login authenticates against one candidate address and returns the
// session, whose api.Client the command then uses. Nil means the real
// auth.Login.
Login LoginFactory
// PromptPassword reads a password without echoing it. Nil means the
// x/term implementation with a stdin fallback.
PromptPassword func(prompt string) (string, error)
// PromptLine reads one line, used for the username, addresses and
// the optional TOTP secret. Nil means reading from Stdin.
PromptLine func(prompt string) (string, error)
// PromptOTP requests a one-time code when MFA triggers and no secret
// is stored. Nil means reading from Stdin.
PromptOTP func() (string, error)
// Confirm asks a yes/no question. Nil means reading from Stdin.
Confirm func(prompt string) (bool, error)
// Stdin is the input stream for the default prompts.
Stdin io.Reader
// Out receives normal command output.
Out io.Writer
// Err receives diagnostics and error messages.
Err io.Writer
// Now returns the current time; overridable in tests so probe
// durations are deterministic.
Now func() time.Time
// Runtime carries the shared pools, the event bus and the seams the
// pool-backed commands (exec, login, sftp, ssh-pipe, tail, attach,
// log, mcp) use. Nil means WithDefaults builds one.
Runtime *Runtime
}
Deps carries every dependency the command surface needs.
A zero Deps is not usable; call Deps.WithDefaults first, which is what ExecuteArgs and NewRootCommand do.
func (Deps) WithDefaults ¶
WithDefaults returns a copy of d with every nil field filled in.
type ExecFunc ¶
type ExecFunc func(ctx context.Context, srv *config.ServerConfig, sess *auth.Session, assetName, command string, opts connpool.ExecOptions) (transport.Result, error)
ExecFunc runs one command on an asset through the shared pool.
It matches connpool.TerminalPool.Exec so the production default is that method itself.
type ExitCoder ¶
type ExitCoder interface {
ExitCode() int
}
ExitCoder is an error that carries the process exit code the command wants.
It exists because a remote command's exit status is meaningful to the caller: `jms exec host 'test -f /x' && ...` must see the remote status, not a generic 1. ExecuteArgs honours this interface before falling back to the generic failure code.
type LoginFactory ¶
type LoginFactory func(ctx context.Context, srv *config.ServerConfig, baseURL string) (*auth.Session, error)
LoginFactory authenticates against one server address.
It exists so the CLI can build a session without hard-wiring auth.Login, and so tests can inject a fake session. The returned session's Client is what list/search calls use.
type Runtime ¶
type Runtime struct {
// Sessions holds authenticated sessions, keyed by server alias.
Sessions *connpool.SessionPool
// Terminals holds live terminals and serializes execution per terminal.
Terminals *connpool.TerminalPool
// Bus receives audit events; nil disables publication.
Bus *obs.Bus
// Audit is the sink behind Bus; nil means auditing is off or failed.
Audit *obs.AuditSink
// AuditErr records why the audit sink could not be prepared, when it
// could not. The bus stays usable so IPC observation still works.
AuditErr error
// AuditDir is where the per-pid JSONL files live; `tail` and `log show`
// read it, `exec` and `mcp` write through the Bus.
AuditDir string
// Actor names this process in the audit stream ("cli", "mcp", or the
// JMS_ACTOR override), so a reader can tell which surface ran what.
Actor string
// ReaperInterval and IdleTTL drive the background idle reaper. Zero
// means the defaults from DESIGN.md「数值基线」.
ReaperInterval time.Duration
IdleTTL time.Duration
// Exec runs one command through the shared terminal pool. Nil means
// Runtime.Terminals.Exec.
Exec ExecFunc
// Transfer performs one file transfer. Nil means a fresh xfer.Engine
// bound to the resolved asset.
Transfer TransferFunc
// Attach connects to a running host for `jms attach`. Nil means
// ipc.Dial.
Attach AttachFunc
// ServeMCP runs the stdio MCP server. Nil means mcpserver.New(opts).Serve.
ServeMCP ServeMCPFunc
// Bridge runs the stdio relay behind `rsync -e`. Nil means
// xfer.RunBridge.
Bridge BridgeFunc
// contains filtered or unexported fields
}
Runtime carries the long-lived collaborators that the pool-backed commands share, and the seams that make those commands testable without a live JumpServer.
Every field is a seam rather than a concrete pool call, because the pool methods live on concrete structs and a test cannot replace a method. A nil seam means "use the real implementation", matching the rest of Deps.
One Runtime is built per process, so the pools inside it survive across commands. That is the whole point of the rewrite: a second `jms exec` on the same asset reuses the live connection instead of logging in again (DESIGN.md「连接池」).
func (*Runtime) Close ¶ added in v0.1.1
Close flushes the audit log and reports any failure.
It waits for queued events to be delivered first: delivery is asynchronous by design, so a short-lived command would otherwise let its own process exit drop the record of what it did. A stuck subscriber is bounded by drainTimeout rather than hanging the process.
It is idempotent, and safe on a nil Runtime: the command path and the long-lived host both call it, and one of them may run twice.
func (*Runtime) StartReaper ¶ added in v0.1.1
StartReaper runs the idle-terminal reaper in the background until ctx is cancelled.
It is called by the long-lived commands (`mcp`), because that is where pooled terminals actually accumulate: a one-shot command's process exit is its own cleanup. Calling it from a short-lived command would be harmless but pointless.
type ServeMCPFunc ¶
ServeMCPFunc runs the stdio MCP server until the client disconnects.
type TransferFunc ¶
TransferFunc performs one transfer.
type TransferRequest ¶
type TransferRequest struct {
Session *auth.Session
Asset string
Account string
// Task carries the direction, the local and remote paths and the
// verification settings.
Task xfer.Task
// Endpoint is the address kind the session is bound to, for the audit
// record.
Endpoint string
}
TransferRequest is one file transfer, already resolved to a session.