@@ -6,10 +6,13 @@ mod client;
66mod convert;
77mod dbus_type;
88mod introspection;
9+ mod pattern;
910
1011use config:: * ;
1112use client:: * ;
1213
14+ use crate :: pattern:: Pattern ;
15+
1316fn main ( ) {
1417 serve_plugin ( & mut NuPluginDbus , MsgPackSerializer )
1518}
@@ -29,10 +32,10 @@ impl Plugin for NuPluginDbus {
2932 PluginSignature :: build( "dbus introspect" )
3033 . is_dbus_command( )
3134 . accepts_dbus_client_options( )
35+ . accepts_timeout( )
3236 . usage( "Introspect a D-Bus object" )
3337 . extra_usage( "Returns information about available nodes, interfaces, methods, \
3438 signals, and properties on the given object path")
35- . named( "timeout" , SyntaxShape :: Duration , "How long to wait for a response" , None )
3639 . required_named( "dest" , SyntaxShape :: String ,
3740 "The name of the connection that owns the object" ,
3841 None )
@@ -63,9 +66,9 @@ impl Plugin for NuPluginDbus {
6366 PluginSignature :: build( "dbus call" )
6467 . is_dbus_command( )
6568 . accepts_dbus_client_options( )
69+ . accepts_timeout( )
6670 . usage( "Call a method and get its response" )
6771 . extra_usage( "Returns an array if the method call returns more than one value." )
68- . named( "timeout" , SyntaxShape :: Duration , "How long to wait for a response" , None )
6972 . named( "signature" , SyntaxShape :: String ,
7073 "Signature of the arguments to send, in D-Bus format.\n \
7174 If not provided, they will be determined from introspection.\n \
@@ -105,8 +108,8 @@ impl Plugin for NuPluginDbus {
105108 PluginSignature :: build( "dbus get" )
106109 . is_dbus_command( )
107110 . accepts_dbus_client_options( )
111+ . accepts_timeout( )
108112 . usage( "Get a D-Bus property" )
109- . named( "timeout" , SyntaxShape :: Duration , "How long to wait for a response" , None )
110113 . required_named( "dest" , SyntaxShape :: String ,
111114 "The name of the connection to read the property from" ,
112115 None )
@@ -135,8 +138,8 @@ impl Plugin for NuPluginDbus {
135138 PluginSignature :: build( "dbus get-all" )
136139 . is_dbus_command( )
137140 . accepts_dbus_client_options( )
141+ . accepts_timeout( )
138142 . usage( "Get all D-Bus property for the given objects" )
139- . named( "timeout" , SyntaxShape :: Duration , "How long to wait for a response" , None )
140143 . required_named( "dest" , SyntaxShape :: String ,
141144 "The name of the connection to read the property from" ,
142145 None )
@@ -160,8 +163,8 @@ impl Plugin for NuPluginDbus {
160163 PluginSignature :: build( "dbus set" )
161164 . is_dbus_command( )
162165 . accepts_dbus_client_options( )
166+ . accepts_timeout( )
163167 . usage( "Get all D-Bus property for the given objects" )
164- . named( "timeout" , SyntaxShape :: Duration , "How long to wait for a response" , None )
165168 . named( "signature" , SyntaxShape :: String ,
166169 "Signature of the value to set, in D-Bus format.\n \
167170 If not provided, it will be determined from introspection.\n \
@@ -187,6 +190,40 @@ impl Plugin for NuPluginDbus {
187190 result: None ,
188191 } ,
189192 ] ) ,
193+ PluginSignature :: build( "dbus list" )
194+ . is_dbus_command( )
195+ . accepts_dbus_client_options( )
196+ . accepts_timeout( )
197+ . usage( "List all available connection names on the bus" )
198+ . extra_usage( "These can be used as arguments for --dest on any of the other commands." )
199+ . optional( "pattern" , SyntaxShape :: String ,
200+ "An optional glob-like pattern to filter the result by" )
201+ . plugin_examples( vec![
202+ PluginExample {
203+ example: "dbus list" . into( ) ,
204+ description: "List all names available on the bus" . into( ) ,
205+ result: None ,
206+ } ,
207+ PluginExample {
208+ example: "dbus list org.freedesktop.*" . into( ) ,
209+ description: "List top-level freedesktop.org names on the bus \
210+ (e.g. matches `org.freedesktop.PowerManagement`, \
211+ but not `org.freedesktop.Management.Inhibit`)". into( ) ,
212+ result: Some ( Value :: list( vec![
213+ str !( "org.freedesktop.DBus" ) ,
214+ str !( "org.freedesktop.Flatpak" ) ,
215+ str !( "org.freedesktop.Notifications" ) ,
216+ ] , Span :: unknown( ) ) ) ,
217+ } ,
218+ PluginExample {
219+ example: "dbus list org.mpris.MediaPlayer2.**" . into( ) ,
220+ description: "List all MPRIS2 media players on the bus" . into( ) ,
221+ result: Some ( Value :: list( vec![
222+ str !( "org.mpris.MediaPlayer2.spotify" ) ,
223+ str !( "org.mpris.MediaPlayer2.kdeconnect.mpris_000001" ) ,
224+ ] , Span :: unknown( ) ) ) ,
225+ } ,
226+ ] )
190227 ]
191228 }
192229
@@ -208,6 +245,7 @@ impl Plugin for NuPluginDbus {
208245 "dbus get" => self . get ( call) ,
209246 "dbus get-all" => self . get_all ( call) ,
210247 "dbus set" => self . set ( call) ,
248+ "dbus list" => self . list ( call) ,
211249
212250 _ => Err ( LabeledError {
213251 label : "Plugin invoked with unknown command name" . into ( ) ,
@@ -222,6 +260,7 @@ impl Plugin for NuPluginDbus {
222260trait DbusSignatureUtilExt {
223261 fn is_dbus_command ( self ) -> Self ;
224262 fn accepts_dbus_client_options ( self ) -> Self ;
263+ fn accepts_timeout ( self ) -> Self ;
225264}
226265
227266impl DbusSignatureUtilExt for PluginSignature {
@@ -240,6 +279,10 @@ impl DbusSignatureUtilExt for PluginSignature {
240279 Will not call the Hello method on initialization.",
241280 None )
242281 }
282+
283+ fn accepts_timeout ( self ) -> Self {
284+ self . named ( "timeout" , SyntaxShape :: Duration , "How long to wait for a response" , None )
285+ }
243286}
244287
245288impl NuPluginDbus {
@@ -310,4 +353,14 @@ impl NuPluginDbus {
310353 ) ?;
311354 Ok ( Value :: nothing ( call. head ) )
312355 }
356+
357+ fn list ( & self , call : & EvaluatedCall ) -> Result < Value , LabeledError > {
358+ let config = DbusClientConfig :: try_from ( call) ?;
359+ let dbus = DbusClient :: new ( config) ?;
360+ let pattern = call. opt :: < String > ( 0 ) ?. map ( |pat| Pattern :: new ( & pat, Some ( '.' ) ) ) ;
361+ let result = dbus. list ( pattern. as_ref ( ) ) ?;
362+ Ok ( Value :: list (
363+ result. into_iter ( ) . map ( |s| Value :: string ( s, call. head ) ) . collect ( ) ,
364+ call. head ) )
365+ }
313366}
0 commit comments