Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #36 from golemcloud/template-file-frm-stdin
Browse files Browse the repository at this point in the history
template file from stdin
  • Loading branch information
senia-psm committed Nov 24, 2023
2 parents b827633 + 16ca38b commit 48a8df8
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 32 deletions.
79 changes: 52 additions & 27 deletions src/clients/template.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::PathBuf;
use std::io::Read;

use async_trait::async_trait;
use golem_client::model::{
Expand All @@ -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]
Expand All @@ -24,13 +24,13 @@ pub trait TemplateClient {
&self,
project_id: Option<ProjectId>,
name: TemplateName,
file: PathBuf,
file: PathBufOrStdin,
auth: &CloudAuthentication,
) -> Result<TemplateView, GolemError>;
async fn update(
&self,
id: RawTemplateId,
file: PathBuf,
file: PathBufOrStdin,
auth: &CloudAuthentication,
) -> Result<TemplateView, GolemError>;
}
Expand Down Expand Up @@ -197,47 +197,72 @@ impl<C: golem_client::template::Template + Sync + Send> TemplateClient for Templ
&self,
project_id: Option<ProjectId>,
name: TemplateName,
path: PathBuf,
path: PathBufOrStdin,
auth: &CloudAuthentication,
) -> Result<TemplateView, GolemError> {
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())
}

async fn update(
&self,
id: RawTemplateId,
path: PathBuf,
path: PathBufOrStdin,
auth: &CloudAuthentication,
) -> Result<TemplateView, GolemError> {
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())
}
Expand Down
9 changes: 4 additions & 5 deletions src/template.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::path::PathBuf;

use async_trait::async_trait;
use clap::Subcommand;
use indoc::formatdoc;
Expand All @@ -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)]
Expand All @@ -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()]
Expand All @@ -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()]
Expand Down

0 comments on commit 48a8df8

Please sign in to comment.