-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement latest nushell api updates
- Loading branch information
Showing
12 changed files
with
698 additions
and
425 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; | ||
use nu_protocol::{Example, LabeledError, Signature, SyntaxShape, Type, Value}; | ||
|
||
use crate::{client::DbusClient, config::DbusClientConfig, DbusSignatureUtilExt}; | ||
|
||
pub struct Call; | ||
|
||
impl SimplePluginCommand for Call { | ||
type Plugin = crate::NuPluginDbus; | ||
|
||
fn name(&self) -> &str { | ||
"dbus call" | ||
} | ||
|
||
fn signature(&self) -> Signature { | ||
Signature::build(self.name()) | ||
.dbus_command() | ||
.accepts_dbus_client_options() | ||
.accepts_timeout() | ||
.input_output_type(Type::Nothing, Type::Any) | ||
.named( | ||
"signature", | ||
SyntaxShape::String, | ||
"Signature of the arguments to send, in D-Bus format.\n \ | ||
If not provided, they will be determined from introspection.\n \ | ||
If --no-introspect is specified and this is not provided, they will \ | ||
be guessed (poorly)", | ||
None, | ||
) | ||
.switch( | ||
"no-flatten", | ||
"Always return a list of all return values", | ||
None, | ||
) | ||
.switch( | ||
"no-introspect", | ||
"Don't use introspection to determine the correct argument signature", | ||
None, | ||
) | ||
.required_named( | ||
"dest", | ||
SyntaxShape::String, | ||
"The name of the connection to send the method to", | ||
None, | ||
) | ||
.required( | ||
"object", | ||
SyntaxShape::String, | ||
"The path to the object to call the method on", | ||
) | ||
.required( | ||
"interface", | ||
SyntaxShape::String, | ||
"The name of the interface the method belongs to", | ||
) | ||
.required( | ||
"method", | ||
SyntaxShape::String, | ||
"The name of the method to send", | ||
) | ||
.rest( | ||
"args", | ||
SyntaxShape::Any, | ||
"Arguments to send with the method call", | ||
) | ||
} | ||
|
||
fn usage(&self) -> &str { | ||
"Call a method and get its response" | ||
} | ||
|
||
fn extra_usage(&self) -> &str { | ||
"Returns an array if the method call returns more than one value." | ||
} | ||
|
||
fn search_terms(&self) -> Vec<&str> { | ||
vec!["dbus"] | ||
} | ||
|
||
fn examples(&self) -> Vec<Example> { | ||
vec![ | ||
Example { | ||
example: "dbus call --dest=org.freedesktop.DBus \ | ||
/org/freedesktop/DBus org.freedesktop.DBus.Peer Ping", | ||
description: "Ping the D-Bus server itself", | ||
result: None, | ||
}, | ||
Example { | ||
example: "dbus call --dest=org.freedesktop.Notifications \ | ||
/org/freedesktop/Notifications org.freedesktop.Notifications \ | ||
Notify \"Floppy disks\" 0 \"media-floppy\" \"Rarely seen\" \ | ||
\"But sometimes still used\" [] {} 5000", | ||
description: "Show a notification on the desktop for 5 seconds", | ||
result: None, | ||
}, | ||
] | ||
} | ||
|
||
fn run( | ||
&self, | ||
_plugin: &Self::Plugin, | ||
_engine: &EngineInterface, | ||
call: &EvaluatedCall, | ||
_input: &Value, | ||
) -> Result<Value, LabeledError> { | ||
let config = DbusClientConfig::try_from(call)?; | ||
let dbus = DbusClient::new(config)?; | ||
let values = dbus.call( | ||
&call.get_flag("dest")?.unwrap(), | ||
&call.req(0)?, | ||
&call.req(1)?, | ||
&call.req(2)?, | ||
call.get_flag("signature")?.as_ref(), | ||
&call.positional[3..], | ||
)?; | ||
|
||
let flatten = !call.get_flag::<bool>("no-flatten")?.unwrap_or(false); | ||
|
||
// Make the output easier to deal with by returning a list only if there are multiple return | ||
// values (not so common) | ||
match values.len() { | ||
0 if flatten => Ok(Value::nothing(call.head)), | ||
1 if flatten => Ok(values.into_iter().nth(0).unwrap()), | ||
_ => Ok(Value::list(values, call.head)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; | ||
use nu_protocol::{Example, LabeledError, Signature, SyntaxShape, Type, Value}; | ||
|
||
use crate::{client::DbusClient, config::DbusClientConfig, DbusSignatureUtilExt}; | ||
|
||
pub struct Get; | ||
|
||
impl SimplePluginCommand for Get { | ||
type Plugin = crate::NuPluginDbus; | ||
|
||
fn name(&self) -> &str { | ||
"dbus get" | ||
} | ||
|
||
fn signature(&self) -> Signature { | ||
Signature::build(self.name()) | ||
.dbus_command() | ||
.accepts_dbus_client_options() | ||
.accepts_timeout() | ||
.input_output_type(Type::Nothing, Type::Any) | ||
.required_named( | ||
"dest", | ||
SyntaxShape::String, | ||
"The name of the connection to read the property from", | ||
None, | ||
) | ||
.required( | ||
"object", | ||
SyntaxShape::String, | ||
"The path to the object to read the property from", | ||
) | ||
.required( | ||
"interface", | ||
SyntaxShape::String, | ||
"The name of the interface the property belongs to", | ||
) | ||
.required( | ||
"property", | ||
SyntaxShape::String, | ||
"The name of the property to read", | ||
) | ||
} | ||
|
||
fn usage(&self) -> &str { | ||
"Get a D-Bus property" | ||
} | ||
|
||
fn search_terms(&self) -> Vec<&str> { | ||
vec!["dbus", "property", "read"] | ||
} | ||
|
||
fn examples(&self) -> Vec<Example> { | ||
vec![Example { | ||
example: "dbus get --dest=org.mpris.MediaPlayer2.spotify \ | ||
/org/mpris/MediaPlayer2 \ | ||
org.mpris.MediaPlayer2.Player Metadata", | ||
description: "Get the currently playing song in Spotify", | ||
result: Some(Value::test_record(nu_protocol::record!( | ||
"xesam:title" => Value::test_string("Birdie"), | ||
"xesam:artist" => Value::test_list(vec![ | ||
Value::test_string("LOVE PSYCHEDELICO") | ||
]), | ||
"xesam:album" => Value::test_string("Love Your Love"), | ||
"xesam:url" => Value::test_string("https://open.spotify.com/track/51748BvzeeMs4PIdPuyZmv"), | ||
))), | ||
}] | ||
} | ||
|
||
fn run( | ||
&self, | ||
_plugin: &Self::Plugin, | ||
_engine: &EngineInterface, | ||
call: &EvaluatedCall, | ||
_input: &Value, | ||
) -> Result<Value, LabeledError> { | ||
let config = DbusClientConfig::try_from(call)?; | ||
let dbus = DbusClient::new(config)?; | ||
dbus.get( | ||
&call.get_flag("dest")?.unwrap(), | ||
&call.req(0)?, | ||
&call.req(1)?, | ||
&call.req(2)?, | ||
) | ||
} | ||
} |
Oops, something went wrong.