<commit_before>package main

import "context"
import "flag"
import "strings"

import "github.com/digitalocean/godo"
import "golang.org/x/oauth2"

var doClient *godo.Client
var config Config

var logger Logger

func areRecordsEqual(record1 godo.DomainRecord, record2 godo.DomainRecord) bool {
	return record1.Type == record2.Type &&
		record1.Name == record2.Name
}

func findRecord(record godo.DomainRecord) (*godo.DomainRecord, error) {
	ctx := context.TODO()
	opt := &godo.ListOptions{}

	for {
		currentRecords, resp, err := doClient.Domains.Records(ctx, config.TLD(), opt)

		if err != nil {
			return nil, err
		}

		for _, aRecord := range currentRecords {
			if areRecordsEqual(record, aRecord) {
				return &aRecord, nil
			}
		}

		if resp.Links == nil || resp.Links.IsLastPage() {
			break
		}

		page, err := resp.Links.CurrentPage()
		if err != nil {
			return nil, err
		}

		// set the page we want for the next request
		opt.Page = page + 1
	}

	return nil, nil
}

func createRecord(record godo.DomainRecord) (*godo.DomainRecord, error) {
	ctx := context.TODO()
	newRecord, _, err := doClient.Domains.CreateRecord(ctx, config.TLD(), &godo.DomainRecordEditRequest{
		Type: record.Type,
		Name: record.Name,
		Data: record.Data,
	})

	if err != nil {
		return nil, err
	}

	return newRecord, nil
}

func updateRecord(oldRecord *godo.DomainRecord, record godo.DomainRecord) (*godo.DomainRecord, error) {
	ctx := context.TODO()
	logger.Log("Update Record", oldRecord.ID)
	newRecord, _, err := doClient.Domains.EditRecord(ctx, config.TLD(), oldRecord.ID, &godo.DomainRecordEditRequest{
		Type: record.Type,
		Name: record.Name,
		Data: record.Data,
	})

	if err != nil {
		return nil, err
	}

	return newRecord, nil
}

func deleteRecord(oldRecord *godo.DomainRecord) error {
	ctx := context.TODO()
	_, err := doClient.Domains.DeleteRecord(ctx, config.TLD(), oldRecord.ID)

	return err
}

func create(recordData godo.DomainRecord) {
	record, err := createRecord(recordData)

	if err != nil {
		logger.Fatalf("Record create error %s", err)
	}

	logger.Log("Record created", record)
}

func update(record *godo.DomainRecord, recordData godo.DomainRecord) {
	record, err := updateRecord(record, recordData)

	if err != nil {
		logger.Fatalf("Record create error %s", err)
	}

	logger.Log("Record updated", record)
}

func remove(record *godo.DomainRecord) {
	err := deleteRecord(record)

	if err != nil {
		logger.Fatalf("Record delete failed %s", err)
	}

	logger.Log("Record deleted", record)
}

func init() {
	logger = Logger{}
	config = Config{}

	flag.StringVar(&config.Domain, "domain", "", "domain of the record")
	flag.StringVar(&config.Token, "token", "", "digitalocean token, see https://cloud.digitalocean.com/settings/api/tokens")

	flag.StringVar(&config.RecordType, "recordType", "", "Type of the DNS record, like A, TXT, MX, etc.")
	flag.StringVar(&config.RecordName, "recordName", "", "Name of the record, like @, www, email, etc.")
	flag.StringVar(&config.RecordData, "recordData", "", "Value of the record")

	flag.BoolVar(&config.Delete, "delete", false, "Delete the record")
	flag.BoolVar(&config.Create, "create", false, "Create the record if not exists")
	flag.BoolVar(&config.Update, "update", false, "Update the record if exists")
<commit_msg>Print formatted finished action details<commit_after>package main

import "context"
import "flag"
import "strings"
import "fmt"

import "github.com/digitalocean/godo"
import "golang.org/x/oauth2"

var doClient *godo.Client
var config Config

var logger Logger

func areRecordsEqual(record1 godo.DomainRecord, record2 godo.DomainRecord) bool {
	return record1.Type == record2.Type &&
		record1.Name == record2.Name
}

func findRecord(record godo.DomainRecord) (*godo.DomainRecord, error) {
	ctx := context.TODO()
	opt := &godo.ListOptions{}

	for {
		currentRecords, resp, err := doClient.Domains.Records(ctx, config.TLD(), opt)

		if err != nil {
			return nil, err
		}

		for _, aRecord := range currentRecords {
			if areRecordsEqual(record, aRecord) {
				return &aRecord, nil
			}
		}

		if resp.Links == nil || resp.Links.IsLastPage() {
			break
		}

		page, err := resp.Links.CurrentPage()
		if err != nil {
			return nil, err
		}

		// set the page we want for the next request
		opt.Page = page + 1
	}

	return nil, nil
}

func createRecord(record godo.DomainRecord) (*godo.DomainRecord, error) {
	ctx := context.TODO()
	newRecord, _, err := doClient.Domains.CreateRecord(ctx, config.TLD(), &godo.DomainRecordEditRequest{
		Type: record.Type,
		Name: record.Name,
		Data: record.Data,
	})

	if err != nil {
		return nil, err
	}

	return newRecord, nil
}

func updateRecord(oldRecord *godo.DomainRecord, record godo.DomainRecord) (*godo.DomainRecord, error) {
	ctx := context.TODO()
	newRecord, _, err := doClient.Domains.EditRecord(ctx, config.TLD(), oldRecord.ID, &godo.DomainRecordEditRequest{
		Type: record.Type,
		Name: record.Name,
		Data: record.Data,
	})

	if err != nil {
		return nil, err
	}

	return newRecord, nil
}

func deleteRecord(oldRecord *godo.DomainRecord) error {
	ctx := context.TODO()
	_, err := doClient.Domains.DeleteRecord(ctx, config.TLD(), oldRecord.ID)

	return err
}

func printAction(action string, record godo.DomainRecord) {
	fmt.Printf("%s %s %s %s %s\n", action, config.TLD(), record.Name, record.Type, record.Data)
}

func create(recordData godo.DomainRecord) {
	_, err := createRecord(recordData)

	if err != nil {
		logger.Fatalf("Record create error %s", err)
	}

	printAction("create", recordData)
}

func update(record *godo.DomainRecord, recordData godo.DomainRecord) {
	_, err := updateRecord(record, recordData)

	if err != nil {
		logger.Fatalf("Record create error %s", err)
	}

	printAction("update", recordData)
}

func remove(record *godo.DomainRecord) {
	err := deleteRecord(record)

	if err != nil {
		logger.Fatalf("Record delete failed %s", err)
	}

	printAction("delete", *record)
}

func init() {
	logger = Logger{}
	config = Config{}

	flag.StringVar(&config.Domain, "domain", "", "domain of the record")
	flag.StringVar(&config.Token, "token", "", "digitalocean token, see https://cloud.digitalocean.com/settings/api/tokens")

	flag.StringVar(&config.RecordType, "recordType", "", "Type of the DNS record, like A, TXT, MX, etc.")
	flag.StringVar(&config.RecordName, "recordName", "", "Name of the record, like @, www, email, etc.")
	flag.StringVar(&config.RecordData, "recordData", "", "Value of the record")

	flag.BoolVar(&config.Delete, "delete", false, "Delete the record")
	flag.BoolVar(&config.Create, "create", false, "Create the record if not exists")
	flag.BoolVar(&config.Update, "update", false, "Update the record if exists")

{'fraction_non_alphanumeric': 0.14394847213900538, 'fraction_numerical': 0.002396644697423607, 'mean_word_length': 4.4064777327935225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '5137653', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2227, 'n_words': 587}