/
device.go
40 lines (33 loc) · 1.13 KB
/
device.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package syncthing
import (
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/protocol"
)
// GetDevice takes a device ID, looks in the current configuration and returns
// that device.
func (s *Server) GetDevice(id protocol.DeviceID) *config.DeviceConfiguration {
for _, device := range s.Config.Devices {
if device.DeviceID == id {
return &device
}
}
return nil
}
// SetDevice takes a complete device and adds it to the local configuration.
// Server.Update() will then save this. Note that if the device already exists
// it is simply overwritten.
func (s *Server) SetDevice(device *config.DeviceConfiguration) error {
s.RemoveDevice(device.DeviceID)
s.Config.Devices = append(s.Config.Devices, *device)
return nil
}
// RemoveDevice takes a device id and removes it from the local configuration.
// Server.Update() will then save this.
func (s *Server) RemoveDevice(id protocol.DeviceID) {
for i, device := range s.Config.Devices {
if device.DeviceID == id {
s.Config.Devices[i] = s.Config.Devices[len(s.Config.Devices)-1]
s.Config.Devices = s.Config.Devices[:len(s.Config.Devices)-1]
}
}
}