Skip to content

Commit

Permalink
renaming Option '--token' to '--auth'
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsgr authored and htngr committed Dec 21, 2024
1 parent cfb62fd commit 05cec6e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
4 changes: 2 additions & 2 deletions codchi/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,13 +620,13 @@ codchi module set <MACHINE_NAME> <MODULE_NAME> --url=my-project-name
#[arg(long, short = 'p')]
pub use_nixpkgs: Option<NixpkgsLocation>,

/// Authorisation token for private repositories. Generally, the syntax is `<user>:<token>`
/// Authorisation string for private repositories. Generally, the syntax is `<user>:<token>`
/// or just `<token>`.
///
/// GitHub for example has something like `ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx`
/// whereas GitLab uses `oauth2:glpat-xxxxxxxxxxxxxxxxxxxx`.
#[arg(long, short)]
pub token: Option<String>,
pub auth: Option<String>,

/// The git branch to use for the code machine module.
#[arg(long, short)]
Expand Down
35 changes: 17 additions & 18 deletions codchi/src/config/flake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum FlakeLocation {
/// domain:port combination
host: String,
repo: String,
token: Option<String>,
auth: Option<String>,
},
Local {
/// relative to /home/codchi, no leading '/' or '~'
Expand All @@ -53,7 +53,7 @@ impl<W: Hkd> FlakeUrl<W> {
scheme,
host,
repo,
token,
auth,
} => {
let query = metadata_to_query(&[
("commit", self.commit.as_ref()),
Expand All @@ -65,12 +65,12 @@ impl<W: Hkd> FlakeUrl<W> {
format!("{scheme}:{repo}?{query}")
}
Http | Https | Ssh => {
let auth = if let Some(token) = &token {
format!("{token}@")
let auth_host_prefix = if let Some(auth) = &auth {
format!("{auth}@")
} else {
String::new()
};
format!("git+{scheme}://{auth}{host}/{repo}?{query}",)
format!("git+{scheme}://{auth_host_prefix}{host}/{repo}?{query}",)
}
}
}
Expand All @@ -91,7 +91,7 @@ impl<W: Hkd> FlakeUrl<W> {
scheme,
host,
repo,
token,
auth,
} => {
let scheme = match scheme {
FlakeScheme::Http => Scheme::Http,
Expand All @@ -103,13 +103,12 @@ impl<W: Hkd> FlakeUrl<W> {
Some(name) => (name.to_owned(), true),
None => (suffixed_name.to_owned(), false),
};
let (user, token) = token
let (user, auth) = auth
.as_ref()
.map(|token| {
token
.split_once(':')
.map(|auth| {
auth.split_once(':')
.map(|(user, pwd)| (Some(user.to_owned()), Some(pwd.to_owned())))
.unwrap_or((Some(token.to_owned()), None))
.unwrap_or((Some(auth.to_owned()), None))
})
.unwrap_or((None, None));
// let repo = repo.strip_suffix(".git").unwrap_or(repo).to_string();
Expand All @@ -122,7 +121,7 @@ impl<W: Hkd> FlakeUrl<W> {
git_suffix,
path: format!("/{repo}"),
user,
token,
token: auth,
organization: None,
port: None,
scheme_prefix: true,
Expand Down Expand Up @@ -153,7 +152,7 @@ impl<W: Hkd> FlakeUrl<W> {
scheme: _,
host,
repo,
token: _,
auth: _,
} => format!("{host}/{repo}"),
FlakeLocation::Local { path } => format!("~/{path}"),
}
Expand All @@ -167,10 +166,10 @@ impl<W: Hkd> Display for FlakeUrl<W> {
scheme,
host,
repo,
token,
auth,
} => {
let metadata = metadata_to_query(&[
("token", token.as_ref()),
("token", auth.as_ref()),
("commit", self.commit.as_ref()),
("ref", self.r#ref.as_ref()),
]);
Expand Down Expand Up @@ -251,7 +250,7 @@ impl FromStr for FlakeUrl<Optional> {
scheme,
host: host.to_string(),
repo: repo.to_string(),
token: metadata.get("token").cloned(),
auth: metadata.get("token").cloned(),
}
};

Expand Down Expand Up @@ -321,7 +320,7 @@ mod tests {
scheme: FlakeScheme::Github,
host: "github.com".to_owned(),
repo: "aformatik/codchi".to_owned(),
token: None,
auth: None,
},
commit: None,
r#ref: None,
Expand All @@ -334,7 +333,7 @@ mod tests {
scheme: FlakeScheme::Https,
host: "foo.bar".to_owned(),
repo: "aformatik/codchi.git".to_owned(),
token: Some("my:token".to_owned()),
auth: Some("my:token".to_owned()),
},
commit: Some("jakfkl2".to_owned()),
r#ref: Some("my-branch".to_owned()),
Expand Down
24 changes: 12 additions & 12 deletions codchi/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn set(
if opts.tag.is_none()
&& opts.commit.is_none()
&& opts.branch.is_none()
&& opts.token.is_none()
&& opts.auth.is_none()
&& opts.use_nixpkgs.is_none()
&& new_name.is_none()
&& module_path.is_none()
Expand All @@ -137,12 +137,12 @@ pub fn set(
if cfg.nixpkgs_from.as_ref() == Some(module_name) {
cfg.nixpkgs_from = Some(new_name.clone());
}
let old_token = match old.location.clone() {
FlakeLocation::Remote { token, .. } => token,
let old_auth = match old.location.clone() {
FlakeLocation::Remote { auth, .. } => auth,
FlakeLocation::Local { .. } => None,
};
let (url, new_module_path, opts) = if let Some(url) = url {
// if the URL changed, prompt if branch, token etc... stay the same
// if the URL changed, prompt if branch, auth etc... stay the same
let keep_or_edit = |name: &str, value: Option<String>| -> Result<Option<String>> {
let Some(value) = value else { return Ok(None) };
if opts.dont_prompt {
Expand Down Expand Up @@ -178,10 +178,10 @@ pub fn set(
dont_prompt: opts.dont_prompt,
use_nixpkgs: opts.use_nixpkgs.clone(), // is prompted inside fetch_module
no_build: opts.no_build,
token: opts
.token
auth: opts
.auth
.clone()
.map_or_else(|| keep_or_edit("token", old_token), |x| Ok(Some(x)))?,
.map_or_else(|| keep_or_edit("auth", old_auth), |x| Ok(Some(x)))?,
branch: opts
.branch
.clone()
Expand Down Expand Up @@ -210,7 +210,7 @@ pub fn set(
}
}),
no_build: opts.no_build,
token: opts.token.clone().or(old_token),
auth: opts.auth.clone().or(old_auth),
branch: opts.branch.clone().or(old.r#ref.clone()),
tag: opts.branch.clone().or(old.r#ref.clone()),
commit: opts.commit.clone().or(old.commit.clone()),
Expand Down Expand Up @@ -478,10 +478,10 @@ fn inquire_module_url(
_ => unreachable!(),
};

// Token in URL / port only work with git+http(s)
// Auth in URL / port only work with git+http(s)
let (host, scheme) = if let Some(port) = url.port {
(format!("{host}:{port}"), fallback_scheme)
} else if opts.token.is_some() {
} else if opts.auth.is_some() {
(host, fallback_scheme)
} else {
let guess = guess_scheme(&host);
Expand Down Expand Up @@ -525,7 +525,7 @@ fn inquire_module_url(
scheme,
host,
repo,
token: opts.token.clone(),
auth: opts.auth.clone(),
},
commit: opts.commit.clone(),
r#ref: opts.branch.as_ref().or(opts.tag.as_ref()).cloned(),
Expand Down Expand Up @@ -613,7 +613,7 @@ pub fn clone(
let git_url = format!(
"{scheme}://{auth}{host}/{repo}",
auth = input_options
.token
.auth
.map(|t| format!("{t}@"))
.unwrap_or_default(),
scheme = git_url.scheme,
Expand Down

0 comments on commit 05cec6e

Please sign in to comment.