From d4aa75ea938458dba1825d9c4b8d07df7c8e1063 Mon Sep 17 00:00:00 2001 From: Aiden McClelland Date: Mon, 27 Sep 2021 18:37:03 -0600 Subject: [PATCH] separate context for sdk --- appmgr/src/bin/embassy-sdk.rs | 4 +-- appmgr/src/context/cli.rs | 22 +----------- appmgr/src/context/mod.rs | 7 ++++ appmgr/src/context/sdk.rs | 64 +++++++++++++++++++++++++++++++++++ appmgr/src/developer/mod.rs | 4 +-- appmgr/src/lib.rs | 3 -- appmgr/src/s9pk/mod.rs | 4 +-- 7 files changed, 78 insertions(+), 30 deletions(-) create mode 100644 appmgr/src/context/sdk.rs diff --git a/appmgr/src/bin/embassy-sdk.rs b/appmgr/src/bin/embassy-sdk.rs index ff748629b..c33dc2f13 100644 --- a/appmgr/src/bin/embassy-sdk.rs +++ b/appmgr/src/bin/embassy-sdk.rs @@ -1,4 +1,4 @@ -use embassy::context::CliContext; +use embassy::context::SdkContext; use embassy::Error; use rpc_toolkit::run_cli; use rpc_toolkit::yajrc::RpcError; @@ -30,7 +30,7 @@ fn inner_main() -> Result<(), Error> { 4 => log::LevelFilter::Debug, _ => log::LevelFilter::Trace, }); - CliContext::init(matches)? + SdkContext::init(matches)? }, exit: |e: RpcError| { match e.data { diff --git a/appmgr/src/context/cli.rs b/appmgr/src/context/cli.rs index 4519d27a5..a59428c95 100644 --- a/appmgr/src/context/cli.rs +++ b/appmgr/src/context/cli.rs @@ -14,7 +14,6 @@ use rpc_toolkit::url::Host; use rpc_toolkit::Context; use serde::Deserialize; -use super::rpc::RpcContextConfig; use crate::{Error, ResultExt}; #[derive(Debug, Default, Deserialize)] @@ -24,10 +23,7 @@ pub struct CliContextConfig { pub host: Option, #[serde(deserialize_with = "crate::util::deserialize_from_str_opt")] pub proxy: Option, - pub developer_key_path: Option, pub cookie_path: Option, - #[serde(flatten)] - pub server_config: RpcContextConfig, } #[derive(Debug)] @@ -36,7 +32,6 @@ pub struct CliContextSeed { pub client: Client, pub cookie_store: Arc, pub cookie_path: PathBuf, - pub developer_key_path: PathBuf, } impl Drop for CliContextSeed { fn drop(&mut self) { @@ -63,7 +58,7 @@ impl CliContext { /// BLOCKING pub fn init(matches: &ArgMatches) -> Result { let cfg_path = Path::new(matches.value_of("config").unwrap_or(crate::CONFIG_PATH)); - let mut base = if cfg_path.exists() { + let base = if cfg_path.exists() { serde_yaml::from_reader( File::open(cfg_path) .with_ctx(|_| (crate::ErrorKind::Filesystem, cfg_path.display().to_string()))?, @@ -114,23 +109,8 @@ impl CliContext { }, cookie_store, cookie_path, - developer_key_path: base.developer_key_path.unwrap_or_else(|| { - cfg_path - .parent() - .unwrap_or(Path::new("/")) - .join(".developer_key") - }), }))) } - /// BLOCKING - pub fn developer_key(&self) -> Result { - if !self.developer_key_path.exists() { - return Err(Error::new(anyhow!("Developer Key does not exist! Please run `embassy-sdk init` before running this command."), crate::ErrorKind::Uninitialized)); - } - let mut keypair_buf = [0; ed25519_dalek::KEYPAIR_LENGTH]; - File::open(&self.developer_key_path)?.read_exact(&mut keypair_buf)?; - Ok(ed25519_dalek::Keypair::from_bytes(&keypair_buf)?) - } } impl std::ops::Deref for CliContext { type Target = CliContextSeed; diff --git a/appmgr/src/context/mod.rs b/appmgr/src/context/mod.rs index c094c50ab..c7bf15271 100644 --- a/appmgr/src/context/mod.rs +++ b/appmgr/src/context/mod.rs @@ -1,11 +1,13 @@ pub mod cli; pub mod diagnostic; pub mod rpc; +pub mod sdk; pub mod setup; pub use cli::CliContext; pub use diagnostic::DiagnosticContext; pub use rpc::RpcContext; +pub use sdk::SdkContext; pub use setup::SetupContext; impl From for () { @@ -23,6 +25,11 @@ impl From for () { () } } +impl From for () { + fn from(_: SdkContext) -> Self { + () + } +} impl From for () { fn from(_: SetupContext) -> Self { () diff --git a/appmgr/src/context/sdk.rs b/appmgr/src/context/sdk.rs new file mode 100644 index 000000000..96a837c56 --- /dev/null +++ b/appmgr/src/context/sdk.rs @@ -0,0 +1,64 @@ +use std::fs::File; +use std::io::Read; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +use anyhow::anyhow; +use clap::ArgMatches; +use rpc_toolkit::Context; +use serde::Deserialize; + +use crate::{Error, ResultExt}; + +#[derive(Debug, Default, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub struct SdkContextConfig { + pub developer_key_path: Option, +} + +#[derive(Debug)] +pub struct SdkContextSeed { + pub developer_key_path: PathBuf, +} + +#[derive(Debug, Clone)] +pub struct SdkContext(Arc); +impl SdkContext { + /// BLOCKING + pub fn init(matches: &ArgMatches) -> Result { + let cfg_path = Path::new(matches.value_of("config").unwrap_or(crate::CONFIG_PATH)); + let base = if cfg_path.exists() { + serde_yaml::from_reader( + File::open(cfg_path) + .with_ctx(|_| (crate::ErrorKind::Filesystem, cfg_path.display().to_string()))?, + ) + .with_kind(crate::ErrorKind::Deserialization)? + } else { + SdkContextConfig::default() + }; + Ok(SdkContext(Arc::new(SdkContextSeed { + developer_key_path: base.developer_key_path.unwrap_or_else(|| { + cfg_path + .parent() + .unwrap_or(Path::new("/")) + .join(".developer_key") + }), + }))) + } + /// BLOCKING + pub fn developer_key(&self) -> Result { + if !self.developer_key_path.exists() { + return Err(Error::new(anyhow!("Developer Key does not exist! Please run `embassy-sdk init` before running this command."), crate::ErrorKind::Uninitialized)); + } + let mut keypair_buf = [0; ed25519_dalek::KEYPAIR_LENGTH]; + File::open(&self.developer_key_path)?.read_exact(&mut keypair_buf)?; + Ok(ed25519_dalek::Keypair::from_bytes(&keypair_buf)?) + } +} +impl std::ops::Deref for SdkContext { + type Target = SdkContextSeed; + fn deref(&self) -> &Self::Target { + &*self.0 + } +} +impl Context for SdkContext {} diff --git a/appmgr/src/developer/mod.rs b/appmgr/src/developer/mod.rs index ac7925f46..dec75c711 100644 --- a/appmgr/src/developer/mod.rs +++ b/appmgr/src/developer/mod.rs @@ -5,12 +5,12 @@ use std::path::Path; use ed25519_dalek::Keypair; use rpc_toolkit::command; -use crate::context::CliContext; +use crate::context::SdkContext; use crate::util::display_none; use crate::{Error, ResultExt}; #[command(cli_only, blocking, display(display_none))] -pub fn init(#[context] ctx: CliContext) -> Result<(), Error> { +pub fn init(#[context] ctx: SdkContext) -> Result<(), Error> { if !ctx.developer_key_path.exists() { let parent = ctx.developer_key_path.parent().unwrap_or(Path::new("/")); if !parent.exists() { diff --git a/appmgr/src/lib.rs b/appmgr/src/lib.rs index b2ce31152..1bd497110 100644 --- a/appmgr/src/lib.rs +++ b/appmgr/src/lib.rs @@ -54,9 +54,6 @@ pub fn echo(#[arg] message: String) -> Result { #[command(subcommands( version::git_info, echo, - developer::init, - s9pk::pack, - s9pk::verify, inspect::inspect, server, package, diff --git a/appmgr/src/s9pk/mod.rs b/appmgr/src/s9pk/mod.rs index 8043f1655..ca3091f5f 100644 --- a/appmgr/src/s9pk/mod.rs +++ b/appmgr/src/s9pk/mod.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use anyhow::anyhow; use rpc_toolkit::command; -use crate::context::CliContext; +use crate::context::SdkContext; use crate::s9pk::builder::S9pkPacker; use crate::s9pk::manifest::Manifest; use crate::s9pk::reader::S9pkReader; @@ -19,7 +19,7 @@ pub mod reader; pub const SIG_CONTEXT: &'static [u8] = b"s9pk"; #[command(cli_only, display(display_none), blocking)] -pub fn pack(#[context] ctx: CliContext, #[arg] path: Option) -> Result<(), Error> { +pub fn pack(#[context] ctx: SdkContext, #[arg] path: Option) -> Result<(), Error> { use std::fs::File; use std::io::Read;