mirror of
https://github.com/0x1d/rcond.git
synced 2026-03-22 09:04:16 +01:00
feat: add reboot and shutdown
This commit is contained in:
23
pkg/system/dbus.go
Normal file
23
pkg/system/dbus.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/godbus/dbus/v5"
|
||||
)
|
||||
|
||||
// WithDbus executes the given function with a D-Bus system connection
|
||||
// and handles any connection errors
|
||||
func WithDbus(fn func(*dbus.Conn) error) error {
|
||||
conn, err := dbus.SystemBus()
|
||||
if err != nil {
|
||||
log.Printf("Failed to connect to system bus: %v", err)
|
||||
return err
|
||||
}
|
||||
if err := fn(conn); err != nil {
|
||||
log.Print(err)
|
||||
return err
|
||||
}
|
||||
conn.Close()
|
||||
return nil
|
||||
}
|
||||
33
pkg/system/state.go
Normal file
33
pkg/system/state.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/godbus/dbus/v5"
|
||||
)
|
||||
|
||||
// Restart restarts the system.
|
||||
func Restart() error {
|
||||
return WithDbus(func(conn *dbus.Conn) error {
|
||||
obj := conn.Object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
|
||||
log.Println("Rebooting system...")
|
||||
call := obj.Call("org.freedesktop.systemd1.Manager.Reboot", 0)
|
||||
if call.Err != nil {
|
||||
log.Fatal(call.Err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Shutdown shuts down the system.
|
||||
func Shutdown() error {
|
||||
return WithDbus(func(conn *dbus.Conn) error {
|
||||
obj := conn.Object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
|
||||
log.Println("Shutting down system...")
|
||||
call := obj.Call("org.freedesktop.systemd1.Manager.PowerOff", 0)
|
||||
if call.Err != nil {
|
||||
log.Fatal(call.Err)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user