Skip to content

Commit f8fde96

Browse files
authored
Merge pull request #18 from devrc-hub/feature/set-variable-from-cli
feature(cli): Add set variable option from command line
2 parents f80f92e + 6fdffff commit f8fde96

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

src/cli.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,32 @@ use structopt::{
44
StructOpt,
55
};
66

7+
use std::error::Error;
8+
9+
use crate::errors::{DevrcError, DevrcResult};
10+
711
pub fn get_crate_version() -> &'static str {
812
env!("CARGO_PKG_VERSION")
913
}
1014

15+
/// Parse a single key-value pair
16+
fn parse_key_val<T>(s: &str) -> DevrcResult<(T, T)>
17+
where
18+
T: std::str::FromStr,
19+
T::Err: Error + 'static,
20+
{
21+
let pos = s.find('=').ok_or({
22+
//format!("invalid KEY=value: no `=` found in `{}`", s)}
23+
DevrcError::InvalidArgument
24+
})?;
25+
Ok((
26+
s[..pos].parse().map_err(|_| DevrcError::InvalidArgument)?,
27+
s[pos + 1..]
28+
.parse()
29+
.map_err(|_| DevrcError::InvalidArgument)?,
30+
))
31+
}
32+
1133
#[derive(StructOpt, Debug)]
1234
#[structopt(version = get_crate_version())]
1335
#[structopt(name = "devrc")]
@@ -77,11 +99,10 @@ pub struct CommandLine {
7799
/// Suppress all output
78100
#[structopt(short, long)]
79101
pub quiet: bool,
80-
// #[structopt(subcommand)]
81-
// pub sub: Option<Subcommands>, // /// Trailing newline behavior for the password. If "auto",
82-
// // /// a trailing newline will be printed iff stdout is detected to be a tty.
83-
// // #[structopt(long="newline", default_value="auto", raw(possible_values="&NewlineBehavior::variants()"))]
84-
// // newline: NewlineBehavior
102+
103+
/// Override <VARIABLE> with <VALUE>
104+
#[structopt(long = "--set", parse(try_from_str = parse_key_val), name="VAR=VALUE")]
105+
pub set: Vec<(String, String)>,
85106
}
86107

87108
impl CommandLine {

src/errors.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ pub enum DevrcError {
2929
Code { code: i32 },
3030
RuntimeError,
3131
CircularDependencies,
32+
InvalidArgument,
3233
}
3334

3435
impl Display for DevrcError {

src/main.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ fn main() -> Result<(), Box<dyn Error>> {
4141
runner.load()?;
4242
}
4343

44+
if !opt.set.is_empty() {
45+
runner.setup_variables(opt.set.into())?
46+
}
47+
4448
if opt.list {
4549
runner.list_tasks()?;
4650
} else if opt.list_vars {

src/runner.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
get_absolute_path, get_directory_devrc_file, get_global_devrc_file,
1414
get_local_user_defined_devrc_file,
1515
},
16+
variables::RawVariables,
1617
workshop::Designer,
1718
};
1819

@@ -68,6 +69,10 @@ impl Runner {
6869
Ok(())
6970
}
7071

72+
pub fn setup_variables(&mut self, variables: RawVariables) -> DevrcResult<()> {
73+
self.devrc.add_variables(variables)
74+
}
75+
7176
// Try to get devrcfile
7277
pub fn get_rawdevrc_file<F>(&self, try_file_func: F) -> DevrcResult<RawDevrcfile>
7378
where

src/variables.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ impl RawVariables {
8282
}
8383
}
8484

85+
impl From<Vec<(String, String)>> for RawVariables {
86+
fn from(items: Vec<(String, String)>) -> Self {
87+
Self {
88+
vars: items
89+
.iter()
90+
.map(move |x| (x.0.clone(), ValueKind::String(x.1.clone())))
91+
.collect::<indexmap::IndexMap<String, ValueKind>>(),
92+
}
93+
}
94+
}
95+
8596
impl ValueKind {
8697
pub fn evaluate(&self, name: &str, scope: &Scope) -> DevrcResult<String> {
8798
match self {

0 commit comments

Comments
 (0)