-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
76 lines (71 loc) · 1.64 KB
/
main.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"context"
"fmt"
"github.com/cloudflare/cloudflare-go"
"github.com/manifoldco/promptui"
"log"
"os"
)
func main() {
token := os.Getenv("CLOUDFLARE_API_TOKEN")
if token == "" {
token = promptSecret("Enter your CF api token")
}
api, err := cloudflare.NewWithAPIToken(token)
if err != nil {
log.Fatal(err)
}
ctx := context.Background()
zoneName := promptInput("Enter the Zone Name")
id, err := api.ZoneIDByName(zoneName)
if err != nil {
log.Fatal(err)
}
recordType := promptInput("Enter the DNS record type")
log.Printf("Listing %s records. This may take a few seconds...", recordType)
records, err := api.DNSRecords(ctx, id, cloudflare.DNSRecord{Type: recordType})
if err != nil {
log.Fatal(err)
}
confirm := fmt.Sprintf("Found %d records. Continue with delete?", len(records))
if !promptConfirm(confirm) {
return
}
for index, record := range records {
if err := api.DeleteDNSRecord(ctx, id, record.ID); err != nil {
log.Fatalf("Failed to delete record %s: %v", record.Name, err)
}
log.Printf("Deleted %s (%d/%d)", record.Name, index, len(records))
}
}
func promptSecret(text string) string {
p := promptui.Prompt{
Label: text,
HideEntered: true,
}
value, err := p.Run()
if err != nil {
log.Fatal(err)
}
return value
}
func promptInput(text string) string {
p := promptui.Prompt{Label: text}
value, err := p.Run()
if err != nil {
log.Fatal(err)
}
return value
}
func promptConfirm(question string) bool {
prompt := promptui.Select{
Label: question,
Items: []string{"Yes", "No"},
}
_, answer, err := prompt.Run()
if err != nil {
log.Fatal(err)
}
return answer == "Yes"
}