-
Notifications
You must be signed in to change notification settings - Fork 21
/
mod.rs
293 lines (265 loc) · 8.27 KB
/
mod.rs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Command line interface.
mod color_choice;
mod raw;
#[cfg(test)]
mod tests;
use std::env;
use std::io;
use std::path::{Path, PathBuf};
use std::process;
use anyhow::anyhow;
use clap::{IntoApp, Parser};
use clap_complete as complete;
use crate::cli::raw::{Add, RawCommand, RawOpt};
use crate::config::{EditPlugin, GitReference, RawPlugin, Shell};
use crate::context::{log_error, Color, Context, Output, Verbosity};
use crate::lock::LockMode;
use crate::util::build;
/// Parse the command line arguments.
///
/// In the event of failure it will print the error message and quit the program
/// without returning.
pub fn from_args() -> Opt {
Opt::from_raw_opt(RawOpt::parse())
}
/// Resolved command line options with defaults set.
#[derive(Debug)]
pub struct Opt {
/// Global context for use across the entire program.
pub ctx: Context,
/// The subcommand.
pub command: Command,
}
/// The resolved command.
#[derive(Debug)]
pub enum Command {
/// Initialize a new config file.
Init { shell: Option<Shell> },
/// Add a new plugin to the config file.
Add {
name: String,
plugin: Box<EditPlugin>,
},
/// Open up the config file in the default editor.
Edit,
/// Remove a plugin from the config file.
Remove { name: String },
/// Install the plugins sources and generate the lock file.
Lock,
/// Generate and print out the script.
Source,
}
impl Opt {
fn from_raw_opt(raw_opt: RawOpt) -> Self {
let RawOpt {
quiet,
verbose,
color,
home,
data_dir,
config_dir,
config_file,
profile,
command,
} = raw_opt;
let mut lock_mode = None;
let command = match command {
RawCommand::Init { shell } => Command::Init { shell },
RawCommand::Add(add) => {
let (name, plugin) = EditPlugin::from_add(*add);
Command::Add {
name,
plugin: Box::new(plugin),
}
}
RawCommand::Edit => Command::Edit,
RawCommand::Remove { name } => Command::Remove { name },
RawCommand::Lock { update, reinstall } => {
lock_mode = LockMode::from_lock_flags(update, reinstall);
Command::Lock
}
RawCommand::Source {
relock,
update,
reinstall,
} => {
lock_mode = LockMode::from_source_flags(relock, update, reinstall);
Command::Source
}
RawCommand::Completions { shell } => {
let mut app = RawOpt::into_app();
let shell = complete::Shell::from(shell);
clap_complete::generate(shell, &mut app, build::CRATE_NAME, &mut io::stdout());
process::exit(0);
}
RawCommand::Version => {
println!("{} {}", build::CRATE_NAME, build::CRATE_VERBOSE_VERSION);
process::exit(0);
}
};
let verbosity = if quiet {
Verbosity::Quiet
} else if verbose {
Verbosity::Verbose
} else {
Verbosity::Normal
};
let output = Output {
verbosity,
no_color: color.is_no_color(),
};
let home = match home.or_else(home::home_dir).ok_or_else(|| {
anyhow!(
"failed to determine the current user's home directory, try using the `--home` \
option"
)
}) {
Ok(home) => home,
Err(err) => {
log_error(output.no_color, Color::Red, "error", &err);
process::exit(1);
}
};
let (config_dir, data_dir) = resolve_dirs(&home, config_dir, data_dir, output.no_color);
let config_file = config_file.unwrap_or_else(|| config_dir.join("plugins.toml"));
let lock_file = match profile.as_deref() {
Some("") | None => data_dir.join("plugins.lock"),
Some(p) => data_dir.join(&format!("plugins.{}.lock", p)),
};
let clone_dir = data_dir.join("repos");
let download_dir = data_dir.join("downloads");
let ctx = Context {
version: build::CRATE_RELEASE.to_string(),
home,
config_dir,
data_dir,
config_file,
lock_file,
clone_dir,
download_dir,
profile,
output,
lock_mode,
};
Self { ctx, command }
}
}
impl EditPlugin {
fn from_add(add: Add) -> (String, Self) {
let Add {
name,
git,
gist,
github,
remote,
local,
proto,
branch,
rev,
tag,
dir,
uses,
apply,
profiles,
} = add;
let reference = match (branch, rev, tag) {
(Some(s), None, None) => Some(GitReference::Branch(s)),
(None, Some(s), None) => Some(GitReference::Rev(s)),
(None, None, Some(s)) => Some(GitReference::Tag(s)),
(None, None, None) => None,
// this is unreachable because these three options are in the same mutually exclusive
// 'git-reference' CLI group
_ => unreachable!(),
};
(
name,
Self::from(RawPlugin {
git,
gist,
github,
remote,
local,
inline: None,
proto,
reference,
dir,
uses,
apply,
profiles,
rest: None,
}),
)
}
}
impl LockMode {
fn from_lock_flags(update: bool, reinstall: bool) -> Option<Self> {
match (update, reinstall) {
(false, false) => Some(Self::Normal),
(true, false) => Some(Self::Update),
(false, true) => Some(Self::Reinstall),
(true, true) => unreachable!(),
}
}
fn from_source_flags(relock: bool, update: bool, reinstall: bool) -> Option<Self> {
match (relock, update, reinstall) {
(false, false, false) => None,
(true, false, false) => Some(Self::Normal),
(_, true, false) => Some(Self::Update),
(_, false, true) => Some(Self::Reinstall),
(_, true, true) => unreachable!(),
}
}
}
fn resolve_dirs(
home: &Path,
config_dir: Option<PathBuf>,
data_dir: Option<PathBuf>,
no_color: bool,
) -> (PathBuf, PathBuf) {
// TODO: Remove this warning in a later release and stop falling back to
// the old directory.
let err = anyhow!(
r#"using deprecated config file location ~/.sheldon/plugins.toml.
To use the new location move the config file to
~/.config/sheldon/plugins.toml ($XDG_CONFIG_HOME/sheldon/plugins.toml),
~/.sheldon can then be safely deleted.
Or to instead preserve the old behaviour set the following environment variables:
SHELDON_CONFIG_DIR="$HOME/.sheldon"
SHELDON_DATA_DIR="$HOME/.sheldon"
See the release notes at https://github.com/rossmacarthur/sheldon for more information.
"#,
);
let mut using_old = false;
let config_dir = config_dir.unwrap_or_else(|| {
let default = default_config_dir(home);
let old = home.join(".sheldon");
if old.exists() && !default.exists() {
log_error(no_color, Color::Yellow, "warning", &err);
using_old = true;
return old;
}
default
});
let data_dir = data_dir.unwrap_or_else(|| {
let default = default_data_dir(home);
if using_old && !default.exists() {
return config_dir.clone();
}
default
});
(config_dir, data_dir)
}
fn default_config_dir(home: &Path) -> PathBuf {
let mut p = env::var_os("XDG_CONFIG_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| home.join(".config"));
p.push("sheldon");
p
}
fn default_data_dir(home: &Path) -> PathBuf {
let mut p = env::var_os("XDG_DATA_HOME")
.map(PathBuf::from)
.unwrap_or_else(|| home.join(".local/share"));
p.push("sheldon");
p
}