From 0380753104c032874beb9423ab365c8c9aee67a0 Mon Sep 17 00:00:00 2001 From: Callan Bryant Date: Thu, 5 Mar 2020 20:35:51 +0000 Subject: [PATCH] add remove cmd --- cmd/dsnet.go | 3 +++ configtypes.go | 20 ++++++++++++++++++++ remove.go | 15 +++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 remove.go diff --git a/cmd/dsnet.go b/cmd/dsnet.go index f84bd72..cbc6b22 100644 --- a/cmd/dsnet.go +++ b/cmd/dsnet.go @@ -31,6 +31,9 @@ func main() { case "report": dsnet.Report() + case "remove": + dsnet.Remove() + case "down": dsnet.Down() diff --git a/configtypes.go b/configtypes.go index 07ff462..0fe24c7 100644 --- a/configtypes.go +++ b/configtypes.go @@ -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 diff --git a/remove.go b/remove.go new file mode 100644 index 0000000..1f3d1f3 --- /dev/null +++ b/remove.go @@ -0,0 +1,15 @@ +package dsnet + +import ( + "os" +) + +func Remove() { + if len(os.Args) <= 2 { + ExitFail("Hostname argument required: dsnet remove ") + } + conf := MustLoadDsnetConfig() + hostname := os.Args[2] + conf.MustRemovePeer(hostname) + conf.MustSave() +}