CLI Command Structure
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
Too Many Secrets (2ms) is a Go-based CLI tool designed to identify sensitive data like secrets and passwords across various platforms, including Slack, Discord, Confluence, and Git repositories. The CLI architecture is built using the cobra and viper frameworks, providing a structured approach to command execution, flag management, and configuration loading.
The command structure follows a hierarchical pattern where a root command provides global configuration and flags, while specialized sub-commands handle platform-specific scanning logic. This modular design allows the tool to easily extend its capabilities to new platforms through a plugin-based system.
Sources: README.md:14-22, cmd/main.go:51-68
Core Command Hierarchy
The 2ms CLI organizes its functionality into a single entry point with multiple specialized commands. The root command (2ms) manages persistent flags that apply to all operations, while "Scan Commands" are dynamically registered from the available plugins.
Root Command and Persistent Flags
The root command provides the base configuration for the engine, logging, and reporting. It uses viper for environment variable binding and configuration file parsing.
| Flag | Description | Default |
|---|---|---|
--config | Path to the YAML/JSON configuration file | "" |
--log-level | Logging verbosity (trace, debug, info, warn, error, fatal) | "info" |
--report-path | Output paths for scan results (.json, .yaml, .sarif) | [] |
--stdout-format | Format for terminal output | "yaml" |
--validate | Triggers active validation of discovered secrets | false |
--regex | Custom regular expressions to apply during scanning | [] |
Sources: cmd/main.go:66-73, cmd/main.go:102-114, README.md:141-155
Command Initialization Flow
The initialization process involves loading configurations, setting global log levels, and initializing the scanning engine before executing specific plugin logic.
Plugin-Based Scan Commands
The CLI leverages a plugin architecture defined in cmd/main.go. Every plugin implements the IPlugin interface, which includes a DefineCommand method to register itself with the cobra root.
Available Scan Commands
The current implementation includes the following scan commands:
confluence: Scans Confluence spaces and page history.git: Scans local Git repositories including history and branches.filesystem: Scans local directories and files.discord/slack: Scans communication platform histories.paligo: Scans Paligo instance content.
Sources: cmd/main.go:78-85, README.md:128-135
Command Definition Logic
Each plugin defines its own usage, short/long descriptions, and platform-specific flags. For example, the confluence plugin adds flags for URLs, tokens, and space filters.
Execution Lifecycle
The execution of a scan command involves three distinct phases: PreRunE, Run, and PostRunE.
1. Pre-Run (preRun)
In the preRun phase, the CLI validates output formats and initializes the scanning engine. It also spawns worker go-routines for processing items, secrets, and validation/scoring.
- Engine Initialization: Loads rules (selected, ignored, and special).
- Worker Setup: Starts
processItems,processSecrets, andprocessSecretsExtras.
Sources: cmd/main.go:142-171, engine/engine.go:52-73
2. Run (Plugin Execution)
The Run function of the sub-command triggers the specific plugin's scanning logic. Plugins find "items" (files, pages, messages) and push them into the channels.Items channel for the engine to process.
3. Post-Run (postRun)
The postRun phase waits for all asynchronous processing to finish via a sync.WaitGroup. It then generates the final report and writes it to the specified paths or stdout.
Configuration and Flag Binding
2ms supports a hybrid configuration mode. Flags can be set via CLI arguments, environment variables (prefixed with 2MS_), or a configuration file.
- Config File: Loaded using
vConfig.LoadConfig. - Flag Binding: Handled by
utils.BindFlags, which ensures that CLI arguments take precedence over configuration file values. - Format Validation: The
validateFormatfunction ensures that both--stdout-formatand--report-pathextensions match supported formats:json,yaml, orsarif.
Sources: cmd/config.go:17-26, cmd/config.go:45-59, README.md:157-170
Summary
The CLI Command Structure of 2ms is designed for scalability and asynchronous performance. By separating the entry point (root command), the scanning logic (plugins), and the detection logic (engine), the project maintains a clean separation of concerns. The use of Go channels and WaitGroups allows the CLI to process large volumes of data across different platforms simultaneously while maintaining a unified interface for configuration and reporting.
Sources: cmd/main.go:87-92, engine/engine.go:82-85