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::{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: 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        /// Bond each child of source as a separate bond into target
33        #[arg(long)]
34        contents: bool,
35        /// Give this bond a name for easy reference
36        #[arg(long)]
37        name: Option<String>,
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    },
57
58    /// View or modify configuration
59    Config {
60        #[command(subcommand)]
61        action: ConfigAction,
62    },
63
64    /// Update an existing bond's source or target
65    Update {
66        /// Bond ID (or prefix)
67        id: String,
68        /// New source path
69        #[arg(long)]
70        source: Option<PathBuf>,
71        /// New target path
72        #[arg(long)]
73        target: Option<PathBuf>,
74        /// Set or change the bond's name
75        #[arg(long)]
76        name: Option<String>,
77    },
78
79    /// Move a bond's target to a new directory
80    Migrate {
81        /// Bond name or ID prefix
82        id: String,
83        /// Destination directory (defaults to configured default directory)
84        dest: Option<PathBuf>,
85    },
86
87    /// Read or modify metadata for a bond
88    Metadata {
89        #[command(subcommand)]
90        action: MetadataAction,
91    },
92}
93
94/// 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.
95#[derive(Subcommand)]
96pub enum ConfigAction {
97    /// Get the current value of a config key
98    Get {
99        /// Config key to read (e.g., "default")
100        key: String,
101    },
102    /// Set a config key to a new value
103    Set {
104        /// Config key to set (e.g., "default")
105        key: String,
106        /// New value
107        value: String,
108    },
109}
110
111/// 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.
112#[derive(Subcommand)]
113pub enum MetadataAction {
114    /// Print metadata; pass key to read a single value
115    Get {
116        /// Bond name, full ID, or unique ID prefix
117        id: String,
118        /// Optional metadata key
119        key: Option<String>,
120    },
121    /// Set (upsert) one metadata key/value
122    Set {
123        /// Bond name, full ID, or unique ID prefix
124        id: String,
125        /// Metadata key
126        key: String,
127        /// Metadata value
128        value: String,
129    },
130    /// Remove one metadata key
131    Remove {
132        /// Bond name, full ID, or unique ID prefix
133        id: String,
134        /// Metadata key to remove
135        key: String,
136    },
137}