add flag to output to file
This commit is contained in:
parent
9028d9a69e
commit
f7a2922d7d
35
main.go
35
main.go
@ -1,12 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
@ -21,6 +20,8 @@ func init() {
|
|||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
cfAPIKey := flag.String("cfapikey", "", "in cf, my profile -> API Tokens -> Create Token")
|
cfAPIKey := flag.String("cfapikey", "", "in cf, my profile -> API Tokens -> Create Token")
|
||||||
|
onlyZone := flag.String("onlyZone", "", "if specified, only the zone with this name will be processed. If omitted, all zones will be processed")
|
||||||
|
outFile := flag.String("outFile", "cfcleaner.csv", "the path of the output file")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
if *cfAPIKey == "" {
|
if *cfAPIKey == "" {
|
||||||
@ -38,6 +39,9 @@ func main() {
|
|||||||
outRows := make([]cf.Record, 0)
|
outRows := make([]cf.Record, 0)
|
||||||
|
|
||||||
for _, zone := range zones {
|
for _, zone := range zones {
|
||||||
|
if *onlyZone != "" && zone.Name != *onlyZone {
|
||||||
|
continue
|
||||||
|
}
|
||||||
log.Printf("processing zone '%s' with ID '%s'", zone.Name, zone.ID)
|
log.Printf("processing zone '%s' with ID '%s'", zone.Name, zone.ID)
|
||||||
|
|
||||||
records, err := cfClient.GetRecords(zone)
|
records, err := cfClient.GetRecords(zone)
|
||||||
@ -60,20 +64,25 @@ func main() {
|
|||||||
return outRows[i].NumberQueries < outRows[j].NumberQueries
|
return outRows[i].NumberQueries < outRows[j].NumberQueries
|
||||||
})
|
})
|
||||||
|
|
||||||
buf := bytes.NewBuffer(nil)
|
f, err := os.OpenFile(*outFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
||||||
writer := csv.NewWriter(buf)
|
if err != nil {
|
||||||
writer.Write([]string{"Name", "Type", "NumberQueries", "CreatedOn", "ModifiedOn", "Comment", "Content"})
|
log.Fatalf("failed to open output file '%s': %v", *outFile, err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
writer := csv.NewWriter(f)
|
||||||
|
defer writer.Flush()
|
||||||
|
err = writer.Write([]string{"Name", "Type", "NumberQueries", "CreatedOn", "ModifiedOn", "Comment", "Content"})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to write to outFile: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
for _, row := range outRows {
|
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})
|
err = 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 {
|
if err != nil {
|
||||||
log.Fatalf("failed to read bytes from output buffer: %v", err)
|
log.Fatalf("failed to write to outFile: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(b))
|
fmt.Printf("wrote to file: %s\n", *outFile)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user