Nushell plugin for interacting with D-Bus
1use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand};
2use nu_protocol::{Example, LabeledError, Signature, SyntaxShape, Type, Value};
3
4use crate::{DbusSignatureUtilExt, client::DbusClient, config::DbusClientConfig};
5
6pub struct Get;
7
8impl SimplePluginCommand for Get {
9 type Plugin = crate::NuPluginDbus;
10
11 fn name(&self) -> &str {
12 "dbus get"
13 }
14
15 fn signature(&self) -> Signature {
16 Signature::build(self.name())
17 .dbus_command()
18 .accepts_dbus_client_options()
19 .accepts_timeout()
20 .input_output_type(Type::Nothing, Type::Any)
21 .required_named(
22 "dest",
23 SyntaxShape::String,
24 "The name of the connection to read the property from",
25 None,
26 )
27 .required(
28 "object",
29 SyntaxShape::String,
30 "The path to the object to read the property from",
31 )
32 .required(
33 "interface",
34 SyntaxShape::String,
35 "The name of the interface the property belongs to",
36 )
37 .required(
38 "property",
39 SyntaxShape::String,
40 "The name of the property to read",
41 )
42 }
43
44 fn description(&self) -> &str {
45 "Get a D-Bus property"
46 }
47
48 fn search_terms(&self) -> Vec<&str> {
49 vec!["dbus", "property", "read"]
50 }
51
52 fn examples(&self) -> Vec<Example<'_>> {
53 vec![Example {
54 example: "dbus get --dest=org.mpris.MediaPlayer2.spotify \
55 /org/mpris/MediaPlayer2 \
56 org.mpris.MediaPlayer2.Player Metadata",
57 description: "Get the currently playing song in Spotify",
58 result: Some(Value::test_record(nu_protocol::record!(
59 "xesam:title" => Value::test_string("Birdie"),
60 "xesam:artist" => Value::test_list(vec![
61 Value::test_string("LOVE PSYCHEDELICO")
62 ]),
63 "xesam:album" => Value::test_string("Love Your Love"),
64 "xesam:url" => Value::test_string("https://open.spotify.com/track/51748BvzeeMs4PIdPuyZmv"),
65 ))),
66 }]
67 }
68
69 fn run(
70 &self,
71 _plugin: &Self::Plugin,
72 _engine: &EngineInterface,
73 call: &EvaluatedCall,
74 _input: &Value,
75 ) -> Result<Value, LabeledError> {
76 let config = DbusClientConfig::try_from(call)?;
77 let dbus = DbusClient::new(config)?;
78 dbus.get(
79 &call.get_flag("dest")?.unwrap(),
80 &call.req(0)?,
81 &call.req(1)?,
82 &call.req(2)?,
83 )
84 }
85}