Skip to content

Commit d1b4ad3

Browse files
committed
Use github token if available
1 parent e7a5776 commit d1b4ad3

7 files changed

Lines changed: 65 additions & 22 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
edition = "2024"
33
name = "rops"
4-
version = "0.5.1"
4+
version = "0.5.2"
55

66
[dependencies]
77
base64 = "0.22.1"

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ It will download the latest release from GitHub and place the executable in `$HO
1616
export ROPS_INSTALL_DIR=/your/custom/path
1717
```
1818

19-
If `GITHUB_TOKEN` is set in your environment, the installer will use it to authenticate with the GitHub API and avoid rate limits. You can set it with:
19+
If `GITHUB_TOKEN` is set in your environment, github installers will use it to authenticate with the GitHub API and avoid rate limits. You can set it with:
2020

2121
```bash
2222
export GITHUB_TOKEN=your_token

src/git.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
error::{RopsError, RopsResult},
33
settings::Settings,
4-
utils::{StreamCommand, rimraf},
4+
utils::{Secret, StreamCommand, rimraf},
55
};
66
use reqwest::blocking::Client;
77
use serde::{Deserialize, Serialize};
@@ -15,14 +15,16 @@ pub struct GitSettings {
1515
pub branch: String,
1616
#[serde(default = "GitSettings::get_git_sha")]
1717
pub sha: String,
18+
#[serde(default = "GitSettings::get_github_token", skip_deserializing)]
19+
pub github_token: Option<Secret>,
1820
}
1921

2022
#[derive(Clone, Debug)]
2123
pub struct GithubDownloadRelease {
2224
pub repo: String,
2325
pub file_name: String,
2426
pub client: Client,
25-
pub token: Option<String>,
27+
pub token: Option<Secret>,
2628
pub version: Option<String>,
2729
/// A different download url
2830
pub download_url: Option<String>,
@@ -56,6 +58,10 @@ impl GitSettings {
5658
std::env::var("GIT_DEFAULT_BRANCH").unwrap_or_else(|_| "main".to_string())
5759
}
5860

61+
pub fn get_github_token() -> Option<Secret> {
62+
std::env::var("GITHUB_TOKEN").ok().map(Secret::new)
63+
}
64+
5965
/// Derives the Git SHA by executing `git rev-parse HEAD`.
6066
fn get_git_sha() -> String {
6167
match Command::new("git").arg("rev-parse").arg("HEAD").output() {
@@ -134,25 +140,24 @@ impl GitSettings {
134140
)))
135141
}
136142
}
143+
144+
pub fn release_downloader(&self, repo: &str, file_name: &str) -> GithubDownloadRelease {
145+
GithubDownloadRelease::new(repo, file_name, self.github_token.clone())
146+
}
137147
}
138148

139149
impl GithubDownloadRelease {
140-
pub fn new(repo: &str, file_name: &str) -> Self {
150+
pub fn new(repo: &str, file_name: &str, token: Option<Secret>) -> Self {
141151
Self {
142152
repo: repo.to_string(),
143153
file_name: file_name.to_string(),
144154
client: Client::new(),
145-
token: None,
155+
token,
146156
version: None,
147157
download_url: None,
148158
}
149159
}
150160

151-
pub fn with_token<S: Into<String>>(mut self, token: S) -> Self {
152-
self.token = Some(token.into());
153-
self
154-
}
155-
156161
pub fn with_version<S: Into<String>>(mut self, version: S) -> Self {
157162
self.version = Some(version.into());
158163
self
@@ -166,7 +171,7 @@ impl GithubDownloadRelease {
166171
pub fn request(&self, url: String) -> reqwest::blocking::RequestBuilder {
167172
let mut builder = self.client.get(url).header("User-Agent", "quantmind/rops");
168173
if let Some(ref token) = self.token {
169-
builder = builder.header("Authorization", format!("Bearer {}", token));
174+
builder = builder.header("Authorization", format!("Bearer {}", token.value()));
170175
}
171176
builder
172177
}

src/self_update.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
use crate::git;
2-
use std::env;
3-
41
use crate::{
52
error::{RopsError, RopsResult},
63
settings::Settings,
74
};
85

96
pub fn self_update(settings: &Settings) -> RopsResult<()> {
10-
let github_token = env::var("GITHUB_TOKEN").map_err(|_| {
11-
RopsError::Error("GITHUB_TOKEN not set - add it to the .env file".to_string())
12-
})?;
13-
let installer =
14-
git::GithubDownloadRelease::new("quantmind/devops", "rops-{arch}").with_token(github_token);
7+
let installer = settings
8+
.git
9+
.release_downloader("quantmind/rops", "rops-{arch}");
1510
let asset = installer.download(settings)?;
1611

1712
self_replace::self_replace(&asset.name).map_err(|err| RopsError::Error(err.to_string()))?;

src/tools.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::settings::Settings;
22
use crate::{
33
error::{RopsError, RopsResult},
4-
git::GithubDownloadRelease,
4+
git::{GitSettings, GithubDownloadRelease},
55
utils,
66
};
77
use std::collections::HashMap;
@@ -70,6 +70,7 @@ impl Default for Tools {
7070
GithubDownloadRelease::new(
7171
"helm/helm",
7272
"helm-{version}-{os}-{arch}.tar.gz",
73+
GitSettings::get_github_token(),
7374
)
7475
.with_download_url("https://get.helm.sh"),
7576
),
@@ -80,6 +81,7 @@ impl Default for Tools {
8081
InstallMethod::GithubDownload(GithubDownloadRelease::new(
8182
"derailed/k9s",
8283
"k9s_{os}_{arch}.tar.gz",
84+
GitSettings::get_github_token(),
8385
)),
8486
),
8587
ThirdPartyTool::new(
@@ -88,6 +90,7 @@ impl Default for Tools {
8890
InstallMethod::GithubDownload(GithubDownloadRelease::new(
8991
"tamasfe/taplo",
9092
"taplo-{os}-{arch}.gz",
93+
GitSettings::get_github_token(),
9194
)),
9295
),
9396
ThirdPartyTool::new(
@@ -96,6 +99,7 @@ impl Default for Tools {
9699
InstallMethod::GithubDownload(GithubDownloadRelease::new(
97100
"getsops/sops",
98101
"sops-{version}.{os}.{arch}",
102+
GitSettings::get_github_token(),
99103
)),
100104
),
101105
]

src/utils.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ pub trait FromEnv: Sized {
1111
fn from_env(key: &str, default: Self) -> Self;
1212
}
1313

14+
#[derive(Clone)]
15+
pub struct Secret(String);
16+
1417
/// Implementation for simple String types.
1518
impl FromEnv for String {
1619
fn from_env(key: &str, default: Self) -> Self {
@@ -28,6 +31,42 @@ impl FromEnv for Option<String> {
2831
}
2932
}
3033

34+
impl Secret {
35+
pub fn new(value: String) -> Self {
36+
Self(value)
37+
}
38+
39+
pub fn value(&self) -> &str {
40+
&self.0
41+
}
42+
}
43+
44+
impl std::fmt::Display for Secret {
45+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46+
if self.0.len() <= 3 {
47+
write!(f, "***")
48+
} else {
49+
let prefix = &self.0[..3];
50+
write!(f, "{}***", prefix)
51+
}
52+
}
53+
}
54+
55+
impl serde::Serialize for Secret {
56+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
57+
where
58+
S: serde::Serializer,
59+
{
60+
serializer.serialize_str(&self.to_string())
61+
}
62+
}
63+
64+
impl std::fmt::Debug for Secret {
65+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66+
write!(f, "Secret(\"{}\")", self)
67+
}
68+
}
69+
3170
/// Gets a value from an environment variable, returning a default if not present.
3271
pub fn get_default_from_env<T: FromEnv>(key: &str, default: T) -> T {
3372
T::from_env(key, default)

0 commit comments

Comments
 (0)