<commit_before>package dns

import (
	"os"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/bparli/goavail/notify"
	"github.com/cloudflare/cloudflare-go"
)

type CFlare struct {
	DnsDomain string
	Proxied   bool
	Addresses []string
	Hostnames []string
}

func (r *CFlare) formatHostname(host string) string {
	if strings.Contains(host, r.DnsDomain) {
		return host
	}
	return host + "." + r.DnsDomain
}

func (r *CFlare) AddIp(ipAddress string, dryRun bool) error {
	if dryRun {
		log.Infoln("Dry Run is True.  Would have updated DNS for address " + ipAddress)
	} else {
		for _, name := range r.Hostnames {
			err := r.addDNSName(name, ipAddress)
			if err != nil {
				return err
			}
		}
	}
	if notify.SlackNotify.UseSlack == true {
		notify.SlackNotify.SendToSlack(ipAddress, r.DnsDomain, "Added", dryRun)
	}
	return nil
}

func (r *CFlare) addDNSName(name string, ipAddress string) error {
	// Construct a new API object
	log.Infoln("Adding", name, ipAddress, "to Cloudflare")
	api, err := cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"))
	if err != nil {
		return err
	}

	// Fetch the zone ID
	zoneId, err := api.ZoneIDByName(r.DnsDomain) // Assumes exists in CloudFlare already
	if err != nil {
		return err
	}

	params := &cloudflare.DNSRecord{
		Type:     "A",
		Name:     r.formatHostname(name),
		ZoneID:   zoneId,
		ZoneName: r.DnsDomain,
		Content:  ipAddress,
		Proxied:  r.Proxied,
	}

	dnsRec, err := api.DNSRecords(zoneId, *params)
	if err != nil {
		return err
	}
	if len(dnsRec) == 1 {
		log.Infoln("DNS Record already added")
		return nil
	}

	resp, err := api.CreateDNSRecord(zoneId, *params)
	if err != nil {
		return err
	}
	log.Debugln("CF response", resp)

	return nil
}

func (r *CFlare) RemoveIp(ipAddress string, dryRun bool) error {
	if dryRun {
		log.Infoln("Dry Run is True.  Would have updated DNS for address " + ipAddress)
	} else {
		for _, name := range r.Hostnames {
			err := r.deleteDNSName(name, ipAddress)
			if err != nil {
				return err
			}
		}
	}
	if notify.SlackNotify.UseSlack == true {
		notify.SlackNotify.SendToSlack(ipAddress, r.DnsDomain, "Removed", dryRun)
	}
	return nil
}

func (r *CFlare) deleteDNSName(name string, ipAddress string) error {
	// Construct a new API object
	log.Infoln("Deleting", name, ipAddress, "from Cloudflare")
	api, err := cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"))
	if err != nil {
		return err
	}

	// Fetch the zone ID
	zoneId, err := api.ZoneIDByName(r.DnsDomain) // Assumes exists in CloudFlare already
	if err != nil {
		return err
	}
	log.Debugln(r.Proxied, ipAddress, r.DnsDomain, zoneId, r.formatHostname(name))
	params := &cloudflare.DNSRecord{
		Type:     "A",
		Name:     r.formatHostname(name),
		ZoneID:   zoneId,
		ZoneName: r.DnsDomain,
		Content:  ipAddress,
		Proxied:  r.Proxied,
	}

	dnsRec, err := api.DNSRecords(zoneId, *params)
	if err != nil {
		return err
	}

	log.Infoln(dnsRec)

	if len(dnsRec) == 0 {
		log.Infoln("DNS Record already removed")
		return nil
	}

	err = api.DeleteDNSRecord(zoneId, dnsRec[0].ID)
	if err != nil {
		return err
	}
	return nil
}<commit_msg>move message closer to actual dns action<commit_after>package dns

import (
	"os"
	"strings"

	log "github.com/Sirupsen/logrus"
	"github.com/bparli/goavail/notify"
	"github.com/cloudflare/cloudflare-go"
)

type CFlare struct {
	DnsDomain string
	Proxied   bool
	Addresses []string
	Hostnames []string
}

func (r *CFlare) formatHostname(host string) string {
	if strings.Contains(host, r.DnsDomain) {
		return host
	}
	return host + "." + r.DnsDomain
}

func (r *CFlare) AddIp(ipAddress string, dryRun bool) error {
	if dryRun {
		log.Infoln("Dry Run is True.  Would have updated DNS for address " + ipAddress)
	} else {
		for _, name := range r.Hostnames {
			err := r.addDNSName(name, ipAddress, dryRun)
			if err != nil {
				return err
			}
		}
	}
	if notify.SlackNotify.UseSlack == true {
		notify.SlackNotify.SendToSlack(ipAddress, r.DnsDomain, "Added", dryRun)
	}
	return nil
}

func (r *CFlare) addDNSName(name string, ipAddress string, dryRun bool) error {
	// Construct a new API object
	log.Infoln("Adding", name, ipAddress, "to Cloudflare")
	api, err := cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"))
	if err != nil {
		return err
	}

	// Fetch the zone ID
	zoneId, err := api.ZoneIDByName(r.DnsDomain) // Assumes exists in CloudFlare already
	if err != nil {
		return err
	}

	params := &cloudflare.DNSRecord{
		Type:     "A",
		Name:     r.formatHostname(name),
		ZoneID:   zoneId,
		ZoneName: r.DnsDomain,
		Content:  ipAddress,
		Proxied:  r.Proxied,
	}

	dnsRec, err := api.DNSRecords(zoneId, *params)
	if err != nil {
		return err
	}
	if len(dnsRec) == 1 {
		log.Infoln("DNS Record already added")
		return nil
	}
	if dryRun {
		log.Infoln("Dry Run is True.  Would have updated DNS for address " + ipAddress)
	} else {
		resp, err := api.CreateDNSRecord(zoneId, *params)
		if err != nil {
			return err
		}
		log.Debugln("CF response", resp)
	}
	if notify.SlackNotify.UseSlack == true {
		notify.SlackNotify.SendToSlack(ipAddress, r.DnsDomain, "Removed", dryRun)
	}
	return nil
}

func (r *CFlare) RemoveIp(ipAddress string, dryRun bool) error {

	for _, name := range r.Hostnames {
		err := r.deleteDNSName(name, ipAddress, dryRun)
		if err != nil {
			return err
		}
	}
	return nil
}

func (r *CFlare) deleteDNSName(name string, ipAddress string, dryRun bool) error {
	// Construct a new API object
	log.Infoln("Deleting", name, ipAddress, "from Cloudflare")
	api, err := cloudflare.New(os.Getenv("CF_API_KEY"), os.Getenv("CF_API_EMAIL"))
	if err != nil {
		return err
	}

	// Fetch the zone ID
	zoneId, err := api.ZoneIDByName(r.DnsDomain) // Assumes exists in CloudFlare already
	if err != nil {
		return err
	}
	log.Debugln(r.Proxied, ipAddress, r.DnsDomain, zoneId, r.formatHostname(name))
	params := &cloudflare.DNSRecord{
		Type:     "A",
		Name:     r.formatHostname(name),
		ZoneID:   zoneId,
		ZoneName: r.DnsDomain,
		Content:  ipAddress,
		Proxied:  r.Proxied,
	}

	dnsRec, err := api.DNSRecords(zoneId, *params)
	if err != nil {
		return err
	}

	log.Infoln(dnsRec)

	if len(dnsRec) == 0 {
		log.Infoln("DNS Record already removed")
		return nil
	}
	if dryRun {
		log.Infoln("Dry Run is True.  Would have updated DNS for address " + ipAddress)
	} else {
		err = api.DeleteDNSRecord(zoneId, dnsRec[0].ID)
		if err != nil {
			return err
		}
	}
	if notify.SlackNotify.UseSlack == true {
		notify.SlackNotify.SendToSlack(ipAddress, r.DnsDomain, "Removed", dryRun)
	}
	return nil
}
{'fraction_non_alphanumeric': 0.12886991001982614, 'fraction_numerical': 0.0009150526155253927, 'mean_word_length': 3.5828092243186584, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '587007', 'n_tokens_mistral': 2693, 'n_tokens_neox': 2539, 'n_words': 637}