Skip to content

Commit 4d0bd18

Browse files
committed
Merge pull request #34 from unihorn/43
feat(dbus): add GetUnitProperty interface
2 parents 88b39e2 + 4b8a4eb commit 4d0bd18

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

dbus/methods.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ func (c *Conn) GetUnitProperties(unit string) (map[string]interface{}, error) {
167167
return c.getProperties(unit, "org.freedesktop.systemd1.Unit")
168168
}
169169

170+
func (c *Conn) getProperty(unit string, dbusInterface string, propertyName string) (*Property, error) {
171+
var err error
172+
var prop dbus.Variant
173+
174+
path := ObjectPath("/org/freedesktop/systemd1/unit/" + unit)
175+
if !path.IsValid() {
176+
return nil, errors.New("invalid unit name: " + unit)
177+
}
178+
179+
obj := c.sysconn.Object("org.freedesktop.systemd1", path)
180+
err = obj.Call("org.freedesktop.DBus.Properties.Get", 0, dbusInterface, propertyName).Store(&prop)
181+
if err != nil {
182+
return nil, err
183+
}
184+
185+
return &Property{Name: propertyName, Value: prop}, nil
186+
}
187+
188+
func (c *Conn) GetUnitProperty(unit string, propertyName string) (*Property, error) {
189+
return c.getProperty(unit, "org.freedesktop.systemd1.Unit", propertyName)
190+
}
191+
170192
// GetUnitTypeProperties returns the extra properties for a unit, specific to the unit type.
171193
// Valid values for unitType: Service, Socket, Target, Device, Mount, Automount, Snapshot, Timer, Swap, Path, Slice, Scope
172194
// return "dbus.Error: Unknown interface" if the unitType is not the correct type of the unit
@@ -185,6 +207,10 @@ func (c *Conn) SetUnitProperties(name string, runtime bool, properties ...Proper
185207
return c.sysobj.Call("SetUnitProperties", 0, name, runtime, properties).Store()
186208
}
187209

210+
func (c *Conn) GetUnitTypeProperty(unit string, unitType string, propertyName string) (*Property, error) {
211+
return c.getProperty(unit, "org.freedesktop.systemd1." + unitType, propertyName)
212+
}
213+
188214
// ListUnits returns an array with all currently loaded units. Note that
189215
// units may be known by multiple names at the same time, and hence there might
190216
// be more unit names loaded than actual units behind them.

dbus/methods_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"math/rand"
2323
"os"
2424
"path/filepath"
25+
"reflect"
2526
"testing"
2627
)
2728

@@ -174,6 +175,20 @@ func TestGetUnitProperties(t *testing.T) {
174175
if names[0] != "system.slice" {
175176
t.Fatal("unexpected wants for /")
176177
}
178+
179+
prop, err := conn.GetUnitProperty(unit, "Wants")
180+
if err != nil {
181+
t.Fatal(err)
182+
}
183+
184+
if prop.Name != "Wants" {
185+
t.Fatal("unexpected property name")
186+
}
187+
188+
val := prop.Value.Value().([]string)
189+
if !reflect.DeepEqual(val, names) {
190+
t.Fatal("unexpected property value")
191+
}
177192
}
178193

179194
// TestGetUnitPropertiesRejectsInvalidName attempts to get the properties for a
@@ -185,7 +200,11 @@ func TestGetUnitPropertiesRejectsInvalidName(t *testing.T) {
185200
unit := "//invalid#$^/"
186201

187202
_, err := conn.GetUnitProperties(unit)
203+
if err == nil {
204+
t.Fatal("Expected an error, got nil")
205+
}
188206

207+
_, err = conn.GetUnitProperty(unit, "Wants")
189208
if err == nil {
190209
t.Fatal("Expected an error, got nil")
191210
}

0 commit comments

Comments
 (0)