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

template file from stdin #36

Merged
merged 1 commit into from
Nov 24, 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
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
Loading