Skip to content

Commit 9c16584

Browse files
committed
Add support for installing josh-proxy
1 parent eb7d065 commit 9c16584

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

src/bin/josh_sync.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use anyhow::Context;
22
use clap::Parser;
33
use josh_sync::JoshConfig;
4+
use josh_sync::josh::{JoshProxy, try_install_josh};
5+
use std::path::{Path, PathBuf};
46

57
const DEFAULT_CONFIG_PATH: &str = "josh-sync.toml";
68

@@ -14,6 +16,12 @@ struct Args {
1416
enum Command {
1517
/// Initialize a config file for this repository.
1618
Init,
19+
/// Pull changes from the main `rustc` repository.
20+
/// This creates new commits that should be then merged into this subtree repository.
21+
Pull {
22+
#[clap(long, default_value(DEFAULT_CONFIG_PATH))]
23+
config: PathBuf,
24+
},
1725
}
1826

1927
fn main() -> anyhow::Result<()> {
@@ -27,8 +35,40 @@ fn main() -> anyhow::Result<()> {
2735
};
2836
let config = toml::to_string_pretty(&config).context("cannot serialize config")?;
2937
std::fs::write(DEFAULT_CONFIG_PATH, config).context("cannot write config")?;
38+
println!("Created config file at {DEFAULT_CONFIG_PATH}");
39+
}
40+
Command::Pull { config } => {
41+
let config = load_config(&config)
42+
.context("cannot load config. Run the `init` command to initialize it.")?;
43+
let josh = get_josh_proxy()?;
3044
}
3145
}
3246

3347
Ok(())
3448
}
49+
50+
fn get_josh_proxy() -> anyhow::Result<JoshProxy> {
51+
match JoshProxy::lookup() {
52+
Some(proxy) => Ok(proxy),
53+
None => {
54+
println!("josh-proxy not found. Do you want to install it? [y/n]");
55+
let mut line = String::new();
56+
std::io::stdin().read_line(&mut line)?;
57+
if line.trim().to_lowercase() == "y" {
58+
match try_install_josh() {
59+
Some(proxy) => Ok(proxy),
60+
None => Err(anyhow::anyhow!("Could not install josh-proxy")),
61+
}
62+
} else {
63+
Err(anyhow::anyhow!("josh-proxy could not be found"))
64+
}
65+
}
66+
}
67+
}
68+
69+
fn load_config(path: &Path) -> anyhow::Result<JoshConfig> {
70+
let data = std::fs::read_to_string(path)
71+
.with_context(|| format!("cannot load config file from {}", path.display()))?;
72+
let config: JoshConfig = toml::from_str(&data).context("cannot load config as TOML")?;
73+
Ok(config)
74+
}

src/josh.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl JoshProxy {
1212
}
1313
}
1414

15-
fn install_josh() -> Option<JoshProxy> {
15+
pub fn try_install_josh() -> Option<JoshProxy> {
1616
check_output(&[
1717
"cargo",
1818
"install",
@@ -21,6 +21,7 @@ fn install_josh() -> Option<JoshProxy> {
2121
"https://github.com/josh-project/josh",
2222
"--tag",
2323
"r24.10.04",
24+
"josh-proxy",
2425
]);
2526
JoshProxy::lookup()
2627
}

0 commit comments

Comments
 (0)