package main import ( "bytes" "encoding/csv" "flag" "fmt" "io" "log" "sort" "strconv" "time" "code.stevenpolley.net/steven/cfcleaner/cf" ) func init() { } func main() { cfAPIKey := flag.String("cfapikey", "", "in cf, my profile -> API Tokens -> Create Token") flag.Parse() if *cfAPIKey == "" { fmt.Println("you must specify an API key with the -cfapikey= parameter") return } cfClient := cf.NewClient(*cfAPIKey) zones, err := cfClient.GetZones() if err != nil { log.Fatalf("failed to get zones: %v", err) } outRows := make([]cf.Record, 0) for _, zone := range zones { log.Printf("processing zone '%s' with ID '%s'", zone.Name, zone.ID) records, err := cfClient.GetRecords(zone) if err != nil { log.Printf("failed to get records for zone '%s' with ID '%s': %v", zone.Name, zone.ID, err) } for _, record := range records { analytic, err := cfClient.GetRecordAnalytic(record) if err != nil { log.Printf("failed to get record report for record '%s' in zone '%s' with ID '%s': %v", record.Name, zone.Name, zone.ID, err) continue } record.NumberQueries = analytic.Result.Totals.QueryCount outRows = append(outRows, record) } } sort.Slice(outRows, func(i, j int) bool { return outRows[i].NumberQueries < outRows[j].NumberQueries }) buf := bytes.NewBuffer(nil) writer := csv.NewWriter(buf) writer.Write([]string{"Name", "Type", "NumberQueries", "CreatedOn", "ModifiedOn", "Comment", "Content"}) for _, row := range outRows { writer.Write([]string{row.Name, row.Type, strconv.Itoa(row.NumberQueries), row.CreatedOn.Format(time.RFC3339), row.ModifiedOn.Format(time.RFC3339), row.Comment, row.Content}) } writer.Flush() b, err := io.ReadAll(buf) if err != nil { log.Fatalf("failed to read bytes from output buffer: %v", err) } fmt.Println(string(b)) }