1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use std::path::PathBuf;

use clap::{Args, Parser, Subcommand, ValueEnum};

#[derive(Parser, Debug, Clone)]
#[command(author, version, about)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

#[derive(Args, Debug, Clone)]
pub struct Create {
    /// The files to add to the archive.
    ///
    /// These files are put in the root of the archive.
    pub source: Vec<PathBuf>,

    /// The path of the SQLite archive to create.
    ///
    /// This is required when archiving multiple files or when creating an empty archive.
    #[arg(long, short)]
    pub archive: Option<PathBuf>,

    /// Follow symbolic links.
    #[arg(long, default_value = "false", overrides_with = "_no_follow")]
    pub follow: bool,

    /// Don't follow symbolic links (default).
    #[arg(long = "no-follow", default_value = "true")]
    pub _no_follow: bool,

    /// Copy the given directory recursively (default).
    #[arg(long = "recursive", default_value = "true")]
    _recursive: bool,

    /// Don't copy the given directory recursively.
    #[arg(long, default_value = "false", overrides_with = "_recursive")]
    pub no_recursive: bool,

    /// Preserve file metadata (default).
    #[arg(long = "preserve", default_value = "true")]
    _preserve: bool,

    /// Don't preserve file metadata.
    #[arg(long, default_value = "false", overrides_with = "_preserve")]
    pub no_preserve: bool,
}

#[derive(Args, Debug, Clone)]
pub struct Extract {
    /// The directory in the filesystem to extract the files into.
    #[arg(default_value = ".")]
    pub dest: PathBuf,

    /// The path of the SQLite archive.
    #[arg(long, short)]
    pub archive: PathBuf,

    /// The path of a specific file or directory in the archive to extract.
    ///
    /// This can be passed multiple times.
    #[arg(short, long)]
    pub source: Vec<PathBuf>,

    /// Extract given directory recursively (default).
    #[arg(long = "recursive", default_value = "true")]
    _recursive: bool,

    /// Don't extract the given directory recursively.
    #[arg(long, default_value = "false", overrides_with = "_recursive")]
    pub no_recursive: bool,
}

#[derive(Args, Debug, Clone)]
pub struct Archive {
    /// The file or directory in the filesystem to archive.
    pub source: PathBuf,

    /// The destination of the file in the archive.
    pub dest: Option<PathBuf>,

    /// The path of the SQLite archive.
    #[arg(long, short)]
    pub archive: PathBuf,

    /// Follow symbolic links.
    #[arg(long, default_value = "false", overrides_with = "_no_follow")]
    pub follow: bool,

    /// Don't follow symbolic links (default).
    #[arg(long = "no-follow", default_value = "true")]
    pub _no_follow: bool,

    /// Copy the given directory recursively (default).
    #[arg(long = "recursive", default_value = "true")]
    _recursive: bool,

    /// Don't copy the given directory recursively.
    #[arg(long, default_value = "false", overrides_with = "_recursive")]
    pub no_recursive: bool,

    /// Preserve file metadata (default).
    #[arg(long = "preserve", default_value = "true")]
    _preserve: bool,

    /// Don't preserve file metadata.
    #[arg(long, default_value = "false", overrides_with = "_preserve")]
    pub no_preserve: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
pub enum FileType {
    /// Regular files.
    File,

    /// Directories.
    Dir,

    /// Symbolic links.
    Symlink,
}

impl From<FileType> for sqlarfs::FileType {
    fn from(kind: FileType) -> Self {
        match kind {
            FileType::File => sqlarfs::FileType::File,
            FileType::Dir => sqlarfs::FileType::Dir,
            FileType::Symlink => sqlarfs::FileType::Symlink,
        }
    }
}

#[derive(Args, Debug, Clone)]
pub struct List {
    /// Only return descendants of this directory.
    pub parent: Option<PathBuf>,

    /// The path of the SQLite archive.
    #[arg(long, short)]
    pub archive: PathBuf,

    /// Return all descendants (children, grandchildren, etc.) (default).
    #[arg(long, default_value = "true", conflicts_with = "children")]
    pub tree: bool,

    /// Only return immediate children.
    #[arg(long, short, default_value = "false", conflicts_with = "tree")]
    pub children: bool,

    /// Only return files of this type.
    #[arg(long, short, value_enum)]
    pub r#type: Option<FileType>,
}

#[derive(Args, Debug, Clone)]
pub struct Remove {
    /// The path of the file or directory to remove.
    pub path: PathBuf,

    /// The path of the SQLite archive.
    #[arg(long, short)]
    pub archive: PathBuf,
}

#[derive(Subcommand, Debug, Clone)]
pub enum Commands {
    /// Create a new SQLite archive from the given files.
    #[command(visible_alias = "c")]
    Create(Create),

    /// Extract file or directories from an archive.
    ///
    /// Unless --source passed, this extracts the entire archive.
    #[command(visible_alias = "ex")]
    Extract(Extract),

    /// Copy a file or directory into an existing archive.
    #[command(visible_alias = "ar")]
    Archive(Archive),

    /// List files in an archive.
    #[command(visible_alias = "ls")]
    List(List),

    /// Remove a file or directory from an archive.
    #[command(visible_alias = "rm")]
    Remove(Remove),
}