2019-07-03 17:37:09 +00:00
|
|
|
use indexmap::IndexMap;
|
|
|
|
use nu::{
|
2019-07-18 01:49:12 +00:00
|
|
|
serve_plugin, Args, CommandConfig, NamedType, Plugin, PositionalType, Primitive, ReturnSuccess,
|
2019-07-13 02:07:06 +00:00
|
|
|
ReturnValue, ShellError, Spanned, SpannedItem, Value,
|
2019-07-03 17:37:09 +00:00
|
|
|
};
|
2019-06-27 04:56:48 +00:00
|
|
|
|
2019-07-02 07:56:20 +00:00
|
|
|
struct Inc {
|
2019-07-18 01:32:19 +00:00
|
|
|
field: Option<String>,
|
2019-07-18 01:49:12 +00:00
|
|
|
major: bool,
|
|
|
|
minor: bool,
|
|
|
|
patch: bool,
|
2019-06-27 04:56:48 +00:00
|
|
|
}
|
2019-07-02 07:56:20 +00:00
|
|
|
impl Inc {
|
|
|
|
fn new() -> Inc {
|
2019-07-18 01:49:12 +00:00
|
|
|
Inc {
|
|
|
|
field: None,
|
|
|
|
major: false,
|
|
|
|
minor: false,
|
|
|
|
patch: false,
|
|
|
|
}
|
2019-07-18 01:32:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 01:49:12 +00:00
|
|
|
fn inc(
|
|
|
|
&self,
|
|
|
|
value: Spanned<Value>,
|
|
|
|
field: &Option<String>,
|
|
|
|
) -> Result<Spanned<Value>, ShellError> {
|
2019-07-18 01:32:19 +00:00
|
|
|
match value.item {
|
|
|
|
Value::Primitive(Primitive::Int(i)) => Ok(Value::int(i + 1).spanned(value.span)),
|
|
|
|
Value::Primitive(Primitive::Bytes(b)) => {
|
|
|
|
Ok(Value::bytes(b + 1 as u64).spanned(value.span))
|
|
|
|
}
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
|
|
|
if let Ok(i) = s.parse::<u64>() {
|
|
|
|
Ok(Spanned {
|
|
|
|
item: Value::string(format!("{}", i + 1)),
|
|
|
|
span: value.span,
|
|
|
|
})
|
2019-07-18 01:49:12 +00:00
|
|
|
} else if let Ok(mut ver) = semver::Version::parse(&s) {
|
|
|
|
if self.major {
|
|
|
|
ver.increment_major();
|
|
|
|
} else if self.minor {
|
|
|
|
ver.increment_minor();
|
|
|
|
} else {
|
|
|
|
self.patch;
|
|
|
|
ver.increment_patch();
|
|
|
|
}
|
|
|
|
Ok(Spanned {
|
|
|
|
item: Value::string(ver.to_string()),
|
|
|
|
span: value.span,
|
|
|
|
})
|
2019-07-18 01:32:19 +00:00
|
|
|
} else {
|
|
|
|
Err(ShellError::string("string could not be incremented"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Value::Object(_) => match field {
|
|
|
|
Some(f) => {
|
|
|
|
let replacement = match value.item.get_data_by_path(value.span, f) {
|
2019-07-18 01:49:12 +00:00
|
|
|
Some(result) => self.inc(result.map(|x| x.clone()), &None)?,
|
2019-07-18 01:32:19 +00:00
|
|
|
None => {
|
|
|
|
return Err(ShellError::string("inc could not find field to replace"))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
match value
|
|
|
|
.item
|
|
|
|
.replace_data_at_path(value.span, f, replacement.item.clone())
|
|
|
|
{
|
|
|
|
Some(v) => return Ok(v),
|
|
|
|
None => {
|
|
|
|
return Err(ShellError::string("inc could not find field to replace"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => Err(ShellError::string(
|
|
|
|
"inc needs a field when incrementing a value in an object",
|
|
|
|
)),
|
|
|
|
},
|
|
|
|
x => Err(ShellError::string(format!(
|
|
|
|
"Unrecognized type in stream: {:?}",
|
|
|
|
x
|
|
|
|
))),
|
|
|
|
}
|
2019-06-27 04:56:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-13 02:07:06 +00:00
|
|
|
impl Plugin for Inc {
|
|
|
|
fn config(&mut self) -> Result<CommandConfig, ShellError> {
|
2019-07-18 01:49:12 +00:00
|
|
|
let mut named = IndexMap::new();
|
|
|
|
named.insert("major".to_string(), NamedType::Switch);
|
|
|
|
named.insert("minor".to_string(), NamedType::Switch);
|
|
|
|
named.insert("patch".to_string(), NamedType::Switch);
|
|
|
|
|
2019-07-13 02:07:06 +00:00
|
|
|
Ok(CommandConfig {
|
|
|
|
name: "inc".to_string(),
|
2019-07-18 01:32:19 +00:00
|
|
|
positional: vec![PositionalType::optional_any("Field")],
|
2019-07-13 02:07:06 +00:00
|
|
|
is_filter: true,
|
|
|
|
is_sink: false,
|
2019-07-18 01:49:12 +00:00
|
|
|
named,
|
2019-07-13 02:07:06 +00:00
|
|
|
rest_positional: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
fn begin_filter(&mut self, args: Args) -> Result<(), ShellError> {
|
2019-07-18 01:49:12 +00:00
|
|
|
if args.has("major") {
|
|
|
|
self.major = true;
|
|
|
|
}
|
|
|
|
if args.has("minor") {
|
|
|
|
self.minor = true;
|
|
|
|
}
|
|
|
|
if args.has("patch") {
|
|
|
|
self.patch = true;
|
|
|
|
}
|
|
|
|
|
2019-07-13 02:07:06 +00:00
|
|
|
if let Some(args) = args.positional {
|
|
|
|
for arg in args {
|
|
|
|
match arg {
|
|
|
|
Spanned {
|
2019-07-18 01:32:19 +00:00
|
|
|
item: Value::Primitive(Primitive::String(s)),
|
2019-07-13 02:07:06 +00:00
|
|
|
..
|
|
|
|
} => {
|
2019-07-18 01:32:19 +00:00
|
|
|
self.field = Some(s);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return Err(ShellError::string(format!(
|
|
|
|
"Unrecognized type in params: {:?}",
|
|
|
|
arg
|
|
|
|
)))
|
2019-06-27 04:56:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-13 02:07:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn filter(&mut self, input: Spanned<Value>) -> Result<Vec<ReturnValue>, ShellError> {
|
2019-07-18 01:49:12 +00:00
|
|
|
Ok(vec![ReturnSuccess::value(self.inc(input, &self.field)?)])
|
2019-06-27 04:56:48 +00:00
|
|
|
}
|
2019-07-02 07:56:20 +00:00
|
|
|
}
|
2019-06-27 04:56:48 +00:00
|
|
|
|
2019-07-02 07:56:20 +00:00
|
|
|
fn main() {
|
|
|
|
serve_plugin(&mut Inc::new());
|
2019-06-27 04:56:48 +00:00
|
|
|
}
|