separate context for sdk

This commit is contained in:
Aiden McClelland
2021-09-27 18:37:03 -06:00
parent 5c11b14383
commit 69fd8da2ff
7 changed files with 78 additions and 30 deletions

View File

@@ -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 {

View File

@@ -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<Url>,
#[serde(deserialize_with = "crate::util::deserialize_from_str_opt")]
pub proxy: Option<Url>,
pub developer_key_path: Option<PathBuf>,
pub cookie_path: Option<PathBuf>,
#[serde(flatten)]
pub server_config: RpcContextConfig,
}
#[derive(Debug)]
@@ -36,7 +32,6 @@ pub struct CliContextSeed {
pub client: Client,
pub cookie_store: Arc<CookieStoreMutex>,
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<Self, crate::Error> {
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<ed25519_dalek::Keypair, Error> {
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;

View File

@@ -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<CliContext> for () {
@@ -23,6 +25,11 @@ impl From<RpcContext> for () {
()
}
}
impl From<SdkContext> for () {
fn from(_: SdkContext) -> Self {
()
}
}
impl From<SetupContext> for () {
fn from(_: SetupContext) -> Self {
()

64
appmgr/src/context/sdk.rs Normal file
View File

@@ -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<PathBuf>,
}
#[derive(Debug)]
pub struct SdkContextSeed {
pub developer_key_path: PathBuf,
}
#[derive(Debug, Clone)]
pub struct SdkContext(Arc<SdkContextSeed>);
impl SdkContext {
/// BLOCKING
pub fn init(matches: &ArgMatches) -> Result<Self, crate::Error> {
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<ed25519_dalek::Keypair, Error> {
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 {}

View File

@@ -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() {

View File

@@ -54,9 +54,6 @@ pub fn echo(#[arg] message: String) -> Result<String, RpcError> {
#[command(subcommands(
version::git_info,
echo,
developer::init,
s9pk::pack,
s9pk::verify,
inspect::inspect,
server,
package,

View File

@@ -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<PathBuf>) -> Result<(), Error> {
pub fn pack(#[context] ctx: SdkContext, #[arg] path: Option<PathBuf>) -> Result<(), Error> {
use std::fs::File;
use std::io::Read;