sqlarfs_cli/
cli.rs

1use std::path::PathBuf;
2
3use clap::{Args, Parser, Subcommand, ValueEnum};
4
5#[derive(Parser, Debug, Clone)]
6#[command(author, version, about)]
7pub struct Cli {
8    #[command(subcommand)]
9    pub command: Commands,
10}
11
12#[derive(Args, Debug, Clone)]
13pub struct Create {
14    /// The files to add to the archive.
15    ///
16    /// These files are put in the root of the archive.
17    pub source: Vec<PathBuf>,
18
19    /// The path of the SQLite archive to create.
20    ///
21    /// This is required when archiving multiple files or when creating an empty archive.
22    #[arg(long, short)]
23    pub archive: Option<PathBuf>,
24
25    /// Follow symbolic links.
26    #[arg(long, default_value = "false", overrides_with = "_no_follow")]
27    pub follow: bool,
28
29    /// Don't follow symbolic links (default).
30    #[arg(long = "no-follow", default_value = "true")]
31    pub _no_follow: bool,
32
33    /// Copy the given directory recursively (default).
34    #[arg(long = "recursive", default_value = "true")]
35    _recursive: bool,
36
37    /// Don't copy the given directory recursively.
38    #[arg(long, default_value = "false", overrides_with = "_recursive")]
39    pub no_recursive: bool,
40
41    /// Preserve file metadata (default).
42    #[arg(long = "preserve", default_value = "true")]
43    _preserve: bool,
44
45    /// Don't preserve file metadata.
46    #[arg(long, default_value = "false", overrides_with = "_preserve")]
47    pub no_preserve: bool,
48}
49
50#[derive(Args, Debug, Clone)]
51pub struct Extract {
52    /// The directory in the filesystem to extract the files into.
53    #[arg(default_value = ".")]
54    pub dest: PathBuf,
55
56    /// The path of the SQLite archive.
57    #[arg(long, short)]
58    pub archive: PathBuf,
59
60    /// The path of a specific file or directory in the archive to extract.
61    ///
62    /// This can be passed multiple times.
63    #[arg(short, long)]
64    pub source: Vec<PathBuf>,
65
66    /// Extract given directory recursively (default).
67    #[arg(long = "recursive", default_value = "true")]
68    _recursive: bool,
69
70    /// Don't extract the given directory recursively.
71    #[arg(long, default_value = "false", overrides_with = "_recursive")]
72    pub no_recursive: bool,
73}
74
75#[derive(Args, Debug, Clone)]
76pub struct Archive {
77    /// The file or directory in the filesystem to archive.
78    pub source: PathBuf,
79
80    /// The destination of the file in the archive.
81    pub dest: Option<PathBuf>,
82
83    /// The path of the SQLite archive.
84    #[arg(long, short)]
85    pub archive: PathBuf,
86
87    /// Follow symbolic links.
88    #[arg(long, default_value = "false", overrides_with = "_no_follow")]
89    pub follow: bool,
90
91    /// Don't follow symbolic links (default).
92    #[arg(long = "no-follow", default_value = "true")]
93    pub _no_follow: bool,
94
95    /// Copy the given directory recursively (default).
96    #[arg(long = "recursive", default_value = "true")]
97    _recursive: bool,
98
99    /// Don't copy the given directory recursively.
100    #[arg(long, default_value = "false", overrides_with = "_recursive")]
101    pub no_recursive: bool,
102
103    /// Preserve file metadata (default).
104    #[arg(long = "preserve", default_value = "true")]
105    _preserve: bool,
106
107    /// Don't preserve file metadata.
108    #[arg(long, default_value = "false", overrides_with = "_preserve")]
109    pub no_preserve: bool,
110}
111
112#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
113pub enum FileType {
114    /// Regular files.
115    File,
116
117    /// Directories.
118    Dir,
119
120    /// Symbolic links.
121    Symlink,
122}
123
124impl From<FileType> for sqlarfs::FileType {
125    fn from(kind: FileType) -> Self {
126        match kind {
127            FileType::File => sqlarfs::FileType::File,
128            FileType::Dir => sqlarfs::FileType::Dir,
129            FileType::Symlink => sqlarfs::FileType::Symlink,
130        }
131    }
132}
133
134#[derive(Args, Debug, Clone)]
135pub struct List {
136    /// Only return descendants of this directory.
137    pub parent: Option<PathBuf>,
138
139    /// The path of the SQLite archive.
140    #[arg(long, short)]
141    pub archive: PathBuf,
142
143    /// Return all descendants (children, grandchildren, etc.) (default).
144    #[arg(long, default_value = "true", conflicts_with = "children")]
145    pub tree: bool,
146
147    /// Only return immediate children.
148    #[arg(long, short, default_value = "false", conflicts_with = "tree")]
149    pub children: bool,
150
151    /// Only return files of this type.
152    #[arg(long, short, value_enum)]
153    pub r#type: Option<FileType>,
154}
155
156#[derive(Args, Debug, Clone)]
157pub struct Remove {
158    /// The path of the file or directory to remove.
159    pub path: PathBuf,
160
161    /// The path of the SQLite archive.
162    #[arg(long, short)]
163    pub archive: PathBuf,
164}
165
166#[derive(Subcommand, Debug, Clone)]
167pub enum Commands {
168    /// Create a new SQLite archive from the given files.
169    #[command(visible_alias = "c")]
170    Create(Create),
171
172    /// Extract file or directories from an archive.
173    ///
174    /// Unless --source passed, this extracts the entire archive.
175    #[command(visible_alias = "ex")]
176    Extract(Extract),
177
178    /// Copy a file or directory into an existing archive.
179    #[command(visible_alias = "ar")]
180    Archive(Archive),
181
182    /// List files in an archive.
183    #[command(visible_alias = "ls")]
184    List(List),
185
186    /// Remove a file or directory from an archive.
187    #[command(visible_alias = "rm")]
188    Remove(Remove),
189}