Skip to content

Commit eb7d065

Browse files
committed
Add function for installing Josh
1 parent 0cfd360 commit eb7d065

File tree

6 files changed

+129
-10
lines changed

6 files changed

+129
-10
lines changed

Cargo.lock

Lines changed: 71 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ anyhow = "1"
88
clap = { version = "4", features = ["derive"] }
99
toml = "0.8"
1010
serde = { version = "1", features = ["derive"] }
11+
which = "8"

src/bin/josh_sync.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
use anyhow::Context;
22
use clap::Parser;
3-
use rustc_josh_sync::JoshConfig;
3+
use josh_sync::JoshConfig;
44

55
const DEFAULT_CONFIG_PATH: &str = "josh-sync.toml";
66

77
#[derive(clap::Parser)]
88
struct Args {
99
#[clap(subcommand)]
10-
cmd: Command
10+
cmd: Command,
1111
}
1212

1313
#[derive(clap::Parser)]
1414
enum Command {
1515
/// Initialize a config file for this repository.
16-
Init
16+
Init,
1717
}
1818

1919
fn main() -> anyhow::Result<()> {

src/josh.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
use crate::utils::check_output;
2+
use std::path::PathBuf;
3+
4+
pub struct JoshProxy {
5+
path: PathBuf,
6+
}
7+
8+
impl JoshProxy {
9+
/// Tries to figure out if `josh-proxy` is installed.
10+
pub fn lookup() -> Option<Self> {
11+
which::which("josh-proxy").ok().map(|path| Self { path })
12+
}
13+
}
14+
15+
fn install_josh() -> Option<JoshProxy> {
16+
check_output(&[
17+
"cargo",
18+
"install",
19+
"--locked",
20+
"--git",
21+
"https://github.com/josh-project/josh",
22+
"--tag",
23+
"r24.10.04",
24+
]);
25+
JoshProxy::lookup()
26+
}

src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
pub mod josh;
2+
mod utils;
3+
14
#[derive(serde::Serialize, serde::Deserialize)]
25
pub struct JoshConfig {
36
#[serde(default = "default_org")]
47
pub org: String,
58
pub repo: String,
69
/// Last SHA of rust-lang/rust that was pulled into this subtree.
710
#[serde(default)]
8-
pub upstream_sha: Option<String>
11+
pub upstream_sha: Option<String>,
912
}
1013

1114
fn default_org() -> String {

src/utils.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::process::{Command, Stdio};
2+
3+
/// Run a command from an array, collecting its output.
4+
pub fn check_output<'a, Args: AsRef<[&'a str]>>(l: Args) -> String {
5+
let l = l.as_ref();
6+
check_output_cfg(l[0], |c| c.args(&l[1..]))
7+
}
8+
9+
/// [`read`] with configuration. All shell helpers print the command and pass stderr.
10+
fn check_output_cfg(prog: &str, f: impl FnOnce(&mut Command) -> &mut Command) -> String {
11+
let mut cmd = Command::new(prog);
12+
cmd.stderr(Stdio::inherit());
13+
f(&mut cmd);
14+
eprintln!("+ {cmd:?}");
15+
let out = cmd.output().expect("command failed");
16+
let stdout = String::from_utf8_lossy(out.stdout.trim_ascii()).to_string();
17+
if !out.status.success() {
18+
panic!(
19+
"Command `{cmd:?}` failed with exit code {:?}. STDOUT:\n{stdout}",
20+
out.status.code()
21+
);
22+
}
23+
stdout
24+
}

0 commit comments

Comments
 (0)