Global Output Formatting & Execution Modes
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
The IPATool CLI implements a centralized system for managing output formatting and execution modes across all commands. This system ensures consistent user experiences whether the tool is used manually by a human or integrated into automated pipelines. By decoupling the business logic from the presentation layer, the tool supports multiple output formats such as plain text and structured JSON, alongside specialized execution modes like non-interactive and verbose logging.
The architecture relies on persistent flags defined at the root command level, which are then propagated through a dependency injection pattern to sub-commands. This allows for global control over how information is logged, how errors are reported, and how the tool interacts with the host operating system's environment.
Sources: cmd/root.go:21-45, cmd/common.go:42-56, README.md:58-65
Execution Modes
IPATool supports several execution modes that alter its behavior during runtime. These are primarily controlled via persistent flags available to every command in the suite.
Interactive vs. Non-Interactive
By default, IPATool operates in an interactive mode, which may prompt the user for input (e.g., keychain passphrases or 2FA codes). For automation, the --non-interactive flag disables these prompts. If a required piece of information (like a keychain passphrase) is missing in non-interactive mode, the tool will exit with an error rather than waiting for user input.
Sources: cmd/root.go:44, cmd/common.go:76-78, README.md:154-156
Verbose Logging
The --verbose flag enables detailed logging, including stack traces for errors and internal state transitions. When enabled, the logger captures and outputs more granular information to assist in debugging.
Sources: cmd/root.go:43, cmd/common.go:42-56, cmd/root.go:68-81
Execution Mode Logic Flow
The following diagram illustrates how execution modes are determined during the tool's initialization phase:
The initialization process captures the interaction state in a Go context and passes it to the dependency injection container.
Sources: cmd/root.go:34-39, cmd/common.go:94-110
Output Formatting
Output formatting is controlled via the --format flag, which accepts text or json. This flag determines how the log.Logger and command results are rendered to the user.
Supported Formats
| Format | Implementation | Typical Use Case |
|---|---|---|
text | log.NewWriter() | Human-readable terminal output with colors and status messages. |
json | zerolog.SyncWriter(os.Stdout) | Machine-readable structured data for scripting and integration. |
Sources: cmd/common.go:42-56, cmd/root.go:40-42
Object Marshalling for JSON
To support the json format, domain objects (like App or Account) implement custom marshalling interfaces. For example, the App struct implements MarshalZerologObject to ensure consistent field names and types when outputting to JSON.
Sources: pkg/appstore/app_test.go:41-62
Dependency Initialization Sequence
The sequence below shows how the output format and logging level are established before a command executes:
This sequence ensures that by the time any business logic runs, the output stream is already configured to the user's preferred format.
Sources: cmd/common.go:94-110, cmd/root.go:37-39
Global Flags Summary
The following table summarizes the global flags that govern execution and formatting:
| Flag | Type | Default | Description |
|---|---|---|---|
--format | string | text | Sets output format; can be 'text' or 'json'. |
--non-interactive | bool | false | Disables interactive prompts. |
--verbose | bool | false | Enables verbose logging and stack traces. |
--keychain-passphrase | string | "" | Passphrase for unlocking the local credential store. |
Sources: cmd/root.go:40-45, README.md:58-65
Error Handling and Reporting
IPATool uses a centralized error reporting mechanism that respects the global output settings. If a command fails, the Execute function catches the error and uses the configured logger to report it.
- Standard Errors: Reported with a "success: false" field in JSON or a red error message in Text mode.
- AppStore Errors: These include additional metadata (e.g., failure types or customer messages from Apple) which are extracted and logged as structured fields if the logger is in verbose mode or JSON format.
Sources: cmd/root.go:61-86, cmd/common.go:42-56
Conclusion
The Global Output Formatting and Execution Modes system in IPATool provides a robust foundation for both CLI usability and automation compatibility. By leveraging a centralized initialization routine and standardized logging interfaces, the project maintains consistency across diverse commands such as auth, search, and download. This architectural choice allows developers to add new commands easily without reinventing the reporting or interaction logic.
Sources: AGENTS.md:10-15, cmd/root.go:47-52