-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsinkship.go
61 lines (50 loc) · 1.45 KB
/
sinkship.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main
import (
"sync"
log "github.com/Sirupsen/logrus"
"github.com/digitalocean/godo"
)
// connects to your DO account and deletes all your Droplets
func main() {
// try a couple of different places to find the file
pat, err := getTokenFromChain(getTokenFromCli, getTokenFromEnv, getTokenFromFile)
if err != nil {
log.Fatalf("couldn't get token: %s", err.Error())
}
client := newClient(pat)
// get a list of all the droplets
droplets, _, err := client.Droplets.List(nil)
if err != nil {
log.Fatalf("couldn't get a list of your droplets: %s", err.Error())
}
if len(droplets) == 0 {
log.Info("You've got no droplets in your account.")
return
}
log.Infof("Found %d droplets, preparing to delete", len(droplets))
// the wait group is so we know how many concurrent requests to wait for
wg := &sync.WaitGroup{}
// the mutex is so that they print out in order
mut := &sync.Mutex{}
// this is the current index value
count := 0
for _, v := range droplets {
wg.Add(1)
go func(droplet godo.Droplet) {
_, err := client.Droplets.Delete(droplet.ID)
// lock the mutex, increment the count
mut.Lock()
count++
if err != nil {
log.Errorf("[%d] couldn't delete droplet %s (%d):\n\t%s", count, droplet.Name, droplet.ID, err)
} else {
log.Infof("[%d] deleted %s (%d)", count, droplet.Name, droplet.ID)
}
// done with the mutex
mut.Unlock()
wg.Done()
}(v)
}
// wait for all the goroutines to finish
wg.Wait()
}