-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdestroy.go
More file actions
64 lines (57 loc) · 1.35 KB
/
destroy.go
File metadata and controls
64 lines (57 loc) · 1.35 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package fleet
import (
"fmt"
"strings"
"sync"
"time"
)
// Destroy units for a given target
func (c *FleetClient) Destroy(targets []string, wg *sync.WaitGroup, outchan chan string, errchan chan error) {
for _, target := range targets {
wg.Add(1)
go doDestroy(c, target, wg, outchan, errchan)
}
return
}
func doDestroy(c *FleetClient, target string, wg *sync.WaitGroup, outchan chan string, errchan chan error) {
defer wg.Done()
// prepare string representation
component, num, err := splitTarget(target)
if err != nil {
errchan <- err
return
}
name, err := formatUnitName(component, num)
if err != nil {
errchan <- err
return
}
destroyed := fmt.Sprintf("\033[0;33m%v:\033[0m destroyed \r", name)
// bail early if unit doesn't exist
_, err = c.Units(name)
if err != nil {
if strings.Contains(err.Error(), "could not find unit") {
outchan <- destroyed
}
return
}
// otherwise destroy it
if err = c.Fleet.DestroyUnit(name); err != nil {
// ignore already destroyed units
if !strings.Contains(err.Error(), "could not find unit") {
errchan <- err
return
}
}
// loop until actually destroyed
for {
_, err = c.Units(name)
if err != nil {
if strings.Contains(err.Error(), "could not find unit") {
outchan <- destroyed
return
}
}
time.Sleep(250 * time.Millisecond)
}
}