Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
feat(par)!: add par command support to wash-lib
Browse files Browse the repository at this point in the history
* Added par support to wash-lib

Signed-off-by: aish-where-ya <aharpale@cosmonic.com>

* Addressed reveiw comments

Signed-off-by: aish-where-ya <aharpale@cosmonic.com>

* Minor fix

Signed-off-by: aish-where-ya <aharpale@cosmonic.com>

---------

Signed-off-by: aish-where-ya <aharpale@cosmonic.com>
  • Loading branch information
aish-where-ya authored Oct 3, 2023
1 parent 32bef50 commit 90f7944
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 45 deletions.
1 change: 1 addition & 0 deletions crates/wash-lib/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pub mod get;
pub mod inspect;
pub mod link;
pub mod output;
pub mod par;
pub mod registry;
pub mod spy;
pub mod start;
Expand Down
59 changes: 59 additions & 0 deletions crates/wash-lib/src/cli/par.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use anyhow::{anyhow, Context, Result};
use provider_archive::ProviderArchive;
use std::path::PathBuf;

pub struct ParCreateArgs {
pub capid: String,
pub vendor: String,
pub revision: Option<i32>,
pub version: Option<String>,
pub schema: Option<PathBuf>,
pub name: String,
pub arch: String,
}

pub async fn create_provider_archive(
ParCreateArgs {
capid,
vendor,
revision,
version,
schema,
name,
arch,
}: ParCreateArgs,
binary_bytes: &[u8],
) -> Result<ProviderArchive> {
let mut par = ProviderArchive::new(&capid, &name, &vendor, revision, version);

par.add_library(&arch, binary_bytes)
.map_err(convert_error)?;

if let Some(ref schema) = schema {
let bytes = std::fs::read(schema)?;
par.set_schema(
serde_json::from_slice::<serde_json::Value>(&bytes)
.with_context(|| "Unable to parse JSON from file contents".to_string())?,
)
.map_err(convert_error)
.with_context(|| format!("Error parsing JSON schema from file '{:?}'", schema))?;
}

Ok(par)
}

pub async fn insert_provider_binary(
arch: String,
binary_bytes: &[u8],
mut par: ProviderArchive,
) -> Result<ProviderArchive> {
par.add_library(&arch, binary_bytes)
.map_err(convert_error)?;

Ok(par)
}

/// Converts error from Send + Sync error to standard anyhow error
pub fn convert_error(e: Box<dyn ::std::error::Error + Send + Sync>) -> anyhow::Error {
anyhow!(e.to_string())
}
76 changes: 37 additions & 39 deletions src/par.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::{collections::HashMap, fs::File, io::prelude::*, path::PathBuf};
use std::fs::File;
use std::io::Read;
use std::{collections::HashMap, path::PathBuf};

use crate::util::convert_error;
use anyhow::{anyhow, bail, Context, Result};
use clap::{Parser, Subcommand};
use log::warn;
use nkeys::KeyPairType;
use provider_archive::ProviderArchive;
use serde_json::json;
use wash_lib::cli::{extract_keypair, inspect, CommandOutput, OutputKind};
use wash_lib::cli::par::{convert_error, create_provider_archive, insert_provider_binary};
use wash_lib::cli::{extract_keypair, inspect, par, CommandOutput, OutputKind};

const GZIP_MAGIC: [u8; 2] = [0x1f, 0x8b];

Expand Down Expand Up @@ -204,6 +206,21 @@ impl From<InspectCommand> for inspect::InspectCliCommand {
}
}
}

impl From<CreateCommand> for par::ParCreateArgs {
fn from(cmd: CreateCommand) -> Self {
par::ParCreateArgs {
capid: cmd.capid,
vendor: cmd.vendor,
revision: cmd.revision,
version: cmd.version,
schema: cmd.schema,
name: cmd.name,
arch: cmd.arch,
}
}
}

pub(crate) async fn handle_command(
command: ParCliCommand,
output_kind: OutputKind,
Expand All @@ -223,39 +240,29 @@ pub(crate) async fn handle_create(
cmd: CreateCommand,
output_kind: OutputKind,
) -> Result<CommandOutput> {
let mut par = ProviderArchive::new(
&cmd.capid,
&cmd.name,
&cmd.vendor,
cmd.revision,
cmd.version,
);

let mut f = File::open(cmd.binary.clone())?;
let mut lib = Vec::new();
f.read_to_end(&mut lib)?;

let issuer = extract_keypair(
cmd.issuer,
cmd.issuer.clone(),
Some(cmd.binary.clone()),
cmd.directory.clone(),
KeyPairType::Account,
cmd.disable_keygen,
output_kind,
)?;
let subject = extract_keypair(
cmd.subject,
cmd.subject.clone(),
Some(cmd.binary.clone()),
cmd.directory,
cmd.directory.clone(),
KeyPairType::Service,
cmd.disable_keygen,
output_kind,
)?;

par.add_library(&cmd.arch, &lib).map_err(convert_error)?;

let extension = if cmd.compress { ".par.gz" } else { ".par" };
let outfile = match cmd.destination {
let outfile = match cmd.destination.clone() {
Some(path) => path,
None => format!(
"{}{}",
Expand All @@ -267,16 +274,8 @@ pub(crate) async fn handle_create(
extension
),
};
if let Some(ref schema) = cmd.schema {
let bytes = std::fs::read(schema)?;
par.set_schema(
serde_json::from_slice::<serde_json::Value>(&bytes)
.with_context(|| "Unable to parse JSON from file contents".to_string())?,
)
.map_err(convert_error)
.with_context(|| format!("Error parsing JSON schema from file '{:?}'", schema))?;
}

let mut par = create_provider_archive(cmd.clone().into(), &lib).await?;
par.write(&outfile, &issuer, &subject, cmd.compress)
.await
.map_err(|e| anyhow!("{}", e))
Expand Down Expand Up @@ -304,33 +303,32 @@ pub(crate) async fn handle_insert(
let mut f = File::open(cmd.archive.clone())?;
f.read_to_end(&mut buf)?;

let mut par = ProviderArchive::try_load(&buf)
.await
.map_err(convert_error)?;
let mut f = File::open(cmd.binary.clone())?;
let mut lib = Vec::new();
f.read_to_end(&mut lib)?;

let issuer = extract_keypair(
cmd.issuer,
Some(cmd.binary.clone()),
cmd.issuer.clone(),
Some(cmd.binary.clone().to_owned()),
cmd.directory.clone(),
KeyPairType::Account,
cmd.disable_keygen,
output_kind,
)?;
let subject = extract_keypair(
cmd.subject,
Some(cmd.binary.clone()),
cmd.directory,
cmd.subject.clone(),
Some(cmd.binary.clone().to_owned()),
cmd.directory.clone(),
KeyPairType::Service,
cmd.disable_keygen,
output_kind,
)?;

let mut f = File::open(cmd.binary.clone())?;
let mut lib = Vec::new();
f.read_to_end(&mut lib)?;

par.add_library(&cmd.arch, &lib).map_err(convert_error)?;
let mut par = ProviderArchive::try_load(&buf)
.await
.map_err(convert_error)?;

par = insert_provider_binary(cmd.arch, &lib, par).await?;
par.write(&cmd.archive, &issuer, &subject, is_compressed(&buf)?)
.await
.map_err(convert_error)?;
Expand Down
7 changes: 1 addition & 6 deletions src/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fs::File, io::Read, path::PathBuf};

use anyhow::{anyhow, Context, Result};
use anyhow::{Context, Result};
use term_table::{Table, TableStyle};
use wash_lib::config::DEFAULT_NATS_TIMEOUT_MS;

Expand All @@ -25,11 +25,6 @@ pub(crate) fn default_timeout_ms() -> u64 {
DEFAULT_NATS_TIMEOUT_MS
}

/// Converts error from Send + Sync error to standard anyhow error
pub(crate) fn convert_error(e: Box<dyn ::std::error::Error + Send + Sync>) -> anyhow::Error {
anyhow!(e.to_string())
}

/// Transform a json string (e.g. "{"hello": "world"}") into msgpack bytes
pub(crate) fn json_str_to_msgpack_bytes(payload: &str) -> Result<Vec<u8>> {
let json = serde_json::from_str::<serde_json::Value>(payload)?;
Expand Down

0 comments on commit 90f7944

Please sign in to comment.