-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathcommand_override.rs
158 lines (138 loc) · 4.15 KB
/
command_override.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
use std::{
env::current_dir,
path::{Path, PathBuf},
};
use anyhow::{bail, Context, Result};
use cli_table::{
format::{Border, HorizontalLine, Separator},
print_stdout, ColorChoice, Table, WithTitle,
};
use itertools::Itertools;
use crate::{
config_file::{load_config_db, load_mut_config_db, save_config_db, JuliaupOverride},
global_paths::GlobalPaths,
};
#[derive(Table)]
struct OverrideRow {
#[table(title = "Path")]
path: String,
#[table(title = "Channel")]
channel: String,
}
pub fn run_command_override_status(paths: &GlobalPaths) -> Result<()> {
let config_file = load_config_db(paths, None)
.with_context(|| "`override status` command failed to load configuration file.")?;
let rows_in_table: Vec<_> = config_file
.data
.overrides
.iter()
.sorted_by_key(|i| i.path.to_string())
.map(|i| -> OverrideRow {
OverrideRow {
path: dunce::simplified(&PathBuf::from(&i.path))
.to_string_lossy()
.to_string(),
channel: i.channel.to_string(),
}
})
.collect();
print_stdout(
rows_in_table
.with_title()
.color_choice(ColorChoice::Never)
.border(Border::builder().build())
.separator(
Separator::builder()
.title(Some(HorizontalLine::new('1', '2', '3', '-')))
.build(),
),
)?;
Ok(())
}
pub fn run_command_override_set(
paths: &GlobalPaths,
channel: String,
path: Option<String>,
) -> Result<()> {
let mut config_file = load_mut_config_db(paths)
.with_context(|| "`override set` command failed to load configuration data.")?;
if !config_file.data.installed_channels.contains_key(&channel) {
bail!(
"'{}' is not installed. Please run `juliaup add {}` to install channel or version.",
&channel,
&channel
);
}
let path = match path {
Some(path) => PathBuf::from(path),
None => current_dir()?,
}
.canonicalize()?;
if let Some(i) = config_file
.data
.overrides
.iter_mut()
.find(|i| i.path == path.to_string_lossy())
{
if i.channel == channel {
eprintln!(
"Override already set to '{}' for '{}'.",
channel,
path.to_string_lossy()
);
return Ok(());
} else {
eprintln!(
"Override changed from '{}' to '{}' for '{}'.",
i.channel,
channel,
path.to_string_lossy()
);
i.channel = channel.clone();
save_config_db(&mut config_file).with_context(|| {
"Failed to save configuration file from `override set` command."
})?;
return Ok(());
}
}
config_file.data.overrides.push(JuliaupOverride {
path: path.to_string_lossy().to_string(),
channel: channel.clone(),
});
eprintln!(
"Override set to '{}' for '{}'.",
channel,
path.to_string_lossy()
);
save_config_db(&mut config_file)
.with_context(|| "Failed to save configuration file from `override set` command.")?;
Ok(())
}
pub fn run_command_override_unset(
paths: &GlobalPaths,
nonexistent: bool,
path: Option<String>,
) -> Result<()> {
let mut config_file = load_mut_config_db(paths)
.with_context(|| "`override unset` command failed to load configuration data.")?;
let path = match path {
Some(path) => PathBuf::from(path),
None => current_dir()?,
}
.canonicalize()?;
if nonexistent {
config_file
.data
.overrides
.retain(|x| Path::new(&x.path).is_dir());
} else {
// First remove any duplicates
config_file
.data
.overrides
.retain(|x| Path::new(&x.path) != path);
}
save_config_db(&mut config_file)
.with_context(|| "Failed to save configuration file from `override add` command.")?;
Ok(())
}