Skip to content

Commit cde89d1

Browse files
committed
refactor: move commands into a module hierarchy
fixes #313 (partially?)
1 parent e94bc62 commit cde89d1

29 files changed

+157
-218
lines changed

src/bin/juliainstaller.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -223,9 +223,7 @@ pub fn main() -> Result<()> {
223223
};
224224
use is_terminal::IsTerminal;
225225
use juliaup::{
226-
command_add::run_command_add,
227-
command_default::run_command_default,
228-
command_selfchannel::run_command_selfchannel,
226+
command,
229227
config_file::JuliaupSelfConfig,
230228
get_juliaup_target, get_own_version,
231229
global_paths::get_paths,
@@ -266,12 +264,6 @@ pub fn main() -> Result<()> {
266264

267265
let mut paths = get_paths().with_context(|| "Trying to load all global paths.")?;
268266

269-
use juliaup::{
270-
command_config_backgroundselfupdate::run_command_config_backgroundselfupdate,
271-
command_config_modifypath::run_command_config_modifypath,
272-
command_config_startupselfupdate::run_command_config_startupselfupdate,
273-
command_config_symlinks::run_command_config_symlinks,
274-
};
275267
use log::{debug, info, trace};
276268

277269
println!("{}", style("Welcome to Julia!").bold());
@@ -500,26 +492,32 @@ pub fn main() -> Result<()> {
500492
paths.juliaupselfconfig = self_config_path.clone();
501493
}
502494

503-
run_command_config_backgroundselfupdate(
495+
command::config::background_self_update::run(
504496
Some(install_choices.backgroundselfupdate),
505497
true,
506498
&paths,
507499
)
508500
.unwrap();
509-
run_command_config_startupselfupdate(Some(install_choices.startupselfupdate), true, &paths)
510-
.unwrap();
501+
command::config::startup_self_update::run(
502+
Some(install_choices.startupselfupdate),
503+
true,
504+
&paths,
505+
)
506+
.unwrap();
511507
if install_choices.modifypath {
512508
// We only run this if true so that we don't try to touch the various shell scripts at all
513509
// if this is not selected.
514-
run_command_config_modifypath(Some(install_choices.modifypath), true, &paths).unwrap();
510+
command::config::modify_path::run(Some(install_choices.modifypath), true, &paths).unwrap();
515511
}
516-
run_command_config_symlinks(Some(install_choices.symlinks), true, &paths).unwrap();
517-
run_command_selfchannel(Some(args.juliaup_channel), &paths).unwrap();
518512

519-
run_command_add(&args.default_channel, &paths)
513+
#[cfg(not(windows))]
514+
command::config::symlinks::run(Some(install_choices.symlinks), true, &paths).unwrap();
515+
command::selfchannel::run(Some(args.juliaup_channel), &paths).unwrap();
516+
517+
command::add::run(&args.default_channel, &paths)
520518
.with_context(|| "Failed to run `run_command_add`.")?;
521519

522-
run_command_default(&args.default_channel, &paths)
520+
command::default::run(&args.default_channel, &paths)
523521
.with_context(|| "Failed to run `run_command_default`.")?;
524522

525523
let symlink_path = juliaupselfbin.join("julia");

src/bin/juliaup.rs

Lines changed: 29 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,8 @@
11
use anyhow::{Context, Result};
22
use clap::Parser;
33
use juliaup::cli::{ConfigSubCmd, Juliaup, OverrideSubCmd, SelfSubCmd};
4-
use juliaup::command_api::run_command_api;
5-
use juliaup::command_completions::generate_completion_for_command;
6-
use juliaup::command_config_autoinstall::run_command_config_autoinstall;
7-
#[cfg(not(windows))]
8-
use juliaup::command_config_symlinks::run_command_config_symlinks;
9-
use juliaup::command_config_versionsdbupdate::run_command_config_versionsdbupdate;
10-
use juliaup::command_default::run_command_default;
11-
use juliaup::command_gc::run_command_gc;
12-
use juliaup::command_info::run_command_info;
13-
use juliaup::command_initial_setup_from_launcher::run_command_initial_setup_from_launcher;
14-
use juliaup::command_link::run_command_link;
15-
use juliaup::command_list::run_command_list;
16-
use juliaup::command_override::{run_command_override_status, run_command_override_unset};
17-
use juliaup::command_remove::run_command_remove;
18-
use juliaup::command_selfupdate::run_command_selfupdate;
19-
use juliaup::command_status::run_command_status;
20-
use juliaup::command_update::run_command_update;
21-
use juliaup::command_update_version_db::run_command_update_version_db;
4+
use juliaup::command;
225
use juliaup::global_paths::get_paths;
23-
use juliaup::{command_add::run_command_add, command_override::run_command_override_set};
24-
#[cfg(feature = "selfupdate")]
25-
use juliaup::{
26-
command_config_backgroundselfupdate::run_command_config_backgroundselfupdate,
27-
command_config_modifypath::run_command_config_modifypath,
28-
command_config_startupselfupdate::run_command_config_startupselfupdate,
29-
command_selfchannel::run_command_selfchannel,
30-
};
31-
32-
#[cfg(feature = "selfupdate")]
33-
use juliaup::command_selfuninstall::run_command_selfuninstall;
34-
35-
#[cfg(not(feature = "selfupdate"))]
36-
use juliaup::command_selfuninstall::run_command_selfuninstall_unavailable;
376

387
use log::info;
398

@@ -48,7 +17,7 @@ fn main() -> Result<()> {
4817
.write_style("JULIAUP_LOG_STYLE");
4918
env_logger::init_from_env(env);
5019

51-
#[cfg(feature = "winpkgidentityext")]
20+
#[cfg(all(windows, feature = "winpkgidentityext"))]
5221
{
5322
use windows::Management::Deployment::{AddPackageOptions, PackageManager};
5423

@@ -92,68 +61,68 @@ fn main() -> Result<()> {
9261
let paths = get_paths().with_context(|| "Trying to load all global paths.")?;
9362

9463
match args {
95-
Juliaup::Default { channel } => run_command_default(&channel, &paths),
96-
Juliaup::Add { channel } => run_command_add(&channel, &paths),
97-
Juliaup::Remove { channel } => run_command_remove(&channel, &paths),
98-
Juliaup::Status {} => run_command_status(&paths),
99-
Juliaup::Update { channel } => run_command_update(&channel, &paths),
100-
Juliaup::Gc { prune_linked } => run_command_gc(prune_linked, &paths),
64+
Juliaup::Default { channel } => command::default::run(&channel, &paths),
65+
Juliaup::Add { channel } => command::add::run(&channel, &paths),
66+
Juliaup::Remove { channel } => command::remove::run(&channel, &paths),
67+
Juliaup::Status {} => command::status::run(&paths),
68+
Juliaup::Update { channel } => command::update::run(&channel, &paths),
69+
Juliaup::Gc { prune_linked } => command::gc::run(prune_linked, &paths),
10170
Juliaup::Link {
10271
channel,
10372
target,
10473
args,
105-
} => run_command_link(&channel, &target, &args, &paths),
106-
Juliaup::List {} => run_command_list(&paths),
74+
} => command::link::run(&channel, &target, &args, &paths),
75+
Juliaup::List {} => command::list::run(&paths),
10776
Juliaup::Config(subcmd) => match subcmd {
10877
#[cfg(not(windows))]
10978
ConfigSubCmd::ChannelSymlinks { value } => {
110-
run_command_config_symlinks(value, false, &paths)
79+
command::config::symlinks::run(value, false, &paths)
11180
}
11281
#[cfg(feature = "selfupdate")]
11382
ConfigSubCmd::BackgroundSelfupdateInterval { value } => {
114-
run_command_config_backgroundselfupdate(value, false, &paths)
83+
command::config::background_self_update::run(value, false, &paths)
11584
}
11685
#[cfg(feature = "selfupdate")]
11786
ConfigSubCmd::StartupSelfupdateInterval { value } => {
118-
run_command_config_startupselfupdate(value, false, &paths)
87+
command::config::startup_self_update::run(value, false, &paths)
11988
}
12089
#[cfg(feature = "selfupdate")]
12190
ConfigSubCmd::ModifyPath { value } => {
122-
run_command_config_modifypath(value, false, &paths)
91+
command::config::modify_path::run(value, false, &paths)
12392
}
12493
ConfigSubCmd::VersionsDbUpdateInterval { value } => {
125-
run_command_config_versionsdbupdate(value, false, &paths)
94+
command::config::versionsdb_update::run(value, false, &paths)
12695
}
12796
ConfigSubCmd::AutoInstallChannels { value } => {
128-
run_command_config_autoinstall(value, false, &paths)
97+
command::config::autoinstall::run(value, false, &paths)
12998
}
13099
},
131-
Juliaup::Api { command } => run_command_api(&command, &paths),
132-
Juliaup::InitialSetupFromLauncher {} => run_command_initial_setup_from_launcher(&paths),
133-
Juliaup::UpdateVersionDb {} => run_command_update_version_db(&paths),
100+
Juliaup::Api { command } => command::api::run(&command, &paths),
101+
Juliaup::InitialSetupFromLauncher {} => command::initial_setup_from_launcher::run(&paths),
102+
Juliaup::UpdateVersionDb {} => command::update_versiondb::run(&paths),
134103
Juliaup::OverrideSubCmd(subcmd) => match subcmd {
135-
OverrideSubCmd::Status {} => run_command_override_status(&paths),
104+
OverrideSubCmd::Status {} => command::r#override::status(&paths),
136105
OverrideSubCmd::Set { channel, path } => {
137-
run_command_override_set(&paths, channel, path)
106+
command::r#override::set(&paths, channel, path)
138107
}
139108
OverrideSubCmd::Unset { nonexistent, path } => {
140-
run_command_override_unset(&paths, nonexistent, path)
109+
command::r#override::unset(&paths, nonexistent, path)
141110
}
142111
},
143-
Juliaup::Info {} => run_command_info(&paths),
112+
Juliaup::Info {} => command::info::run(&paths),
144113
#[cfg(feature = "selfupdate")]
145-
Juliaup::SecretSelfUpdate {} => run_command_selfupdate(&paths),
114+
Juliaup::SecretSelfUpdate {} => command::selfupdate::run(&paths),
146115
Juliaup::SelfSubCmd(subcmd) => match subcmd {
147-
SelfSubCmd::Update {} => run_command_selfupdate(&paths),
116+
SelfSubCmd::Update {} => command::selfupdate::run(&paths),
148117
#[cfg(feature = "selfupdate")]
149-
SelfSubCmd::Channel { channel } => run_command_selfchannel(channel, &paths),
118+
SelfSubCmd::Channel { channel } => command::selfchannel::run(channel, &paths),
150119
#[cfg(feature = "selfupdate")]
151-
SelfSubCmd::Uninstall {} => run_command_selfuninstall(&paths),
120+
SelfSubCmd::Uninstall {} => command::selfuninstall::run(&paths),
152121
#[cfg(not(feature = "selfupdate"))]
153-
SelfSubCmd::Uninstall {} => run_command_selfuninstall_unavailable(),
122+
SelfSubCmd::Uninstall {} => command::selfuninstall::unavailable(),
154123
},
155124
Juliaup::Completions { shell } => {
156-
generate_completion_for_command::<Juliaup>(shell, "juliaup")
125+
command::completions::generate::<Juliaup>(shell, "juliaup")
157126
}
158127
}
159128
}

src/command_add.rs renamed to src/command/add.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::versions_file::load_versions_db;
1010
use anyhow::{anyhow, Context, Result};
1111
use regex::Regex;
1212

13-
pub fn run_command_add(channel: &str, paths: &GlobalPaths) -> Result<()> {
13+
pub fn run(channel: &str, paths: &GlobalPaths) -> Result<()> {
1414
// This regex is dynamically compiled, but its runtime is negligible compared to downloading Julia
1515
if Regex::new(r"^(?:pr\d+|nightly|\d+\.\d+-nightly)(?:~|$)")
1616
.unwrap()

src/command_api.rs renamed to src/command/api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub struct JuliaupApiGetinfoReturn {
2929
pub other_versions: Vec<JuliaupChannelInfo>,
3030
}
3131

32-
pub fn run_command_api(command: &str, paths: &GlobalPaths) -> Result<()> {
32+
pub fn run(command: &str, paths: &GlobalPaths) -> Result<()> {
3333
if command != "getconfig1" {
3434
bail!("Wrong API command.");
3535
}

src/command_completions.rs renamed to src/command/completions.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::cli;
22
use anyhow::Result;
33
use clap::CommandFactory;
4-
use clap_complete::{generate, Shell};
4+
use clap_complete::Shell;
55
use cli::CompletionShell;
66
use std::io;
77

@@ -25,10 +25,8 @@ impl From<CompletionShell> for GeneratorType {
2525
}
2626

2727
/// Generic completion generator that supports both standard shells and nushell
28-
pub fn generate_completion_for_command<T: CommandFactory>(
29-
shell: CompletionShell,
30-
app_name: &str,
31-
) -> Result<()> {
28+
pub fn generate<T: CommandFactory>(shell: CompletionShell, app_name: &str) -> Result<()> {
29+
use clap_complete::generate;
3230
let mut cmd = T::command();
3331
let mut stdout = io::stdout().lock();
3432

src/command_config_autoinstall.rs renamed to src/command/config/autoinstall.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
use anyhow::{anyhow, Result};
1+
use crate::config_file::{load_config_db, load_mut_config_db, save_config_db};
2+
use crate::utils::{print_juliaup_style, JuliaupMessageType};
3+
use anyhow::{bail, Context};
24

3-
pub fn run_command_config_autoinstall(
5+
pub fn run(
46
value: Option<String>,
57
quiet: bool,
68
paths: &crate::global_paths::GlobalPaths,
7-
) -> Result<()> {
8-
use crate::config_file::{load_config_db, load_mut_config_db, save_config_db};
9-
use crate::utils::{print_juliaup_style, JuliaupMessageType};
10-
use anyhow::Context;
11-
9+
) -> anyhow::Result<()> {
1210
match value {
1311
Some(value_str) => {
1412
let mut config_file = load_mut_config_db(paths)
@@ -19,12 +17,10 @@ pub fn run_command_config_autoinstall(
1917
"true" => Some(true),
2018
"false" => Some(false),
2119
"default" => None,
22-
_ => {
23-
return Err(anyhow!(
20+
_ => bail!(
2421
"Invalid value '{}'. Valid values are: true, false, default (to unset the property)",
2522
value_str
26-
))
27-
}
23+
),
2824
};
2925

3026
if new_value != config_file.data.settings.auto_install_channels {

src/command_config_backgroundselfupdate.rs renamed to src/command/config/background_self_update.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
#[cfg(feature = "selfupdate")]
1+
use crate::config_file::{load_config_db, load_mut_config_db, save_config_db};
22
use crate::utils::{print_juliaup_style, JuliaupMessageType};
3-
#[cfg(feature = "selfupdate")]
4-
use anyhow::Result;
3+
use anyhow::{bail, Context};
54

6-
#[cfg(feature = "selfupdate")]
7-
pub fn run_command_config_backgroundselfupdate(
5+
pub fn run(
86
value: Option<i64>,
97
quiet: bool,
108
paths: &crate::global_paths::GlobalPaths,
11-
) -> Result<()> {
12-
use crate::config_file::{load_config_db, load_mut_config_db, save_config_db};
9+
) -> anyhow::Result<()> {
1310
use crate::operations::{install_background_selfupdate, uninstall_background_selfupdate};
14-
use anyhow::{bail, Context};
1511

1612
match value {
1713
Some(value) => {

src/command/config/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pub mod autoinstall;
2+
3+
#[cfg(feature = "selfupdate")]
4+
pub mod background_self_update;
5+
6+
#[cfg(feature = "selfupdate")]
7+
pub mod modify_path;
8+
9+
#[cfg(feature = "selfupdate")]
10+
pub mod startup_self_update;
11+
12+
#[cfg(not(windows))]
13+
pub mod symlinks;
14+
15+
pub mod versionsdb_update;

src/command_config_modifypath.rs renamed to src/command/config/modify_path.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
#[cfg(feature = "selfupdate")]
2-
pub fn run_command_config_modifypath(
1+
use crate::config_file::{load_config_db, load_mut_config_db, save_config_db};
2+
use crate::utils::{print_juliaup_style, JuliaupMessageType};
3+
use anyhow::Context;
4+
5+
pub fn run(
36
value: Option<bool>,
47
quiet: bool,
58
paths: &crate::global_paths::GlobalPaths,
69
) -> anyhow::Result<()> {
7-
use crate::config_file::{load_config_db, load_mut_config_db, save_config_db};
810
use crate::operations::{
911
add_binfolder_to_path_in_shell_scripts, remove_binfolder_from_path_in_shell_scripts,
1012
};
11-
use crate::utils::{print_juliaup_style, JuliaupMessageType};
12-
use anyhow::Context;
1313

1414
match value {
1515
Some(value) => {

src/command_config_startupselfupdate.rs renamed to src/command/config/startup_self_update.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
#[cfg(feature = "selfupdate")]
1+
use crate::config_file::{load_config_db, load_mut_config_db, save_config_db};
22
use crate::utils::{print_juliaup_style, JuliaupMessageType};
3-
#[cfg(feature = "selfupdate")]
4-
use anyhow::Result;
3+
use anyhow::{bail, Context};
54

6-
#[cfg(feature = "selfupdate")]
7-
pub fn run_command_config_startupselfupdate(
5+
pub fn run(
86
value: Option<i64>,
97
quiet: bool,
108
paths: &crate::global_paths::GlobalPaths,
11-
) -> Result<()> {
12-
use crate::config_file::{load_config_db, load_mut_config_db, save_config_db};
13-
use anyhow::{bail, Context};
14-
9+
) -> anyhow::Result<()> {
1510
match value {
1611
Some(value) => {
1712
if value < 0 {

0 commit comments

Comments
 (0)