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 Set;
7
8impl SimplePluginCommand for Set {
9 type Plugin = crate::NuPluginDbus;
10
11 fn name(&self) -> &str {
12 "dbus set"
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::Nothing)
21 .named(
22 "signature",
23 SyntaxShape::String,
24 "Signature of the value to set, in D-Bus format.\n \
25 If not provided, it will be determined from introspection.\n \
26 If --no-introspect is specified and this is not provided, it will \
27 be guessed (poorly)",
28 None,
29 )
30 .required_named(
31 "dest",
32 SyntaxShape::String,
33 "The name of the connection to write the property on",
34 None,
35 )
36 .required(
37 "object",
38 SyntaxShape::String,
39 "The path to the object to write the property on",
40 )
41 .required(
42 "interface",
43 SyntaxShape::String,
44 "The name of the interface the property belongs to",
45 )
46 .required(
47 "property",
48 SyntaxShape::String,
49 "The name of the property to write",
50 )
51 .required(
52 "value",
53 SyntaxShape::Any,
54 "The value to write to the property",
55 )
56 }
57
58 fn description(&self) -> &str {
59 "Set a D-Bus property"
60 }
61
62 fn search_terms(&self) -> Vec<&str> {
63 vec!["dbus", "property", "write", "put"]
64 }
65
66 fn examples(&self) -> Vec<Example<'_>> {
67 vec![Example {
68 example: "dbus set --dest=org.mpris.MediaPlayer2.spotify \
69 /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player \
70 Volume 0.5",
71 description: "Set the volume of Spotify to 50%",
72 result: None,
73 }]
74 }
75
76 fn run(
77 &self,
78 _plugin: &Self::Plugin,
79 _engine: &EngineInterface,
80 call: &EvaluatedCall,
81 _input: &Value,
82 ) -> Result<Value, LabeledError> {
83 let config = DbusClientConfig::try_from(call)?;
84 let dbus = DbusClient::new(config)?;
85 dbus.set(
86 &call.get_flag("dest")?.unwrap(),
87 &call.req(0)?,
88 &call.req(1)?,
89 &call.req(2)?,
90 call.get_flag("signature")?.as_ref(),
91 &call.req(3)?,
92 )?;
93 Ok(Value::nothing(call.head))
94 }
95}