Adding New Commands
Relevant source files
The following files were used as context for generating this wiki page:
- cmd/root.go
- cmd/common.go
- cmd/auth.go
- pkg/appstore/appstore_download.go
- AGENTS.md
- main.go (referenced via entry point documentation)
Introduction
Adding new commands to ipatool involves extending the Command Line Interface (CLI) built on the Cobra library. The architecture follows a modular pattern where command definitions are located in the cmd/ directory and business logic is encapsulated within packages under pkg/, primarily pkg/appstore.
The development workflow emphasizes idiomatic Go, consistent CLI behavior, and preservation of backward compatibility for flags and output formats. All new commands must integrate with the central dependency injection system and respect global flags such as --format, --verbose, and --non-interactive.
Sources: AGENTS.md:6-21, cmd/root.go:21-43
Command Architecture
The CLI structure is hierarchical, starting from a root command and branching into specific sub-commands. Each command is responsible for parsing its own flags and invoking the necessary business logic through initialized dependencies.
Command Definition and Registration
New commands are defined as functions returning a *cobra.Command pointer. These functions are then registered in root.go using the AddCommand method.
The diagram above shows the hierarchy of commands within the ipatool application.
Sources: cmd/root.go:45-51, cmd/auth.go:21-31
Dependency Injection
ipatool uses a global dependencies struct to manage shared resources like the logger, keychain, and App Store client. New commands must call initWithCommand(cmd) during their execution phase (usually in PersistentPreRun or at the start of RunE) to ensure these resources are properly instantiated.
| Dependency | Purpose | Source |
|---|---|---|
Logger | Handles formatted text and JSON logging | cmd/common.go:34-49 |
Keychain | Manages Apple ID credentials and tokens | cmd/common.go:61-98 |
AppStore | Provides the interface for App Store interactions | cmd/common.go:101-119 |
CookieJar | Maintains persistent session cookies | cmd/common.go:56-60 |
Sources: cmd/common.go:22-30, cmd/common.go:101-119
Implementing a New Command
Step 1: Define the Command
Create a new file in the cmd/ directory (e.g., cmd/mycommand.go). Define a function that configures the cobra.Command struct, including its use string, short description, and execution logic.
func myCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "my-command",
Short: "Description of the command",
RunE: func(cmd *cobra.Command, args []string) error {
// Initialization and Logic
return nil
},
}
return cmd
}
Sources: cmd/auth.go:34-45, cmd/root.go:21-35
Step 2: Handle Input and Flags
Commands use flags to receive user input. Flags can be local to the command or persistent for sub-commands. Standard global flags are already defined in the root command.
| Flag Type | Purpose | Implementation Example |
|---|---|---|
| Persistent | Available to all sub-commands | cmd.PersistentFlags().BoolVar(...) |
| Local | Specific to one command | cmd.Flags().StringVarP(...) |
| Required | Forces user to provide value | cmd.MarkFlagRequired("name") |
Sources: cmd/root.go:45-51, cmd/auth.go:119-123
Step 3: Integrate with AppStore Logic
Most commands interact with the App Store. Logic should be added to the AppStore interface in pkg/appstore and then invoked from the command's RunE function.
This sequence shows the typical flow from a CLI command through the dependency layer to the business logic.
Sources: cmd/common.go:101-119, cmd/auth.go:76-88, pkg/appstore/appstore_download.go:28-35
Error Handling and Logging
Commands should return errors via RunE to allow the root executor to handle them. The system distinguishes between standard errors and appstore.Error, which may contain additional metadata for verbose logging.
- Interactive Mode: If a command requires sensitive input (like a password) and is running in an interactive session, it should prompt the user using
term.ReadPassword. - Non-Interactive Mode: If
--non-interactiveis set, the command should fail if required credentials are not provided via flags.
Sources: cmd/root.go:64-83, cmd/auth.go:48-60, cmd/common.go:75-85
Summary
Adding a new command to ipatool requires defining the command in the cmd/ package, registering it in root.go, and ensuring it leverages the existing dependency injection for logging, authentication, and App Store communication. By following these patterns, new features remain consistent with the tool's architecture and provide a unified experience for the user.