···11use nu_plugin::{serve_plugin, MsgPackSerializer, Plugin, EvaluatedCall, LabeledError};
22-use nu_protocol::{PluginSignature, Value, SyntaxShape, PluginExample};
22+use nu_protocol::{PluginSignature, Value, SyntaxShape, PluginExample, Span};
3344mod config;
55mod client;
66mod convert;
77+mod dbus_type;
7889use config::*;
910use client::*;
···2526 .is_dbus_command()
2627 .accepts_dbus_client_options()
2728 .usage("Call a method and get its response")
2929+ .extra_usage("Returns an array if the method call returns more than one value.")
2830 .named("timeout", SyntaxShape::Duration, "How long to wait for a response", None)
3131+ .named("signature", SyntaxShape::String,
3232+ "Signature of the arguments to send, in D-Bus format\n\
3333+ If not provided, they will be guessed automatically (but poorly)", None)
3434+ .switch("no-flatten", "Always return a list of all return values", None)
2935 .required_named("dest", SyntaxShape::String,
3036 "The name of the connection to send the method to",
3137 None)
···3541 "The name of the interface the method belongs to")
3642 .required("method", SyntaxShape::String,
3743 "The name of the method to send")
4444+ .rest("args", SyntaxShape::Any,
4545+ "Arguments to send with the method call")
3846 .plugin_examples(vec![
3947 PluginExample {
4048 example: "dbus call --dest=org.freedesktop.DBus \
···98106 fn call(&self, call: &EvaluatedCall) -> Result<Value, LabeledError> {
99107 let config = DbusClientConfig::try_from(call)?;
100108 let dbus = DbusClient::new(config)?;
101101- dbus.call(
109109+ let values = dbus.call(
102110 &call.get_flag("dest")?.unwrap(),
103111 &call.req(0)?,
104112 &call.req(1)?,
105113 &call.req(2)?,
106106- )
114114+ call.get_flag("signature")?.as_ref(),
115115+ &call.positional[3..]
116116+ )?;
117117+118118+ let flatten = !call.get_flag::<bool>("no-flatten")?.unwrap_or(false);
119119+120120+ // Make the output easier to deal with by returning a list only if there are multiple return
121121+ // values (not so common)
122122+ match values.len() {
123123+ 0 if flatten => Ok(Value::nothing(Span::unknown())),
124124+ 1 if flatten => Ok(values.into_iter().nth(0).unwrap()),
125125+ _ => Ok(Value::list(values, Span::unknown()))
126126+ }
107127 }
108128}