cli

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 34 Imported by: 0

Documentation

Overview

Package cli wires the cobra command tree and the process exit codes. It is thin: every command resolves its profile and clients through App and then calls a service or the Graph layer. All output goes through output.Printer, which is what the forbidigo linter enforces.

Index

Constants

This section is empty.

Variables

View Source
var (
	Version = "dev"
	Commit  = "none"
	Date    = "unknown"
)

Version information, injected at build time with

-ldflags "-X github.com/floriscornel/teams-cli/internal/cli.Version=… -X github.com/floriscornel/teams-cli/internal/cli.Commit=… -X github.com/floriscornel/teams-cli/internal/cli.Date=…"

(see the build/install tasks in mise.toml and .goreleaser.yaml).

Functions

func Main

func Main(args []string) int

Main is the process entry point: it runs the CLI, prints the error once and returns the documented exit code. cmd/teams/main.go calls os.Exit with it.

func MainWith

func MainWith(app *App, args []string) int

MainWith runs the CLI on a prepared App. main.go uses Main; the in-process testscript harness uses MainWith so it can install its fakes without any test-only flag or environment variable reaching a release build.

Types

type App

type App struct {
	Stdin  io.Reader
	Stdout io.Writer
	Stderr io.Writer
	// Clock is injectable so relative output and retry math are deterministic.
	Clock clock.Clock
	// Hooks are the in-process test seams (see hooks.go). Production leaves them
	// empty: there is no flag or environment variable that can set them.
	Hooks Hooks

	Printer *output.Printer
	// contains filtered or unexported fields
}

App carries everything a command needs: the streams, the resolved profile and lazily built clients. One App per process run keeps tests able to run the CLI in-process with their own streams and fakes.

func New

func New(stdin io.Reader, stdout, stderr io.Writer) *App

New builds an App around the given streams. The Printer is built here rather than only in Execute so that any App method (confirm, Debugf) is safe to call before a command runs; Execute rebuilds it once the global flags are known.

func (*App) Auth

func (a *App) Auth(ctx context.Context) (*auth.Client, error)

Auth returns the auth session for the selected profile.

func (*App) Choose

func (a *App) Choose(question string, options []string) (int, error)

Choose implements ref.Chooser. The options are printed to stderr so stdout stays parseable, and the answer is a 1-based number.

func (*App) Config

func (a *App) Config() (*config.Config, error)

Config returns the loaded config file, loading it on first use.

func (*App) ConfigFileExists

func (a *App) ConfigFileExists() bool

ConfigFileExists reports whether the config file is on disk.

func (*App) ConfigPath

func (a *App) ConfigPath() string

ConfigPath is the config file location: TEAMS_CONFIG when set, otherwise the platform default. It deliberately does not depend on the selected profile, so loading the config can never recurse into resolving a profile.

func (*App) Effective

func (a *App) Effective() (config.Effective, error)

Effective resolves the profile with every default filled in.

func (*App) Execute

func (a *App) Execute(ctx context.Context, args []string) error

Execute runs the CLI with the given arguments (without the program name).

func (*App) Graph

func (a *App) Graph(ctx context.Context) (*graph.Client, error)

Graph returns a Graph client for the selected profile, building it on first use. TEAMS_ACCESS_TOKEN and the cached account both flow through auth.Client, which is a graph.TokenSource.

func (*App) GraphBaseURL

func (a *App) GraphBaseURL() string

GraphBaseURL is the service root a request goes to, for the few places that have to spell it out in a body: an odata bind URL and a --dry-run document.

func (*App) Interactive

func (a *App) Interactive() bool

Interactive implements ref.Chooser: a name that matches several objects may be disambiguated on a terminal, and only there.

func (*App) Me

func (a *App) Me(ctx context.Context) (graph.Me, error)

Me fetches (and caches for the run) the signed-in user.

func (*App) Paths

func (a *App) Paths() (store.Paths, error)

Paths returns the on-disk layout for the selected profile.

func (*App) Root

func (a *App) Root() *cobra.Command

Root returns the command tree, for the doc generator and tests.

func (*App) SetHooks

func (a *App) SetHooks(h Hooks)

SetHooks installs the test seams on this App.

type Hooks

type Hooks struct {
	// ConfigPath overrides the config file (tests otherwise use TEAMS_CONFIG).
	ConfigPath string
	// GraphBaseURL points the Graph client at fakegraph.
	GraphBaseURL string
	// GraphHTTPClient is the HTTP client for Graph calls.
	GraphHTTPClient *http.Client
	// AuthHTTPClient is MSAL's HTTP client (fakeidp's TLS client).
	AuthHTTPClient *http.Client
	// AuthAuthority overrides the MSAL authority URL (fakeidp's authority).
	AuthAuthority string
	// AuthOpenURL replaces the browser opener (fakeidp's in-process browser).
	AuthOpenURL func(url string) error
	// DisableInstanceDiscovery is required by fakeidp: its authority host is not
	// a trusted Microsoft host, so MSAL would otherwise call instance discovery.
	DisableInstanceDiscovery bool
	// Environ replaces os.Environ() for the auth and config layers.
	Environ []string
	// Sleeper replaces the retry delay so throttling tests stay fast.
	Sleeper graph.Sleeper
	// Recorder observes Graph traffic for the Layer 6 contract validation.
	Recorder graph.Recorder
	// GrantedScopes replaces the scopes read from the token, so tests can
	// exercise the scope gate without a full MSAL login. nil means "read the
	// token".
	GrantedScopes []string
	// UpdateBaseURL points the update check at a fake GitHub API. Empty means
	// the real api.github.com; like the other URLs here there is no flag or
	// environment variable for it, so a released binary cannot be redirected.
	UpdateBaseURL string
	// UpdateHTTPClient is the HTTP client for the update check.
	UpdateHTTPClient *http.Client
}

Hooks are the in-process seams the Layer 5 tests use to point the real CLI at fakegraph and fakeidp.

PLAN.md requires that released binaries expose no test-only URL override: there is deliberately no flag and no environment variable for these fields, so a user cannot set them. The test harness sets them on the App it constructs, and the release smoke test asserts the shipped binary has no such flag.

type Me

type Me struct {
	ID                string `json:"id"`
	DisplayName       string `json:"displayName"`
	UserPrincipalName string `json:"userPrincipalName,omitempty"`
	Mail              string `json:"mail,omitempty"`
}

Me is the signed-in user, as returned by GET /me. Only the fields the CLI shows are modelled; the `--json` schema is exactly this struct.

GET /me needs User.Read, which reaches only the signed-in user - which is all whoami wants (refs/graph/api-reference/v1.0/includes/permissions/user-get-permissions.md:3).

Jump to

Keyboard shortcuts

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