<commit_before>package check

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/pivotal-cf-experimental/go-pivnet"
	"github.com/pivotal-cf-experimental/pivnet-resource/concourse"
	"github.com/pivotal-cf-experimental/pivnet-resource/versions"
)

//go:generate counterfeiter --fake-name FakeFilter . filter
type filter interface {
	ReleasesByReleaseType(releases []pivnet.Release, releaseType string) ([]pivnet.Release, error)
	ReleasesByVersion(releases []pivnet.Release, version string) ([]pivnet.Release, error)
}

//go:generate counterfeiter --fake-name FakeSorter . sorter
type sorter interface {
	SortBySemver([]pivnet.Release) ([]pivnet.Release, error)
}

//go:generate counterfeiter --fake-name FakePivnetClient . pivnetClient
type pivnetClient interface {
	ReleaseTypes() ([]string, error)
	ReleasesForProductSlug(string) ([]pivnet.Release, error)
	ProductVersions(productSlug string, releases []pivnet.Release) ([]string, error)
	ReleaseETag(productSlug string, releaseID int) (string, error)
}

type CheckCommand struct {
	logger        *log.Logger
	binaryVersion string
	filter        filter
	pivnetClient  pivnetClient
	semverSorter  sorter
	logFilePath   string
}

func NewCheckCommand(
	logger *log.Logger,
	binaryVersion string,
	filter filter,
	pivnetClient pivnetClient,
	semverSorter sorter,
	logFilePath string,
) *CheckCommand {
	return &CheckCommand{
		logger:        logger,
		binaryVersion: binaryVersion,
		filter:        filter,
		pivnetClient:  pivnetClient,
		semverSorter:  semverSorter,
		logFilePath:   logFilePath,
	}
}

func (c *CheckCommand) Run(input concourse.CheckRequest) (concourse.CheckResponse, error) {
	c.logger.Println("Received input, starting Check CMD run")

	logDir := filepath.Dir(c.logFilePath)
	existingLogFiles, err := filepath.Glob(filepath.Join(logDir, "pivnet-resource-check.log*"))
	if err != nil {
		// This is untested because the only error returned by filepath.Glob is a
		// malformed glob, and this glob is hard-coded to be correct.
		return nil, err
	}

	for _, f := range existingLogFiles {
		if filepath.Base(f) != filepath.Base(c.logFilePath) {
			c.logger.Printf("Removing existing log file: %s\n", f)
			err := os.Remove(f)
			if err != nil {
				// This is untested because it is too hard to force os.Remove to return
				// an error.
				return nil, err
			}
		}
	}
<commit_msg>Check removes existing log files under new name.<commit_after>package check

import (
	"fmt"
	"log"
	"os"
	"path/filepath"
	"strings"

	"github.com/pivotal-cf-experimental/go-pivnet"
	"github.com/pivotal-cf-experimental/pivnet-resource/concourse"
	"github.com/pivotal-cf-experimental/pivnet-resource/versions"
)

//go:generate counterfeiter --fake-name FakeFilter . filter
type filter interface {
	ReleasesByReleaseType(releases []pivnet.Release, releaseType string) ([]pivnet.Release, error)
	ReleasesByVersion(releases []pivnet.Release, version string) ([]pivnet.Release, error)
}

//go:generate counterfeiter --fake-name FakeSorter . sorter
type sorter interface {
	SortBySemver([]pivnet.Release) ([]pivnet.Release, error)
}

//go:generate counterfeiter --fake-name FakePivnetClient . pivnetClient
type pivnetClient interface {
	ReleaseTypes() ([]string, error)
	ReleasesForProductSlug(string) ([]pivnet.Release, error)
	ProductVersions(productSlug string, releases []pivnet.Release) ([]string, error)
	ReleaseETag(productSlug string, releaseID int) (string, error)
}

type CheckCommand struct {
	logger        *log.Logger
	binaryVersion string
	filter        filter
	pivnetClient  pivnetClient
	semverSorter  sorter
	logFilePath   string
}

func NewCheckCommand(
	logger *log.Logger,
	binaryVersion string,
	filter filter,
	pivnetClient pivnetClient,
	semverSorter sorter,
	logFilePath string,
) *CheckCommand {
	return &CheckCommand{
		logger:        logger,
		binaryVersion: binaryVersion,
		filter:        filter,
		pivnetClient:  pivnetClient,
		semverSorter:  semverSorter,
		logFilePath:   logFilePath,
	}
}

func (c *CheckCommand) Run(input concourse.CheckRequest) (concourse.CheckResponse, error) {
	c.logger.Println("Received input, starting Check CMD run")

	logDir := filepath.Dir(c.logFilePath)
	existingLogFiles, err := filepath.Glob(filepath.Join(logDir, "*.log*"))
	if err != nil {
		// This is untested because the only error returned by filepath.Glob is a
		// malformed glob, and this glob is hard-coded to be correct.
		return nil, err
	}

	c.logger.Printf("Located logfiles: %v\n", existingLogFiles)

	for _, f := range existingLogFiles {
		if filepath.Base(f) != filepath.Base(c.logFilePath) {
			c.logger.Printf("Removing existing log file: %s\n", f)
			err := os.Remove(f)
			if err != nil {
				// This is untested because it is too hard to force os.Remove to return
				// an error.
				return nil, err
			}
		}
	}

{'fraction_non_alphanumeric': 0.11141134899189357, 'fraction_numerical': 0.0, 'mean_word_length': 4.358574610244989, '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': '1421458', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1613, 'n_words': 401}