Skip to content

Commit

Permalink
feat(config): overhaul how scoped registries are provided
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 7, 2023
1 parent ef46e5a commit 31c3ae7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
7 changes: 5 additions & 2 deletions crates/nassun/src/client.rs
Expand Up @@ -49,8 +49,11 @@ impl NassunOpts {

/// Adds a registry to use for a specific scope.
pub fn scope_registry(mut self, scope: impl AsRef<str>, registry: Url) -> Self {
self.registries
.insert(Some(scope.as_ref().into()), registry);
let scope = scope.as_ref();
self.registries.insert(
Some(scope.strip_prefix('@').unwrap_or(scope).to_string()),
registry,
);
self
}

Expand Down
11 changes: 4 additions & 7 deletions crates/oro-config/src/lib.rs
Expand Up @@ -71,13 +71,10 @@ impl OroConfigLayerExt for Command {
}
} else if let Ok(value) = config.get_table(&opt) {
if !args.contains(&OsString::from(format!("--no-{}", opt))) {
args.push(OsString::from(format!("--{}", opt)));
let joined = value
.iter()
.map(|(k, v)| format!("{}={}", k, v))
.collect::<Vec<_>>()
.join(",");
args.push(OsString::from(joined));
for (key, val) in value {
args.push(OsString::from(format!("--{}", opt)));
args.push(OsString::from(format!("{key}={val}")))
}
}
} else if let Ok(value) = config.get_array(&opt) {
if !args.contains(&OsString::from(format!("--no-{}", opt))) {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/restore.rs
@@ -1,4 +1,4 @@
use std::{path::{Path, PathBuf}, collections::HashMap};
use std::path::{Path, PathBuf};

use async_trait::async_trait;
use clap::Args;
Expand Down Expand Up @@ -71,7 +71,7 @@ pub struct RestoreCmd {
registry: Url,

#[arg(from_global)]
scope_registries: HashMap<String, Url>,
scoped_registries: Vec<(String, Url)>,

#[arg(from_global)]
json: bool,
Expand Down Expand Up @@ -172,7 +172,7 @@ impl RestoreCmd {
span.pb_set_message(line);
});

for (scope, registry) in &self.scope_registries {
for (scope, registry) in &self.scoped_registries {
nm = nm.scope_registry(scope, registry.clone());
}

Expand Down
7 changes: 4 additions & 3 deletions src/commands/view.rs
@@ -1,4 +1,4 @@
use std::{path::PathBuf, collections::HashMap};
use std::path::PathBuf;

use async_trait::async_trait;
use clap::Args;
Expand All @@ -24,7 +24,7 @@ pub struct ViewCmd {
registry: Url,

#[arg(from_global)]
scope_registries: HashMap<String, Url>,
scoped_registries: Vec<(String, Url)>,

#[arg(from_global)]
root: Option<PathBuf>,
Expand All @@ -39,8 +39,9 @@ pub struct ViewCmd {
#[async_trait]
impl OroCommand for ViewCmd {
async fn execute(self) -> Result<()> {
dbg!(&self);
let mut nassun_opts = NassunOpts::new().registry(self.registry);
for (scope, registry) in self.scope_registries {
for (scope, registry) in self.scoped_registries {
nassun_opts = nassun_opts.scope_registry(scope, registry);
}
if let Some(root) = self.root {
Expand Down
41 changes: 16 additions & 25 deletions src/lib.rs
Expand Up @@ -35,11 +35,7 @@
//! [CONTRIBUTING.md]: https://github.com/orogene/orogene/blob/main/CONTRIBUTING.md
//! [Apache 2.0 License]: https://github.com/orogene/orogene/blob/main/LICENSE

use std::{
collections::HashMap,
hash::Hash,
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};

use async_trait::async_trait;
use clap::{Args, CommandFactory, FromArgMatches as _, Parser, Subcommand};
Expand Down Expand Up @@ -90,16 +86,18 @@ pub struct Orogene {
)]
registry: Url,

/// Registries used for specific scopes. Supports multiple scopes by
/// joining them with `,`.
/// Registry to use for a specific `@scope`, using `--scoped-registry
/// @scope=https://foo.com` format.
///
/// Can be provided multiple times to specify multiple scoped registries.
#[arg(
help_heading = "Global Options",
global = true,
long,
default_value = "",
value_parser = parse_hash::<String, Url>
alias = "scoped-registries",
long = "scoped-registry",
value_parser = parse_key_value::<String, Url>
)]
scope_registries: HashMap<String, Url>,
scoped_registries: Vec<(String, Url)>,

/// Location of disk cache.
///
Expand Down Expand Up @@ -376,27 +374,20 @@ fn log_command_line() {
tracing::debug!("Running command: {cmd}");
}

fn parse_hash<T, U>(
fn parse_key_value<T, U>(
s: &str,
) -> Result<HashMap<T, U>, Box<dyn std::error::Error + Send + Sync + 'static>>
) -> Result<(T, U), Box<dyn std::error::Error + Send + Sync + 'static>>
where
T: std::str::FromStr + Hash + Eq,
T: std::str::FromStr,
T::Err: std::error::Error + Send + Sync + 'static,
U: std::str::FromStr,
U::Err: std::error::Error + Send + Sync + 'static,
{
let mut hash = HashMap::new();
for pair in s.trim().split(',') {
if pair.is_empty() {
continue;
}
let pos = pair
.find('=')
.ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
let pos = s
.find('=')
.ok_or_else(|| format!("invalid KEY=VALUE pair: no `=` found in `{s}`"))?;

hash.insert(pair[..pos].parse()?, pair[pos + 1..].parse()?);
}
Ok(hash)
Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
}

#[derive(Debug, Subcommand)]
Expand Down

0 comments on commit 31c3ae7

Please sign in to comment.