Skip to main content

bonds_cli/
args.rs

1//! The `args` module defines the command-line argument structures and parsing logic for the bonds CLI application using the `clap` crate. It includes the main `Cli` struct, which represents the overall command-line interface, and the `Commands` enum, which defines the various subcommands that users can execute. Each subcommand may have its own set of arguments and options, allowing for a flexible and user-friendly command-line experience when managing directory bonds (symlinks with tracking).
2
3use clap::{Args, Parser, Subcommand};
4use std::path::PathBuf;
5
6#[derive(Parser)]
7#[command(
8    name = "bond",
9    version,
10    about = "Manage directory bonds (symlinks with tracking)"
11)]
12/// The `Cli` struct defines the command-line interface for the bonds CLI application. It uses the `clap` crate to parse command-line arguments and subcommands. The `db` field allows users to specify a custom path to the database file, while the `command` field represents the specific action that the user wants to perform, such as adding a new bond, listing existing bonds, or managing configuration. Each command is defined as a variant of the `Commands` enum, which further organizes related actions into subcommands for better usability and clarity.
13pub struct Cli {
14    /// Path to the database file (overrides default ~/.bonds/bonds.db)
15    #[arg(long, global = true)]
16    pub db: Option<PathBuf>,
17
18    /// Command to execute
19    #[command(subcommand)]
20    pub command: Option<Commands>,
21}
22
23/// The `Commands` enum defines the various commands that the bonds CLI application supports. Each variant corresponds to a specific action that can be performed on the bonds, such as adding a new bond, listing existing bonds, updating bond information, and managing metadata. Some commands have their own subcommands for more granular actions, such as the `Config` and `Metadata` commands. This structure allows for a clear and organized command-line interface, making it easier for users to understand and use the available functionality of the CLI application.
24#[derive(Subcommand)]
25pub enum Commands {
26    /// Create a new bond from source to target
27    Add {
28        /// The source directory or file to bond
29        source: PathBuf,
30        /// The target location (defaults to current directory + source name)
31        target: Option<PathBuf>,
32        /// Give this bond a name for easy reference
33        #[arg(long)]
34        name: Option<String>,
35        /// Common flags for mutating commands
36        #[command(flatten)]
37        flags: ActionFlags,
38    },
39
40    /// List all bonds
41    List,
42
43    /// Show details of a specific bond
44    Info {
45        /// Bond ID
46        id: String,
47    },
48
49    /// Remove a bond
50    Remove {
51        /// Bond ID
52        id: String,
53        /// Also delete the target directory/file (not just the symlink)
54        #[arg(long)]
55        with_target: bool,
56        /// Common flags for mutating commands
57        #[command(flatten)]
58        flags: ActionFlags,
59    },
60
61    /// View or modify configuration
62    Config {
63        #[command(subcommand)]
64        action: ConfigAction,
65    },
66
67    /// Update an existing bond's source or target
68    Update {
69        /// Bond ID (or prefix)
70        id: String,
71        /// New source path
72        #[arg(long)]
73        source: Option<PathBuf>,
74        /// New target path
75        #[arg(long)]
76        target: Option<PathBuf>,
77        /// Set or change the bond's name
78        #[arg(long)]
79        name: Option<String>,
80        /// Common flags for mutating commands
81        #[command(flatten)]
82        flags: ActionFlags,
83    },
84
85    /// Move a bond's target to a new directory
86    Migrate {
87        /// Bond name or ID prefix
88        id: String,
89        /// Destination directory (defaults to configured default directory)
90        dest: Option<PathBuf>,
91        /// Common flags for mutating commands
92        #[command(flatten)]
93        flags: ActionFlags,
94    },
95
96    /// Read or modify metadata for a bond
97    Metadata {
98        #[command(subcommand)]
99        action: MetadataAction,
100    },
101}
102
103/// Defines common command-line flags that can be reused across multiple mutating commands in the bonds CLI application. The `dry_run` flag allows users to preview the outcome of a command without making any actual changes to the filesystem or database, which is useful for testing and verification. The `verbose` flag enables the printing of extra execution details, providing users with more insight into what the command is doing, which can be helpful for debugging and inspection purposes. By centralizing these flags in a single struct, we can maintain consistency and reduce redundancy across different commands that perform mutations.
104#[derive(Debug, Clone, Args, Default)]
105pub struct ActionFlags {
106    /// Preview outcome without applying filesystem/database changes.
107    #[arg(long)]
108    pub dry_run: bool,
109
110    /// Print extra execution details for debugging/inspection.
111    #[arg(long)]
112    pub verbose: bool,
113}
114
115/// Subcommands for the `config` command, which allows users to get or set configuration values for the bonds CLI application. The `Get` variant retrieves the current value of a specified configuration key, while the `Set` variant updates a configuration key with a new value. This allows users to customize the behavior of the CLI application by modifying its configuration settings.
116#[derive(Subcommand)]
117pub enum ConfigAction {
118    /// Get the current value of a config key
119    Get {
120        /// Config key to read (e.g., "default")
121        key: String,
122    },
123    /// Set a config key to a new value
124    Set {
125        /// Config key to set (e.g., "default")
126        key: String,
127        /// New value
128        value: String,
129    },
130}
131
132/// Subcommands for the `metadata` command, which allows users to get, set, or remove metadata key-value pairs associated with a specific bond. The `Get` variant retrieves the value of a specified metadata key for a given bond, or all metadata if no key is provided. The `Set` variant updates or adds a metadata key-value pair for a specified bond. The `Remove` variant deletes a specific metadata key from a given bond. This functionality enables users to manage additional information about their bonds in a flexible way.
133#[derive(Subcommand)]
134pub enum MetadataAction {
135    /// Print metadata; pass key to read a single value
136    Get {
137        /// Bond name, full ID, or unique ID prefix
138        id: String,
139        /// Optional metadata key
140        key: Option<String>,
141    },
142    /// Set (upsert) one metadata key/value
143    Set {
144        /// Bond name, full ID, or unique ID prefix
145        id: String,
146        /// Metadata key
147        key: String,
148        /// Metadata value
149        value: String,
150    },
151    /// Remove one metadata key
152    Remove {
153        /// Bond name, full ID, or unique ID prefix
154        id: String,
155        /// Metadata key to remove
156        key: String,
157    },
158}