How cloudfoundry/cli works
Project type and purpose
• This is an open-source, monolithic CLI tool written in Go (the Cloud Foundry CLI).
• It implements two major versions (v7 and v8) of Cloud Foundry API clients, a plugin API, command parsing, and local configuration/state persistence.
High-level architectural layers
a. Presentation / Entrypoint
• main.go → CLI initialization, reads flags, invokes the command loader
• command loader / parser (cf/commandsloader, cf/flagcontext, util/command_parser)
b. Command Registry & Handlers
• cf/commandregistry & cf/commands/v7 + cf/commands (core commands)
• Implements the Command Pattern: each command is a struct with Execute()
c. Actor Layer (Business Logic)
• actor/sharedaction: reusable plumbing (auth, help, target checking)
• actor/v7action & actor/v7pushaction: core push/poll/start/stop logic against CF API
• actor/pluginaction: install/uninstall plugins
• actor/cfnetworkingaction: network policy operations
d. API Client Layer
• api/cloudcontroller (v2 & ccv3), api/uaa, api/router, api/logcache, api/plugin
• Wrap raw HTTP, handle authentication, versioning, error conversion
e. Transport & Helpers
• cf/net & api/*/wrapper: HTTP client, retries, trace headers, request logging
f. Configuration & Persistence
• cf/configuration & util/configv3: read/write ~/.cf/config.json, plugin configs
• manifest & .cfignore parsing (cf/manifest, util/manifestparser)
g. Output / UI
• cf/trace: logging/tracing to stdout or file
• i18n: localization via JSON resource files
h. Plugins Subsystem
• plugin/shim & plugin/rpc: RPC‐based plugin protocol, cf plugin install
• plugin/cli_connection: in‐process plugin invocation
i. External Systems
• Cloud Foundry CC API endpoints (v2/v3), UAA auth, Routing API, Log Cache
• Local disk for config and plugin binaries
• SSH subsystem (cf/ssh, util/clissh)
Relationships & data flow
• User → Terminal → main.go → command parser → command registry
• From a command Execute() → Actor layer (shared / v7 / plugin actor)
• Actors call API Client layer → HTTP Transport → Cloud Foundry back-end services
• Responses flow back up: HTTP → API client → Actor → Command → UI → User
• Configuration reads/writes happen at startup or by specific commands (e.g., cf config)
• Plugin commands are discovered/loaded at startup; calls go through shim → RPC server → plugin process
Architectural patterns & principles
• Command Pattern for extensible CLI commands
• Actor (Service) Layer to encapsulate business logic, isolate from HTTP details
• Adapter/Wrapper around HTTP clients for retry, auth, trace
• Plugin API via RPC for external extension
• Strict separation of concerns: parsing, business logic, transport, UI, config
Technologies & frameworks
• Go modules (go.mod), standard library + minimal external deps
• ginkgo/gomega for testing (numerous _test.go suites)
• JSON‐based i18n and CLI‐help templates
• Makefile, GitHub Actions workflows for build/test/release