Nushell plugin for interacting with D-Bus
at main 90 lines 3.1 kB view raw
1use nu_plugin::{EngineInterface, EvaluatedCall, SimplePluginCommand}; 2use nu_protocol::{Example, LabeledError, Signature, SyntaxShape, Type, Value}; 3 4use crate::{DbusSignatureUtilExt, client::DbusClient, config::DbusClientConfig, pattern::Pattern}; 5 6pub struct List; 7 8impl SimplePluginCommand for List { 9 type Plugin = crate::NuPluginDbus; 10 11 fn name(&self) -> &str { 12 "dbus list" 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::List(Type::String.into())) 21 .optional( 22 "pattern", 23 SyntaxShape::String, 24 "An optional glob-like pattern to filter the result by", 25 ) 26 } 27 28 fn description(&self) -> &str { 29 "List all available connection names on the bus" 30 } 31 32 fn extra_description(&self) -> &str { 33 "These can be used as arguments for --dest on any of the other commands." 34 } 35 36 fn search_terms(&self) -> Vec<&str> { 37 vec!["dbus", "list", "find", "search", "help"] 38 } 39 40 fn examples(&self) -> Vec<Example<'_>> { 41 vec![ 42 Example { 43 example: "dbus list", 44 description: "List all names available on the bus", 45 result: None, 46 }, 47 Example { 48 example: "dbus list org.freedesktop.*", 49 description: "List top-level freedesktop.org names on the bus \ 50 (e.g. matches `org.freedesktop.PowerManagement`, \ 51 but not `org.freedesktop.Management.Inhibit`)", 52 result: Some(Value::test_list(vec![ 53 Value::test_string("org.freedesktop.DBus"), 54 Value::test_string("org.freedesktop.Flatpak"), 55 Value::test_string("org.freedesktop.Notifications"), 56 ])), 57 }, 58 Example { 59 example: "dbus list org.mpris.MediaPlayer2.**", 60 description: "List all MPRIS2 media players on the bus", 61 result: Some(Value::test_list(vec![ 62 Value::test_string("org.mpris.MediaPlayer2.spotify"), 63 Value::test_string("org.mpris.MediaPlayer2.kdeconnect.mpris_000001"), 64 ])), 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 let pattern = call 79 .opt::<String>(0)? 80 .map(|pat| Pattern::new(&pat, Some('.'))); 81 let result = dbus.list(pattern.as_ref())?; 82 Ok(Value::list( 83 result 84 .into_iter() 85 .map(|s| Value::string(s, call.head)) 86 .collect(), 87 call.head, 88 )) 89 } 90}