about summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2017-10-20T11·03+0200
committerKarl Erik Asbjørnsen <karl@asbjornsen.org>2017-10-20T11·17+0200
commit44e72884a6e2a8a5f2c25aeb1e1063eba5d32cfb (patch)
treed85a955b300a9c0df5d5c2b877358113fe124c14 /src/main.rs
parent10dcab636583a65e58a979016683b2fae7a9a62f (diff)
feat: Add create command
Adds a command to create message queues with custom parameters.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs42
1 files changed, 38 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index 8cc30424a060..ee4e7ad231d0 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,18 +1,18 @@
 extern crate clap;
 extern crate posix_mq;
 
-use clap::{App, SubCommand, Arg, AppSettings};
+use clap::{App, SubCommand, Arg, ArgMatches, AppSettings};
 use posix_mq::{Name, Queue};
+use std::fs::{read_dir, File};
+use std::io::Read;
+use std::process::exit;
 
 fn run_ls() {
-    use std::fs::{read_dir, File};
-    use std::io::Read;
 
     let mqueues = read_dir("/dev/mqueue")
         .expect("Could not read message queues");
 
     mqueues.for_each(|queue| {
-        //let queue_name = (&queue.unwrap().file_name()).into_string().unwrap();
         let path = queue.unwrap().path();
         let status = {
             let mut file = File::open(&path)
@@ -38,6 +38,24 @@ fn run_inspect(queue_name: &str) {
     println!("Max. # of pending messages: {}", queue.max_pending());
 }
 
+fn run_create(cmd: &ArgMatches) {
+    let name = Name::new(cmd.value_of("queue").unwrap())
+        .expect("Invalid queue name");
+
+    let max_pending: i64 = cmd.value_of("max-pending").unwrap().parse().unwrap();
+    let max_size: i64 = cmd.value_of("max-size").unwrap().parse().unwrap();
+
+    let queue = Queue::create(name, max_pending, max_size * 1024);
+
+    match queue {
+        Ok(_)  => println!("Queue created successfully"),
+        Err(e) => {
+            println!("Could not create queue: {}", e);
+            exit(1);
+        },
+    };
+}
+
 fn main() {
     let ls = SubCommand::with_name("ls").about("list message queues");
     let inspect = SubCommand::with_name("inspect")
@@ -47,6 +65,20 @@ fn main() {
             .required(true)
             .takes_value(true));
 
+    let create = SubCommand::with_name("create")
+        .about("Create a new queue")
+        .arg(Arg::with_name("queue")
+            .required(true)
+            .takes_value(true))
+        .arg(Arg::with_name("max-size")
+            .help("maximum message size (in kB)")
+            .required(true)
+            .takes_value(true))
+        .arg(Arg::with_name("max-pending")
+            .help("maximum # of pending messages")
+            .required(true)
+            .takes_value(true));
+
 
     let matches = App::new("mq")
         .setting(AppSettings::SubcommandRequiredElseHelp)
@@ -54,11 +86,13 @@ fn main() {
         .about("Administrate and inspect POSIX message queues")
         .subcommand(ls)
         .subcommand(inspect)
+        .subcommand(create)
         .get_matches();
 
     match matches.subcommand() {
         ("ls", _) => run_ls(),
         ("inspect", Some(cmd)) => run_inspect(cmd.value_of("queue").unwrap()),
+        ("create", Some(cmd))  => run_create(cmd),
         _ => unimplemented!(),
     }
 }