Skip to content

Commit

Permalink
Fix positional argumnt parsing
Browse files Browse the repository at this point in the history
As outlined in this bug docopt/docopt.rs#226
Docopt is rather specific about flags versus positional arguments. In
our case we had positional arguments that we where using as flags. This
created some very confusing behavior where you you could reorder the flags
and get a arcane looking config error because 'linux' (the platform arg)
was passed as the config arg. If you look carefully at the example USAGE
in the Docopt README https://github.com/docopt/docopt you see that all
flags not only require the Args strut but also the use of the = sign.

This patch corrects these problems and allows flags to work as expected
  • Loading branch information
jkilpatr committed Feb 7, 2020
1 parent 6176f2a commit 9d5d24a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
4 changes: 2 additions & 2 deletions integration-tests/rita.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def start_rita(node):
save_rita_settings(id, settings)
time.sleep(0.1)
os.system(
'(RUST_BACKTRACE=full RUST_LOG=TRACE ip netns exec netlab-{id} {rita} --config rita-settings-n{id}.toml --platform linux'
'(RUST_BACKTRACE=full RUST_LOG=TRACE ip netns exec netlab-{id} {rita} --config=rita-settings-n{id}.toml --platform=linux'
' 2>&1 & echo $! > rita-n{id}.pid) | '
'grep -Ev "<unknown>|mio|tokio_core|hyper" > rita-n{id}.log &'.format(id=id, rita=RITA,
pwd=dname)
Expand All @@ -240,7 +240,7 @@ def start_rita_exit(node):
save_rita_settings(id, settings)
time.sleep(0.1)
os.system(
'(RUST_BACKTRACE=full RUST_LOG=TRACE ip netns exec netlab-{id} {rita} --config rita-settings-n{id}.toml'
'(RUST_BACKTRACE=full RUST_LOG=TRACE ip netns exec netlab-{id} {rita} --config=rita-settings-n{id}.toml'
' 2>&1 & echo $! > rita-n{id}.pid) | '
'grep -Ev "<unknown>|mio|tokio_core|hyper" > rita-n{id}.log &'.format(id=id, rita=RITA_EXIT,
pwd=dname)
Expand Down
28 changes: 17 additions & 11 deletions rita/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,22 @@ extern crate num256;
mod rita_client;
mod rita_common;

use rita_common::dashboard::network_endpoints::*;
use rita_client::dashboard::network_endpoints::*;
use rita_common::dashboard::network_endpoints::*;
use rita_common::network_endpoints::{hello_response, make_payments};

#[derive(Debug, Deserialize)]
struct Args {
flag_config: String,
flag_platform: String,
}

#[cfg(not(test))]
const USAGE: &str = "
Usage: rita --config <settings> --platform <platform>
Usage: rita --config=<settings> --platform=<platform>
Options:
--config Name of config file
--platform Platform (linux or openwrt)
-c, --config=<settings> Name of config file
-p, --platform=<platform> Platform (linux or openwrt)
";

use althea_kernel_interface::KernelInterface;
Expand All @@ -99,18 +105,18 @@ lazy_static! {
#[cfg(not(test))]
lazy_static! {
pub static ref SETTING: Arc<RwLock<RitaSettingsStruct>> = {
let args = Docopt::new(USAGE)
.and_then(|d| d.parse())
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());

let settings_file = args.get_str("<settings>");
let platform = args.get_str("<platform>");
let settings_file = args.flag_config;
let platform = args.flag_platform;

let s = RitaSettingsStruct::new_watched(settings_file).unwrap();
let s = RitaSettingsStruct::new_watched(&settings_file).unwrap();

clu::init(platform, s.clone());
clu::init(&platform, s.clone());

s.read().unwrap().write(settings_file).unwrap();
s.read().unwrap().write(&settings_file).unwrap();
s
};
}
Expand Down
26 changes: 15 additions & 11 deletions rita/src/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ extern crate actix;
extern crate actix_web;
extern crate bytes;
extern crate clu;
extern crate dotenv;
extern crate docopt;
extern crate dotenv;
extern crate env_logger;
extern crate eui48;
extern crate futures;
Expand All @@ -42,11 +42,10 @@ extern crate tokio;

use settings::{RitaCommonSettings, RitaExitSettings, RitaExitSettingsStruct};


#[cfg(not(test))]
use settings::FileWrite;
#[cfg(not(test))]
use docopt::Docopt;
#[cfg(not(test))]
use settings::FileWrite;

use actix::registry::SystemService;
use actix::*;
Expand All @@ -71,11 +70,16 @@ use std::sync::{Arc, RwLock};
#[cfg(test)]
use std::sync::Mutex;

#[derive(Debug, Deserialize)]
struct Args {
flag_config: String,
}

#[cfg(not(test))]
const USAGE: &str = "
Usage: rita_exit --config <settings>
Usage: rita_exit --config=<settings>
Options:
--config Name of config file
-c, --config=<settings> Name of config file
";

use althea_kernel_interface::KernelInterface;
Expand All @@ -102,14 +106,14 @@ lazy_static! {
#[cfg(not(test))]
lazy_static! {
pub static ref SETTING: Arc<RwLock<RitaExitSettingsStruct>> = {
let args = Docopt::new(USAGE)
.and_then(|d| d.parse())
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());

let settings_file = args.get_str("<settings>");
let settings_file = args.flag_config;

let s = RitaExitSettingsStruct::new_watched(settings_file).unwrap();
s.read().unwrap().write(settings_file).unwrap();
let s = RitaExitSettingsStruct::new_watched(&settings_file).unwrap();
s.read().unwrap().write(&settings_file).unwrap();

clu::exit_init("linux", s.clone());

Expand Down
2 changes: 1 addition & 1 deletion scripts/openwrt_upload.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ set -eux
export ROUTER_IP=192.168.1.1
bash scripts/openwrt_build.sh
scp target/mipsel-unknown-linux-musl/debug/rita root@$ROUTER_IP:/tmp/rita
ssh root@$ROUTER_IP RUST_BACKTRACE=FULL RUST_LOG=TRACE /tmp/rita --config /etc/rita.toml --platform linux &> out.log
ssh root@$ROUTER_IP RUST_BACKTRACE=FULL RUST_LOG=TRACE /tmp/rita --config=/etc/rita.toml --platform=linux &> out.log

0 comments on commit 9d5d24a

Please sign in to comment.