-
Notifications
You must be signed in to change notification settings - Fork 8
rust(feat): sift cli get assets #731
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| use std::process::ExitCode; | ||
|
|
||
| use anyhow::{Context as AnyhowContext, Result}; | ||
| use sift_rs::assets::v1::{ | ||
| ListAssetsRequest, ListAssetsResponse, asset_service_client::AssetServiceClient, | ||
| }; | ||
|
|
||
| use crate::{ | ||
| BIN_NAME, | ||
| cli::{GetAssetArgs, OutputFormats}, | ||
| cmd::Context, | ||
| util::{ | ||
| api::create_grpc_channel, | ||
| app_uri::normalize_app_uri, | ||
| explore_url::build_explore_url, | ||
| tty::{Output, hyperlink, link_style, stdout_is_tty}, | ||
| }, | ||
| }; | ||
|
|
||
| const ASSET_ID_CHAR_LENGTH_WHITESPACE_LENGTH: usize = 38; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should use an ASCII table crate instead
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| pub async fn run(ctx: Context, args: GetAssetArgs) -> Result<ExitCode> { | ||
| let grpc_channel = create_grpc_channel(&ctx)?; | ||
|
|
||
| let ListAssetsResponse { assets, .. } = AssetServiceClient::new(grpc_channel) | ||
| .list_assets(ListAssetsRequest { | ||
| filter: args.filter.unwrap_or_default(), | ||
| order_by: "modified_date desc".to_string(), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be exposed as an option
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| page_size: args.limit.unwrap_or_default(), | ||
| ..Default::default() | ||
| }) | ||
| .await | ||
| .context("failed to list assets")? | ||
| .into_inner(); | ||
| let app_uri = ctx.app_uri.as_deref().and_then(normalize_app_uri); | ||
| let mut output = Output::new(); | ||
| match args.output_format { | ||
| Some(OutputFormats::Json) => { | ||
| output.line(serde_json::to_string_pretty(&assets).context("failed to encode assets")?); | ||
| } | ||
| Some(OutputFormats::Text) | None => { | ||
| if assets.is_empty() { | ||
| output.line("no assets found"); | ||
| } else { | ||
| let linkify = stdout_is_tty(); | ||
|
|
||
| output.line(format_args!( | ||
| "{:<width$}{}", | ||
| "ID", | ||
| "Name", | ||
| width = ASSET_ID_CHAR_LENGTH_WHITESPACE_LENGTH, | ||
| )); | ||
| for asset in &assets { | ||
| let name = match build_explore_url(app_uri, &asset.name, None) { | ||
| Some(url) if linkify => hyperlink(&link_style(&asset.name), &url), | ||
| _ => asset.name.clone(), | ||
| }; | ||
| output.line(format_args!( | ||
| "{:<width$}{name}", | ||
| asset.asset_id, | ||
| width = ASSET_ID_CHAR_LENGTH_WHITESPACE_LENGTH, | ||
| )); | ||
| } | ||
| output.tip(match app_uri { | ||
| Some(host) => format!( | ||
| "View an asset in Explore at {host}/explore?method=single&assets=<NAME>" | ||
| ), | ||
| None => format!( | ||
| "Run `{BIN_NAME} config update --app-uri <SIFT_WEB_ORIGIN>` for Explore \ | ||
| links." | ||
| ), | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| output.print(); | ||
|
|
||
| Ok(ExitCode::SUCCESS) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pub mod assets; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We will be using these in a lot of other
getsubcommands so this should be a reusable struct that we flattenThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#744