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 GetAll;
7
8impl SimplePluginCommand for GetAll {
9 type Plugin = crate::NuPluginDbus;
10
11 fn name(&self) -> &str {
12 "dbus get-all"
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::Record([].into()))
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 }
38
39 fn description(&self) -> &str {
40 "Get all D-Bus properties for the given object"
41 }
42
43 fn search_terms(&self) -> Vec<&str> {
44 vec!["dbus", "properties", "property", "get"]
45 }
46
47 fn examples(&self) -> Vec<Example<'_>> {
48 vec![Example {
49 example: "dbus get-all --dest=org.mpris.MediaPlayer2.spotify \
50 /org/mpris/MediaPlayer2 \
51 org.mpris.MediaPlayer2.Player",
52 description: "Get the current player state of Spotify",
53 result: Some(Value::test_record(nu_protocol::record!(
54 "CanPlay" => Value::test_bool(true),
55 "Volume" => Value::test_float(0.43),
56 "PlaybackStatus" => Value::test_string("Paused"),
57 ))),
58 }]
59 }
60
61 fn run(
62 &self,
63 _plugin: &Self::Plugin,
64 _engine: &EngineInterface,
65 call: &EvaluatedCall,
66 _input: &Value,
67 ) -> Result<Value, LabeledError> {
68 let config = DbusClientConfig::try_from(call)?;
69 let dbus = DbusClient::new(config)?;
70 dbus.get_all(
71 &call.get_flag("dest")?.unwrap(),
72 &call.req(0)?,
73 &call.req(1)?,
74 )
75 }
76}