From 16ca38b85ad42f1d6524be3a3d9732e0313942a1 Mon Sep 17 00:00:00 2001 From: Simon Popugaev Date: Fri, 24 Nov 2023 20:50:53 +0300 Subject: [PATCH] template file frm stdin --- src/clients/template.rs | 79 +++++++++++++++++++++++++++-------------- src/template.rs | 9 +++-- 2 files changed, 56 insertions(+), 32 deletions(-) diff --git a/src/clients/template.rs b/src/clients/template.rs index 1559315..1648a85 100644 --- a/src/clients/template.rs +++ b/src/clients/template.rs @@ -1,4 +1,4 @@ -use std::path::PathBuf; +use std::io::Read; use async_trait::async_trait; use golem_client::model::{ @@ -9,7 +9,7 @@ use tokio::fs::File; use tracing::info; use crate::clients::CloudAuthentication; -use crate::model::{GolemError, TemplateName}; +use crate::model::{GolemError, PathBufOrStdin, TemplateName}; use crate::{ProjectId, RawTemplateId}; #[async_trait] @@ -24,13 +24,13 @@ pub trait TemplateClient { &self, project_id: Option, name: TemplateName, - file: PathBuf, + file: PathBufOrStdin, auth: &CloudAuthentication, ) -> Result; async fn update( &self, id: RawTemplateId, - file: PathBuf, + file: PathBufOrStdin, auth: &CloudAuthentication, ) -> Result; } @@ -197,27 +197,39 @@ impl TemplateClient for Templ &self, project_id: Option, name: TemplateName, - path: PathBuf, + path: PathBufOrStdin, auth: &CloudAuthentication, ) -> Result { info!("Adding template {name:?} from {path:?}"); - let file = File::open(path) - .await - .map_err(|e| GolemError(format!("Can't open template file: {e}")))?; let template_name = golem_client::model::TemplateName { value: name.0 }; + let query = TemplateQuery { + project_id: project_id.map(|ProjectId(id)| id), + component_name: template_name, + }; - let template = self - .client - .upload_template( - TemplateQuery { - project_id: project_id.map(|ProjectId(id)| id), - component_name: template_name, - }, - file, - &auth.header(), - ) - .await?; + let template = match path { + PathBufOrStdin::Path(path) => { + let file = File::open(path) + .await + .map_err(|e| GolemError(format!("Can't open template file: {e}")))?; + + self.client + .upload_template(query, file, &auth.header()) + .await? + } + PathBufOrStdin::Stdin => { + let mut bytes = Vec::new(); + + let _ = std::io::stdin() + .read_to_end(&mut bytes) // TODO: steaming request from stdin + .map_err(|e| GolemError(format!("Failed to read stdin: {e:?}")))?; + + self.client + .upload_template(query, bytes, &auth.header()) + .await? + } + }; Ok((&template).into()) } @@ -225,19 +237,32 @@ impl TemplateClient for Templ async fn update( &self, id: RawTemplateId, - path: PathBuf, + path: PathBufOrStdin, auth: &CloudAuthentication, ) -> Result { info!("Updating template {id:?} from {path:?}"); - let file = File::open(path) - .await - .map_err(|e| GolemError(format!("Can't open template file: {e}")))?; + let template = match path { + PathBufOrStdin::Path(path) => { + let file = File::open(path) + .await + .map_err(|e| GolemError(format!("Can't open template file: {e}")))?; + self.client + .update_template(&id.0.to_string(), file, &auth.header()) + .await? + } + PathBufOrStdin::Stdin => { + let mut bytes = Vec::new(); - let template = self - .client - .update_template(&id.0.to_string(), file, &auth.header()) - .await?; + let _ = std::io::stdin() + .read_to_end(&mut bytes) // TODO: steaming request from stdin + .map_err(|e| GolemError(format!("Failed to read stdin: {e:?}")))?; + + self.client + .update_template(&id.0.to_string(), bytes, &auth.header()) + .await? + } + }; Ok((&template).into()) } diff --git a/src/template.rs b/src/template.rs index 3a55ea9..907b40e 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,5 +1,3 @@ -use std::path::PathBuf; - use async_trait::async_trait; use clap::Subcommand; use indoc::formatdoc; @@ -10,7 +8,8 @@ use crate::clients::project::ProjectClient; use crate::clients::template::{TemplateClient, TemplateView}; use crate::clients::CloudAuthentication; use crate::model::{ - GolemError, GolemResult, ProjectId, ProjectRef, RawTemplateId, TemplateIdOrName, TemplateName, + GolemError, GolemResult, PathBufOrStdin, ProjectId, ProjectRef, RawTemplateId, + TemplateIdOrName, TemplateName, }; #[derive(Subcommand, Debug)] @@ -25,7 +24,7 @@ pub enum TemplateSubcommand { template_name: TemplateName, #[arg(value_name = "template-file", value_hint = clap::ValueHint::FilePath)] - template_file: PathBuf, // TODO: validate exists + template_file: PathBufOrStdin, // TODO: validate exists }, #[command()] @@ -34,7 +33,7 @@ pub enum TemplateSubcommand { template_id_or_name: TemplateIdOrName, #[arg(value_name = "template-file", value_hint = clap::ValueHint::FilePath)] - template_file: PathBuf, // TODO: validate exists + template_file: PathBufOrStdin, // TODO: validate exists }, #[command()]