shared

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientFromCmd

func ClientFromCmd(cmd *cobra.Command) (kukeonv1.Client, error)

ClientFromCmd returns a kukeonv1.Client selected by flags/env:

  • --no-daemon (or KUKEON_NO_DAEMON=true): in-process Client backed by a fresh controller.Exec. Requires privileges.
  • default: JSON-RPC Client dialing KUKEON_HOST (unix:///... today).

The caller owns the returned Client and must Close it.

func ControllerFromCmd

func ControllerFromCmd(cmd *cobra.Command) (*controller.Exec, error)

ControllerFromCmd instantiates a controller.Exec configured with the shared persistent flags (run path, containerd socket) used by the parent command.

func FormatValidationErrors

func FormatValidationErrors(validationErrors []*parser.ValidationError) error

FormatValidationErrors formats validation errors into a single error message. If all errors are parsing errors (contain "failed to parse"), it returns a YAML parsing error instead of a validation error.

func GetControllerWithMock

func GetControllerWithMock[T any](
	cmd *cobra.Command,
	mockKey any,
	realController func(*cobra.Command) (T, error),
) (T, error)

GetControllerWithMock is a generic helper to get a controller from context, supporting mock injection via a context key. If a mock is found in the context, it is returned. Otherwise, a real controller is created using ControllerFromCmd. The mockKey should be a unique type used as the context key.

func GetControllerWithMockWrapper

func GetControllerWithMockWrapper[T any](cmd *cobra.Command, mockKey any, wrapper func(*controller.Exec) T) (T, error)

GetControllerWithMockWrapper is a convenience function that wraps GetControllerWithMock to use ControllerFromCmd as the real controller factory.

func IsCleanAttachExit added in v0.4.0

func IsCleanAttachExit(err error) bool

IsCleanAttachExit reports whether err describes a benign session end — either a clean detach or a peer-side close. Both map to exit 0 from the user's perspective (no error is surfaced to the shell); callers that need to differentiate detach-vs-peer-close (e.g. to decide whether to keep a managed cell alive) should use ClassifyAttachExit instead.

func LoggerFromCmd

func LoggerFromCmd(cmd *cobra.Command) (*slog.Logger, error)

LoggerFromCmd extracts the slog logger from the Cobra command context.

func ParseAndValidateDocuments

func ParseAndValidateDocuments(reader io.Reader) ([]parser.Document, []*parser.ValidationError, error)

ParseAndValidateDocuments parses and validates YAML documents from a reader. Returns the parsed documents and any validation errors encountered. If there are validation errors, they are returned as a slice, but the function still returns the successfully parsed documents.

func PickContainer added in v0.4.0

func PickContainer(
	ctx context.Context,
	client kukeonv1.Client,
	realm, space, stack, cell string,
	include func(v1beta1.ContainerSpec) bool,
) (string, error)

PickContainer enumerates the cell's containers via client.ListContainers and returns the single container ID for which include returns true.

`kuke attach` and `kuke log` both need to resolve the implicit container when the operator omits --container; the two disagree on which specs to keep — `kuke attach` requires Attachable=true, `kuke log` accepts non-Attachable too — but the enumeration / sort / error semantics are identical and live here so a future change to one subcommand cannot drift the other.

Callers pass include and decide for themselves whether to exclude Spec.Root (both current callers do).

Errors:

  • errdefs.ErrAttachNoCandidate (wrapped with cell) when no spec passes include.
  • errdefs.ErrAttachAmbiguous (wrapped with cell + sorted candidate list) when more than one passes.

func PrintJSONOrYAML

func PrintJSONOrYAML(cmd *cobra.Command, data interface{}, format string) error

PrintJSONOrYAML prints data in JSON or YAML format. The data parameter should be a struct that can be marshaled.

func ReadFileOrStdin

func ReadFileOrStdin(file string) (io.Reader, func() error, error)

ReadFileOrStdin reads from a file or stdin if file is "-". Returns the reader and a cleanup function that should be called when done. If file is "-", the cleanup function is a no-op.

func RequireRoot added in v0.5.0

func RequireRoot(subcommand string) error

RequireRoot is the fail-fast UID gate used by direct-write subcommands (kuke init, kuke daemon reset, kuke image load --no-daemon, kuke doctor cgroups --probe). When the effective UID is non-zero it returns an error wrapping errdefs.ErrMustRunAsRoot that names the subcommand and suggests re-running under `sudo`, so operators see a clear cause instead of a confusing "operation not permitted" several phases in. Daemon-routed verbs (`kuke get`, `kuke create`, `kuke apply`, `kuke delete`, …) must not call this — those are the supported `kukeon`-group rootless-client path.

func SetGeteuidForTesting added in v0.5.0

func SetGeteuidForTesting(f func() int) func()

SetGeteuidForTesting replaces the euid lookup with f and returns a restore function. Intended only for unit tests in dependent packages (cmd/kuke/init, cmd/kuke/daemon/reset, cmd/kuke/doctor/cgroups, …) that drive the gated entrypoints under both root and non-root paths without forking under a different UID. Not for production use.

Types

type AttachExit added in v0.4.0

type AttachExit int

AttachExit classifies how an in-process sbsh attach loop ended. `kuke attach` and the default-attach branch of `kuke run` both drive the same loop and need to branch on the same three outcomes.

const (
	// AttachExitDetached signals a clean ^]^] (or peer-issued Detach
	// RPC). The remote terminal is still alive and `kuke run --rm`
	// must NOT kill the cell — the operator may want to re-attach.
	AttachExitDetached AttachExit = iota

	// AttachExitPeerClosed signals the remote terminal dropped the
	// connection (workload exited, peer hung up). From the user's
	// perspective the session ended cleanly (exit 0), but `kuke run
	// --rm` should fire KillCell so a long-lived root (e.g.
	// `sleep infinity`) does not pin the cell.
	AttachExitPeerClosed

	// AttachExitError signals an unrecoverable controller error
	// (control socket lost, RPC failure, context cancel, …). The
	// caller surfaces the error to the user and `kuke run --rm`
	// fires KillCell so a half-detached session does not leak the
	// cell.
	AttachExitError
)

func ClassifyAttachExit added in v0.4.0

func ClassifyAttachExit(err error) AttachExit

ClassifyAttachExit maps the error returned by `github.com/eminwux/sbsh/pkg/attach`.Run to an AttachExit. nil and detach map to AttachExitDetached / AttachExitPeerClosed respectively; any other non-nil error is AttachExitError.

sbsh v0.10.1 made attach.ErrDetached and attach.ErrPeerClosed public (sbsh#192) so embedders can branch on errors.Is rather than the pre-v0.10.1 substring match on "close requested: read/write routines exited".

Jump to

Keyboard shortcuts

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