Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge graphql- and hub-endpoint into api-endpoint. #10

Merged
merged 1 commit into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 3 additions & 30 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ rustyline = { version = "12.0.0", default-features = false }
serde = { version = "1.0.188", features = ["derive"] }
serde_json = { version = "1.0.107", features = ["float_roundtrip", "preserve_order"] }
tokio = { version = "1.32.0", features = ["parking_lot", "rt-multi-thread"] }
toml = { version = "0.8.0", features = ["preserve_order"] }
toml_edit = "0.20.2"
url = { version = "2.4.1", features = ["serde"] }
uuid = { version = "1.4.1", features = ["serde"] }
xxhash-rust = { version = "0.8.7", features = ["xxh3", "const_xxh3"] }
Expand Down
40 changes: 10 additions & 30 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,48 +110,28 @@ pub enum ConfigSubcommand {
/// Print the location of the config file currently used by other hub
/// commands
Path,
/// Set the Hub GraphQL API endpoint
GraphqlEndpoint(ConfigGraphqlEndpoint),
/// Override or reset the Hub root endpoint
HubEndpoint(ConfigHubEndpoint),
/// Refresh the contents of the current config file, updating any
/// deprecated properties
Update,
/// Set the Hub API endpoint
ApiEndpoint(ConfigApiEndpoint),
/// Read a new Hub API token from STDIN
Token,
}

/// Options for hub config graphql-endpoint
/// Options for hub config api-endpoint
#[derive(clap::Args)]
pub struct ConfigGraphqlEndpoint {
/// Print the current GraphQL API endpoint
pub struct ConfigApiEndpoint {
/// Print the current API endpoint
#[arg(long)]
pub get: bool,

/// Specify the GraphQL API endpoint, required if not using a terminal,
/// otherwise STDIN is used as the default
/// Specify the API endpoint, required if not using a terminal, otherwise
/// STDIN is used as the default
#[arg(required = !std::io::stdin().is_terminal(), conflicts_with("get"))]
pub endpoint: Option<String>,
}

/// Options for hub config hub-endpoint
#[derive(clap::Args)]
pub struct ConfigHubEndpoint {
/// Print the current root Hub endpoint
#[arg(long)]
pub get: bool,

/// Reset the endpoint override and infer it from the GraphQL API endpoint
#[arg(short, long, conflicts_with("get"))]
pub reset: bool,

/// Override the root Hub endpoint, required if not using a terminal,
/// otherwise STDIN is used as the default
#[arg(
required = !std::io::stdin().is_terminal(),
conflicts_with("get"),
conflicts_with("reset"),
)]
pub endpoint: Option<String>,
}

/// Options for hub airdrop
#[derive(clap::Args)]
pub struct Airdrop {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/airdrop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ pub fn run(config: &Config, cache: CacheConfig, args: Airdrop) -> Result<()> {
let ctx = Context {
// TODO: what should the correct path for this be?
cache: Cache::load_sync(Path::new(".airdrops").join(drop_id.to_string()), cache)?,
graphql_endpoint: config.graphql_endpoint().clone(),
client: config.graphql_client()?,
graphql_endpoint: config.graphql_endpoint()?,
client: config.api_client()?,
q: tx,
stats: Arc::default(),
};
Expand Down
58 changes: 11 additions & 47 deletions src/commands/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,17 @@ use std::{
use anyhow::{Context, Result};

use crate::{
cli::{Config as Opts, ConfigGraphqlEndpoint, ConfigHubEndpoint, ConfigSubcommand},
cli::{Config as Opts, ConfigApiEndpoint, ConfigSubcommand},
config::{Config, ConfigLocation},
};

fn mutate_config(
config_location: &ConfigLocation,
mutate: impl FnOnce(&mut Config) -> Result<()>,
) -> Result<()> {
let config = config_location.load()?;
let mut next_config = config.clone();
mutate(&mut next_config)?;

if next_config != config {
next_config.save(config_location)?;
}

Ok(())
let mut config = config_location.load()?;
mutate(&mut config)?;
config.save(config_location)
}

pub fn run(config: &ConfigLocation, opts: Opts) -> Result<()> {
Expand All @@ -34,8 +28,8 @@ pub fn run(config: &ConfigLocation, opts: Opts) -> Result<()> {
println!("{}", canon.as_deref().unwrap_or(config.path()).display());
Ok(())
},
ConfigSubcommand::GraphqlEndpoint(e) => mutate_config(config, |c| graphql_endpoint(c, e)),
ConfigSubcommand::HubEndpoint(e) => mutate_config(config, |c| hub_endpoint(c, e)),
ConfigSubcommand::Update => config.load()?.save(config),
ConfigSubcommand::ApiEndpoint(e) => mutate_config(config, |c| api_endpoint(c, e)),
ConfigSubcommand::Token => mutate_config(config, token),
}
}
Expand All @@ -54,51 +48,21 @@ where T::Err: fmt::Display {
}
}

fn graphql_endpoint(config: &mut Config, endpoint: ConfigGraphqlEndpoint) -> Result<()> {
let ConfigGraphqlEndpoint { get, endpoint } = endpoint;
fn api_endpoint(config: &mut Config, endpoint: ConfigApiEndpoint) -> Result<()> {
let ConfigApiEndpoint { get, endpoint } = endpoint;

if get {
println!("{}", config.graphql_endpoint());
println!("{}", config.api_endpoint());
return Ok(());
}

let endpoint = if let Some(e) = endpoint {
e.parse().context("Invalid endpoint URL")?
} else {
read_insecure("Enter new GraphQL endpoint: ", "Invalid URL")?
};

config.set_graphql_endpoint(endpoint);

Ok(())
}

fn hub_endpoint(config: &mut Config, endpoint: ConfigHubEndpoint) -> Result<()> {
let ConfigHubEndpoint {
get,
reset,
endpoint,
} = endpoint;

if get {
println!(
"{}",
config
.hub_endpoint()
.context("Error computing root Hub endpoint from GraphQL endpoint")?
);
return Ok(());
}

let endpoint = if reset {
None
} else if let Some(e) = endpoint {
Some(e.parse().context("Invalid endpoint URL")?)
} else {
Some(read_insecure("Enter new Hub endpoint: ", "Invalid URL")?)
read_insecure("Enter new API endpoint: ", "Invalid URL")?
};

config.set_hub_endpoint(endpoint);
config.set_api_endpoint(endpoint);

Ok(())
}
Expand Down
29 changes: 9 additions & 20 deletions src/commands/upload_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ use crate::{
config::Config,
};

type UploadResponse = Vec<UploadedAsset>;

#[derive(Debug, Serialize, Deserialize)]
struct UploadedAsset {
name: String,
url: Url,
struct UploadResponse {
uri: Url,
cid: String,
}

#[derive(GraphQLQuery)]
Expand Down Expand Up @@ -196,9 +194,9 @@ pub fn run(config: &Config, cache: CacheConfig, args: UploadDrop) -> Result<()>
.into_boxed_slice()
.into(),
drop_id,
graphql_endpoint: config.graphql_endpoint().clone(),
graphql_endpoint: config.graphql_endpoint()?,
upload_endpoint: config.upload_endpoint()?,
client: config.graphql_client()?,
client: config.api_client()?,
q: tx,
stats: Arc::default(),
};
Expand Down Expand Up @@ -475,7 +473,7 @@ impl UploadAssetJob {
.to_string_lossy()
.into_owned();

let mut uploads = ctx
let upload = ctx
.client
.post(ctx.upload_endpoint)
.multipart(
Expand All @@ -499,29 +497,20 @@ impl UploadAssetJob {
.await
.with_context(|| {
format!("Error deserializing upload response JSON for {path:?}")
})?
.into_iter();

if uploads.len() > 1 {
warn!("Trailing values in response data for {path:?}");
}

let upload = uploads
.find(|u| u.name == name)
.with_context(|| format!("Missing upload response data for {path:?}"))?;
})?;

ctx.stats.uploaded_assets.increment();
info!("Successfully uploaded {path:?}");

cache
.set_named(path.clone(), ck, AssetUpload {
url: upload.url.to_string(),
url: upload.uri.to_string(),
})
.await
.map_err(|e| warn!("{e:?}"))
.ok();

dest_url = upload.url;
dest_url = upload.uri;
}

rewrites
Expand Down
Loading