<commit_before>package acmewrapper

import (
	"crypto/tls"
	"crypto/x509"
	"time"

	"github.com/xenolf/lego/acme"
)

// writeCert takes an acme CertificateResource (as returned from the acme.RenewCertificate
// and the acme.ObtainCertificate functions), and writes the cert and key files from it.
// If the files already exist, it renames the old versions by adding .bak to them. This makes
// sure that a little accident doesn't cause too much damage.
func (w *AcmeWrapper) writeCert(certfile, keyfile string, crt acme.CertificateResource) (err error) {
	if err := w.backupAndSaveFile(certfile, crt.Certificate); err != nil {
		return err
	}
	if err := w.backupAndSaveFile(keyfile, crt.PrivateKey); err != nil {
		return err
	}
	return nil
}

func tlsCert(crt acme.CertificateResource) (*tls.Certificate, error) {
	cert, err := tls.X509KeyPair(crt.Certificate, crt.PrivateKey)
	return &cert, err
}

// CertNeedsUpdate returns whether the current certificate either
// does not exist, or is <X days from expiration, where X is set up in config
func (w *AcmeWrapper) CertNeedsUpdate() bool {
	if w.cert == nil {
		// The cert doesn't exist - it certainly needs update
		return true
	}

	// w.cert.Leaf is not set, so we have to manually parse the certs
	// and make sure that they don't expire soon
	for _, c := range w.cert.Certificate {
		crt, err := x509.ParseCertificate(c)
		if err != nil {
			// If there's an error, we assume the cert is broken, and needs update
			return true
		}
		timeLeft := crt.NotAfter.Sub(time.Now().UTC())
		if timeLeft < w.Config.RenewTime {
			return true
		}
	}

	return false
}
<commit_msg>Only create backups when SaveFileCallback is not provided<commit_after>package acmewrapper

import (
	"crypto/tls"
	"crypto/x509"
	"time"

	"github.com/xenolf/lego/acme"
)

// writeCert takes an acme CertificateResource (as returned from the acme.RenewCertificate
// and the acme.ObtainCertificate functions), and writes the cert and key files from it.
// If the files already exist, it renames the old versions by adding .bak to them. This makes
// sure that a little accident doesn't cause too much damage.
func (w *AcmeWrapper) writeCert(certfile, keyfile string, crt acme.CertificateResource) (err error) {
	//only create a backup version if we're definitely saving to disk
	if w.Config.SaveFileCallback == nil {
		if err := w.backupAndSaveFile(certfile, crt.Certificate); err != nil {
			return err
		}
		if err := w.backupAndSaveFile(keyfile, crt.PrivateKey); err != nil {
			return err
		}
	} else {
		if err := w.saveFile(certfile, crt.Certificate); err != nil {
			return err
		}
		if err := w.saveFile(keyfile, crt.PrivateKey); err != nil {
			return err
		}
	}
	return nil
}

func tlsCert(crt acme.CertificateResource) (*tls.Certificate, error) {
	cert, err := tls.X509KeyPair(crt.Certificate, crt.PrivateKey)
	return &cert, err
}

// CertNeedsUpdate returns whether the current certificate either
// does not exist, or is <X days from expiration, where X is set up in config
func (w *AcmeWrapper) CertNeedsUpdate() bool {
	if w.cert == nil {
		// The cert doesn't exist - it certainly needs update
		return true
	}

	// w.cert.Leaf is not set, so we have to manually parse the certs
	// and make sure that they don't expire soon
	for _, c := range w.cert.Certificate {
		crt, err := x509.ParseCertificate(c)
		if err != nil {
			// If there's an error, we assume the cert is broken, and needs update
			return true
		}
		timeLeft := crt.NotAfter.Sub(time.Now().UTC())
		if timeLeft < w.Config.RenewTime {
			return true
		}
	}

	return false
}

{'fraction_non_alphanumeric': 0.10192683607930746, 'fraction_numerical': 0.00502652890254119, 'mean_word_length': 3.639896373056995, 'pattern_counts': {'":': 0, '<': 7, '<?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': '3530545', 'n_tokens_mistral': 1229, 'n_tokens_neox': 1147, 'n_words': 422}