add remove cmd

This commit is contained in:
Callan Bryant 2020-03-05 20:35:51 +00:00
parent b6bd2d33e1
commit 0380753104
No known key found for this signature in database
GPG Key ID: C31FA9DF3ACBFFAA
3 changed files with 38 additions and 0 deletions

View File

@ -31,6 +31,9 @@ func main() {
case "report":
dsnet.Report()
case "remove":
dsnet.Remove()
case "down":
dsnet.Down()

View File

@ -110,6 +110,26 @@ func (conf *DsnetConfig) MustAddPeer(peer PeerConfig) {
conf.Peers = append(conf.Peers, peer)
}
func (conf *DsnetConfig) MustRemovePeer(hostname string) {
peerIndex := -1;
for i, peer := range conf.Peers {
if peer.Hostname == hostname {
peerIndex = i
}
}
if peerIndex == -1 {
ExitFail("Could not find peer with hostname %s", hostname)
}
// remove peer from slice (by moving the last element to peerIndex, and
// truncating)
conf.Peers[peerIndex] = conf.Peers[len(conf.Peers)-1]
conf.Peers = conf.Peers[:len(conf.Peers)-1]
}
func (conf DsnetConfig) IPAllocated(IP net.IP) bool {
if IP.Equal(conf.IP) {
return true

15
remove.go Normal file
View File

@ -0,0 +1,15 @@
package dsnet
import (
"os"
)
func Remove() {
if len(os.Args) <= 2 {
ExitFail("Hostname argument required: dsnet remove <hostname>")
}
conf := MustLoadDsnetConfig()
hostname := os.Args[2]
conf.MustRemovePeer(hostname)
conf.MustSave()
}