basic example application

This commit is contained in:
Jake Hillion 2022-02-28 20:42:44 +00:00
parent 969bf3a9ee
commit 2972f9603b
6 changed files with 73 additions and 9 deletions

View File

@ -1,4 +1,3 @@
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "rust-lang.rust"
"editor.formatOnSave": true
}

View File

@ -1,2 +1,12 @@
# clone-shim
## Running the examples
### examples/basic
The basic example instructs the shim to spawn two processes, each of which writes "hello from main{1,2}!" to stdout.
To run this example:
cargo build --example basic
cargo run -- -s examples/basic/spec.json target/debug/examples/basic

22
examples/basic/main.rs Normal file
View File

@ -0,0 +1,22 @@
fn main() {
let mut args = std::env::args();
let _bin = args.next();
match args.next() {
Some(s) => match s.as_str() {
"main1" => main1(),
"main2" => main2(),
_ => unimplemented!(),
},
None => unimplemented!(),
}
}
fn main1() {
println!("hello from main1!");
}
fn main2() {
println!("hello from main2!");
}

16
examples/basic/spec.json Normal file
View File

@ -0,0 +1,16 @@
{
"entrypoints": {
"main1": {
"args": [
"BinaryName",
"Entrypoint"
]
},
"main2": {
"args": [
"BinaryName",
"Entrypoint"
]
}
}
}

View File

@ -6,7 +6,7 @@ mod specification;
use clone::{clone3, CloneArgs, CloneFlags};
use error::Error;
use specification::{Arg, Entrypoint, Pipe, Specification, Trigger};
use specification::{Arg, Pipe, Specification, Trigger};
use std::collections::HashMap;
use std::ffi::CString;
@ -132,11 +132,11 @@ fn run() -> Result<(), Error> {
})?;
}
}
Trigger::Pipe(s) => {}
Trigger::Pipe(_s) => {
todo!()
}
}
}
Ok(())
}
pub fn pipe_trigger(pipe: File, entry: &Entrypoint) {}

View File

@ -13,8 +13,13 @@ pub struct Specification {
#[derive(Serialize, Deserialize, Debug)]
pub struct Entrypoint {
#[serde(default)]
pub trigger: Trigger,
#[serde(default = "Arg::default_vec")]
pub args: Vec<Arg>,
#[serde(default)]
pub permissions: HashSet<Permissions>,
}
@ -25,8 +30,14 @@ pub enum Trigger {
Pipe(String),
}
impl Default for Trigger {
fn default() -> Self {
Self::Startup
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
// #[serde(tag = "type")]
pub enum Arg {
/// The binary name, or argv[0], of the original program start
BinaryName,
@ -41,6 +52,12 @@ pub enum Arg {
Trailing,
}
impl Arg {
fn default_vec() -> Vec<Arg> {
vec![Arg::BinaryName]
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(tag = "type")]
pub enum Pipe {
@ -74,7 +91,7 @@ impl Specification {
let mut read = Vec::new();
let mut write = Vec::new();
for (_, entry) in &self.entrypoints {
for entry in self.entrypoints.values() {
match &entry.trigger {
Trigger::Startup => {}
Trigger::Pipe(s) => read.push(s.as_str()),
@ -120,7 +137,7 @@ impl Specification {
}
}
for pipe in write_set {
if let Some(pipe) = write_set.into_iter().next() {
return Err(Error::WriteOnlyPipe(pipe.to_string()));
}