Skip to content

Commit 58ca165

Browse files
committed
feat(bestool): download from tailscale proxies when available
1 parent 7075b4a commit 58ca165

File tree

8 files changed

+158
-125
lines changed

8 files changed

+158
-125
lines changed

Cargo.lock

+4-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bestool/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ aws-credential-types = { version = "1.1.7", features = [
2323
aws-sdk-route53 = { version = "1.58.0", optional = true }
2424
aws-sdk-sts = { version = "1.54.1", optional = true }
2525
base64ct = { version = "1.6.0", features = ["std"], optional = true }
26-
binstalk-downloader = { version = "0.13.8", optional = true }
26+
binstalk-downloader = { version = "0.13.8", optional = true, git = "https://github.com/cargo-bins/cargo-binstall", branch = "feat/downloader-custom-client" }
2727
bitflags = { version = "2.7.0", optional = true }
2828
bitvec = { version = "1.0.1", optional = true }
2929
blake3 = { version = "1.5.5", optional = true }
@@ -58,7 +58,7 @@ itertools = { version = "0.14.0", optional = true }
5858
json5 = { version = "0.4.1", optional = true }
5959
leon = { version = "3.0.1", optional = true }
6060
leon-macros = { version = "1.0.2", optional = true }
61-
lloggs = "1.0.0"
61+
lloggs = "1.0.1"
6262
local-ip-address = { version = "0.6.1", optional = true }
6363
mailgun-rs = { version = "1.0.0", optional = true }
6464
merkle_hash = { version = "3.7.0", optional = true }

crates/bestool/src/actions/caddy/download.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
use std::{
2-
iter,
3-
num::{NonZeroU16, NonZeroU64},
4-
path::PathBuf,
5-
};
1+
use std::path::PathBuf;
62

7-
use binstalk_downloader::{
8-
download::{Download, PkgFmt},
9-
remote::{Client, Url},
10-
};
3+
use binstalk_downloader::download::{Download, PkgFmt};
114
use clap::Parser;
125
use detect_targets::get_desired_targets;
136
use miette::{bail, IntoDiagnostic, Result};
147
use tracing::{debug, info};
158

16-
use crate::actions::Context;
9+
use crate::{
10+
actions::Context,
11+
download::{client, DownloadSource},
12+
};
1713

1814
use super::CaddyArgs;
1915

@@ -57,27 +53,23 @@ pub async fn run(ctx: Context<CaddyArgs, DownloadArgs>) -> Result<()> {
5753
let detected_targets = get_desired_targets(target.map(|t| vec![t]));
5854
let detected_targets = detected_targets.get().await;
5955

60-
let client = Client::new(
61-
crate::APP_NAME,
62-
None,
63-
NonZeroU16::new(1).unwrap(),
64-
NonZeroU64::new(1).unwrap(),
65-
iter::empty(),
66-
)
67-
.into_diagnostic()?;
56+
let client = client().await?;
57+
58+
let host = DownloadSource::Tools.host();
6859

6960
let mut url = None;
7061
for target in detected_targets {
71-
let try_url = Url::parse(&format!(
72-
"https://tools.ops.tamanu.io/caddy/{version}/caddy-{target}{ext}?bust={date}",
73-
ext = if target.contains("windows") {
74-
".exe"
75-
} else {
76-
""
77-
},
78-
date = chrono::Utc::now(),
79-
))
80-
.into_diagnostic()?;
62+
let try_url = host
63+
.join(&format!(
64+
"/caddy/{version}/caddy-{target}{ext}?bust={date}",
65+
ext = if target.contains("windows") {
66+
".exe"
67+
} else {
68+
""
69+
},
70+
date = chrono::Utc::now(),
71+
))
72+
.into_diagnostic()?;
8173
debug!(url=%try_url, "trying URL");
8274
if client
8375
.remote_gettable(try_url.clone())

crates/bestool/src/actions/self_update.rs

+22-27
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
1-
use std::{
2-
iter,
3-
num::{NonZeroU16, NonZeroU64},
4-
path::PathBuf,
5-
};
1+
use std::path::PathBuf;
62

73
use clap::Parser;
84

9-
use binstalk_downloader::{
10-
download::{Download, PkgFmt},
11-
remote::{Client, Url},
12-
};
5+
use binstalk_downloader::download::{Download, PkgFmt};
136
use detect_targets::{get_desired_targets, TARGET};
147
use miette::{miette, IntoDiagnostic, Result};
158
use tracing::info;
169

10+
use crate::download::{client, DownloadSource};
11+
1712
use super::Context;
1813

1914
/// Update this bestool.
@@ -50,14 +45,7 @@ pub async fn run(ctx: Context<SelfUpdateArgs>) -> Result<()> {
5045
add_to_path,
5146
} = ctx.args_top;
5247

53-
let client = Client::new(
54-
crate::APP_NAME,
55-
None,
56-
NonZeroU16::new(1).unwrap(),
57-
NonZeroU64::new(1).unwrap(),
58-
iter::empty(),
59-
)
60-
.into_diagnostic()?;
48+
let client = client().await?;
6149

6250
let detected_targets = get_desired_targets(target.map(|t| vec![t]));
6351
let detected_targets = detected_targets.get().await;
@@ -69,16 +57,19 @@ pub async fn run(ctx: Context<SelfUpdateArgs>) -> Result<()> {
6957
);
7058
let dest = dir.join(&filename);
7159

72-
let url = format!(
73-
"https://tools.ops.tamanu.io/bestool/{version}/{target}/{filename}",
74-
target = detected_targets
75-
.first()
76-
.cloned()
77-
.unwrap_or_else(|| TARGET.into()),
78-
);
60+
let host = DownloadSource::Tools.host();
61+
let url = host
62+
.join(&format!(
63+
"/bestool/{version}/{target}/{filename}",
64+
target = detected_targets
65+
.first()
66+
.cloned()
67+
.unwrap_or_else(|| TARGET.into()),
68+
))
69+
.into_diagnostic()?;
7970
info!(url = %url, "downloading");
8071

81-
Download::new(client, Url::parse(&url).into_diagnostic()?)
72+
Download::new(client, url)
8273
.and_extract(PkgFmt::Bin, &dest)
8374
.await
8475
.into_diagnostic()?;
@@ -99,8 +90,12 @@ pub async fn run(ctx: Context<SelfUpdateArgs>) -> Result<()> {
9990
#[cfg(windows)]
10091
fn add_self_to_path() -> Result<()> {
10192
let self_path = std::env::current_exe().into_diagnostic()?;
102-
let self_dir = self_path.parent().ok_or_else(|| miette!("current exe is not in a dir?"))?;
103-
let self_dir = self_dir.to_str().ok_or_else(|| miette!("current exe path is not utf-8"))?;
93+
let self_dir = self_path
94+
.parent()
95+
.ok_or_else(|| miette!("current exe is not in a dir?"))?;
96+
let self_dir = self_dir
97+
.to_str()
98+
.ok_or_else(|| miette!("current exe path is not utf-8"))?;
10499

105100
windows_env::prepend("PATH", self_dir).into_diagnostic()?;
106101

crates/bestool/src/actions/tamanu/download.rs

+23-34
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
use std::{
2-
iter,
3-
num::{NonZeroU16, NonZeroU64},
4-
path::{Path, PathBuf},
5-
};
1+
use std::path::PathBuf;
62

73
use binstalk_downloader::{
84
download::{Download, PkgFmt},
9-
remote::{Client, Url},
5+
remote::Url,
106
};
117
use clap::{Parser, ValueEnum};
128
use miette::{IntoDiagnostic, Result};
139

14-
use crate::actions::Context;
10+
use crate::{
11+
actions::Context,
12+
download::{client, DownloadSource},
13+
};
1514

1615
use super::{ApiServerKind, TamanuArgs};
1716

@@ -29,7 +28,10 @@ pub struct DownloadArgs {
2928
pub kind: ServerKind,
3029

3130
/// Version to download.
32-
#[cfg_attr(docsrs, doc("\n\n**2nd Argument**: version (e.g. `bestool tamanu download web 1.2.3`)"))]
31+
#[cfg_attr(
32+
docsrs,
33+
doc("\n\n**2nd Argument**: version (e.g. `bestool tamanu download web 1.2.3`)")
34+
)]
3335
#[arg(value_name = "VERSION")]
3436
pub version: String,
3537

@@ -76,19 +78,25 @@ pub async fn run(ctx: Context<TamanuArgs, DownloadArgs>) -> Result<()> {
7678
url_only,
7779
} = ctx.args_sub;
7880

79-
let url = make_url(kind, version)?;
81+
let url = make_url(kind, version).await?;
8082

8183
if url_only {
8284
println!("{}", url);
8385
return Ok(());
8486
}
8587

86-
download(url, into).await
88+
let client = client().await?;
89+
Download::new(client, url)
90+
.and_extract(PkgFmt::Tzstd, &into)
91+
.await
92+
.into_diagnostic()?;
93+
Ok(())
8794
}
8895

89-
pub fn make_url(kind: ServerKind, version: String) -> Result<Url> {
90-
let url_string = format!(
91-
"https://servers.ops.tamanu.io/{version}/{kind}-{version}{platform}.tar.zst",
96+
pub async fn make_url(kind: ServerKind, version: String) -> Result<Url> {
97+
let host = DownloadSource::Servers.host();
98+
host.join(&format!(
99+
"/{version}/{kind}-{version}{platform}.tar.zst",
92100
kind = match kind {
93101
ServerKind::Central => "central",
94102
ServerKind::Facility => "facility",
@@ -99,25 +107,6 @@ pub fn make_url(kind: ServerKind, version: String) -> Result<Url> {
99107
} else {
100108
"-windows"
101109
},
102-
);
103-
104-
Url::parse(&url_string).into_diagnostic()
105-
}
106-
107-
pub async fn download(url: Url, into: impl AsRef<Path>) -> Result<()> {
108-
let client = Client::new(
109-
crate::APP_NAME,
110-
None,
111-
NonZeroU16::new(1).unwrap(),
112-
NonZeroU64::new(1).unwrap(),
113-
iter::empty(),
114-
)
115-
.into_diagnostic()?;
116-
let download = Download::new(client, url);
117-
download
118-
.and_extract(PkgFmt::Tzstd, into)
119-
.await
120-
.into_diagnostic()?;
121-
122-
Ok(())
110+
))
111+
.into_diagnostic()
123112
}

crates/bestool/src/actions/walg/download.rs

+20-28
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
use std::{
2-
iter,
3-
num::{NonZeroU16, NonZeroU64},
4-
path::PathBuf,
5-
};
1+
use std::path::PathBuf;
62

7-
use binstalk_downloader::{
8-
download::{Download, PkgFmt},
9-
remote::{Client, Url},
10-
};
3+
use binstalk_downloader::download::{Download, PkgFmt};
114
use clap::Parser;
125
use detect_targets::get_desired_targets;
136
use miette::{bail, IntoDiagnostic, Result};
147
use tracing::{debug, info};
158

16-
use crate::actions::Context;
9+
use crate::{
10+
actions::Context,
11+
download::{client, DownloadSource},
12+
};
1713

1814
use super::WalgArgs;
1915

@@ -57,27 +53,23 @@ pub async fn run(ctx: Context<WalgArgs, DownloadArgs>) -> Result<()> {
5753
let detected_targets = get_desired_targets(target.map(|t| vec![t]));
5854
let detected_targets = detected_targets.get().await;
5955

60-
let client = Client::new(
61-
crate::APP_NAME,
62-
None,
63-
NonZeroU16::new(1).unwrap(),
64-
NonZeroU64::new(1).unwrap(),
65-
iter::empty(),
66-
)
67-
.into_diagnostic()?;
56+
let client = client().await?;
57+
58+
let host = DownloadSource::Tools.host();
6859

6960
let mut url = None;
7061
for target in detected_targets {
71-
let try_url = Url::parse(&format!(
72-
"https://tools.ops.tamanu.io/wal-g/{version}/wal-g-{target}{ext}?bust={date}",
73-
ext = if target.contains("windows") {
74-
".exe"
75-
} else {
76-
""
77-
},
78-
date = chrono::Utc::now(),
79-
))
80-
.into_diagnostic()?;
62+
let try_url = host
63+
.join(&format!(
64+
"/wal-g/{version}/wal-g-{target}{ext}?bust={date}",
65+
ext = if target.contains("windows") {
66+
".exe"
67+
} else {
68+
""
69+
},
70+
date = chrono::Utc::now(),
71+
))
72+
.into_diagnostic()?;
8173
debug!(url=%try_url, "trying URL");
8274
if client
8375
.remote_gettable(try_url.clone())

0 commit comments

Comments
 (0)