11use nu_plugin:: { serve_plugin, MsgPackSerializer , Plugin , EvaluatedCall , LabeledError } ;
2- use nu_protocol:: { PluginSignature , Value } ;
2+ use nu_protocol:: { PluginSignature , Value , SyntaxShape , PluginExample } ;
3+
4+ mod config;
5+ mod client;
6+
7+ use config:: * ;
8+ use client:: * ;
39
410fn main ( ) {
511 serve_plugin ( & mut NuPluginDbus , MsgPackSerializer )
612}
713
14+ /// The main plugin interface for nushell
815struct NuPluginDbus ;
916
1017impl Plugin for NuPluginDbus {
1118 fn signature ( & self ) -> Vec < PluginSignature > {
1219 vec ! [
1320 PluginSignature :: build( "dbus" )
14- . usage( "Commands for interacting with D-Bus" )
15- . search_terms( vec![ "dbus" . into( ) ] )
16- . category( nu_protocol:: Category :: Platform ) ,
21+ . is_dbus_command( )
22+ . usage( "Commands for interacting with D-Bus" ) ,
23+ PluginSignature :: build( "dbus call" )
24+ . is_dbus_command( )
25+ . accepts_dbus_client_options( )
26+ . usage( "Call a method and get its response" )
27+ . named( "timeout" , SyntaxShape :: Duration , "How long to wait for a response" , None )
28+ . required_named( "dest" , SyntaxShape :: String ,
29+ "The name of the connection to send the method to" ,
30+ None )
31+ . required( "object" , SyntaxShape :: String ,
32+ "The path to the object to call the method on" )
33+ . required( "interface" , SyntaxShape :: String ,
34+ "The name of the interface the method belongs to" )
35+ . required( "method" , SyntaxShape :: String ,
36+ "The name of the method to send" )
37+ . plugin_examples( vec![
38+ PluginExample {
39+ example: "dbus call --dest=org.freedesktop.DBus \
40+ /org/freedesktop/DBus org.freedesktop.DBus.Peer Ping". into( ) ,
41+ description: "Ping the D-Bus server itself" . into( ) ,
42+ result: None
43+ }
44+ ] ) ,
1745 ]
1846 }
1947
2048 fn run (
2149 & mut self ,
2250 name : & str ,
2351 call : & EvaluatedCall ,
24- input : & Value ,
52+ _input : & Value ,
2553 ) -> Result < Value , LabeledError > {
2654 match name {
2755 "dbus" => Err ( LabeledError {
@@ -30,6 +58,8 @@ impl Plugin for NuPluginDbus {
3058 span : Some ( call. head )
3159 } ) ,
3260
61+ "dbus call" => self . call ( call) ,
62+
3363 _ => Err ( LabeledError {
3464 label : "Plugin invoked with unknown command name" . into ( ) ,
3565 msg : "unknown command" . into ( ) ,
@@ -38,3 +68,42 @@ impl Plugin for NuPluginDbus {
3868 }
3969 }
4070}
71+
72+ /// For conveniently adding the base options to a dbus command
73+ trait DbusSignatureUtilExt {
74+ fn is_dbus_command ( self ) -> Self ;
75+ fn accepts_dbus_client_options ( self ) -> Self ;
76+ }
77+
78+ impl DbusSignatureUtilExt for PluginSignature {
79+ fn is_dbus_command ( self ) -> Self {
80+ self . search_terms ( vec ! [ "dbus" . into( ) ] )
81+ . category ( nu_protocol:: Category :: Platform )
82+ }
83+
84+ fn accepts_dbus_client_options ( self ) -> Self {
85+ self . switch ( "session" , "Send to the session message bus (default)" , None )
86+ . switch ( "system" , "Send to the system message bus" , None )
87+ . switch ( "started" , "Send to the bus that started this process, if applicable" , None )
88+ . named ( "bus" , SyntaxShape :: String , "Send to the bus server at the given address" , None )
89+ . named ( "peer" , SyntaxShape :: String ,
90+ "Send to a non-bus D-Bus server at the given address. \
91+ Will not call the Hello method on initialization.",
92+ None )
93+ }
94+ }
95+
96+ impl NuPluginDbus {
97+ fn call ( & self , call : & EvaluatedCall ) -> Result < Value , LabeledError > {
98+ let config = DbusClientConfig :: try_from ( call) ?;
99+ let dbus = DbusClient :: new ( config) ?;
100+ dbus. call (
101+ & call. get_flag ( "dest" ) ?. unwrap ( ) ,
102+ & call. req ( 0 ) ?,
103+ & call. req ( 1 ) ?,
104+ & call. req ( 2 ) ?,
105+ ) ?;
106+ // TODO handle response
107+ Ok ( Value :: nothing ( call. head ) )
108+ }
109+ }
0 commit comments