<commit_before>package cmd

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

	"github.com/Sirupsen/logrus"
	"github.com/codeclimate/test-reporter/formatters"
	"github.com/codeclimate/test-reporter/formatters/clover"
	"github.com/codeclimate/test-reporter/formatters/coveragepy"
	"github.com/codeclimate/test-reporter/formatters/gocov"
	"github.com/codeclimate/test-reporter/formatters/lcov"
	"github.com/codeclimate/test-reporter/formatters/simplecov"
	"github.com/gobuffalo/envy"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

type CoverageFormatter struct {
	CoveragePath string
	In           formatters.Formatter
	InputType    string
	Output       string
	Prefix       string
	writer       io.Writer
}

var formatOptions = CoverageFormatter{}

// a prioritized list of the formatters to use
var formatterList = []string{"simplecov", "lcov", "coverage.py", "clover", "gocov"}

// a map of the formatters to use
var formatterMap = map[string]formatters.Formatter{
	"simplecov":   &simplecov.Formatter{},
	"lcov":        &lcov.Formatter{},
	"coverage.py": &coveragepy.Formatter{},
	"gocov":       &gocov.Formatter{},
	"clover":      &clover.Formatter{},
}

// formatCoverageCmd represents the format command
var formatCoverageCmd = &cobra.Command{
	Use:   "format-coverage",
	Short: "Locate, parse, and re-format supported coverage sources.",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) != 0 {
			logrus.Debugf("coverage path %s", args[0])
			formatOptions.CoveragePath = args[0]
		}
		return runFormatter(formatOptions)
	},
}

func runFormatter(formatOptions CoverageFormatter) error {
	envy.Set("PREFIX", formatOptions.Prefix)

	// if a type is specified use that
	if formatOptions.InputType != "" {
		if f, ok := formatterMap[formatOptions.InputType]; ok {
			logrus.Debugf("using formatter %s", formatOptions.InputType)
			if formatOptions.CoveragePath != "" {
				_, err := f.Search(formatOptions.CoveragePath)
				if err != nil {
					logrus.Errorf("could not find coverage file %s\n%s", formatOptions.CoveragePath, err)
					return errors.WithStack(err)
				}
			}
			formatOptions.In = f
		} else {
			return errors.WithStack(errors.Errorf("could not find a formatter of type %s", formatOptions.InputType))
		}
	} else {
		logrus.Debug("searching for a formatter to use")
		// else start searching for files:
		for n, f := range formatterMap {
			logrus.Debugf("checking %s formatter", n)<commit_msg>Raise an error when invalid format-coverage path usage (#143)<commit_after>package cmd

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

	"github.com/Sirupsen/logrus"
	"github.com/codeclimate/test-reporter/formatters"
	"github.com/codeclimate/test-reporter/formatters/clover"
	"github.com/codeclimate/test-reporter/formatters/coveragepy"
	"github.com/codeclimate/test-reporter/formatters/gocov"
	"github.com/codeclimate/test-reporter/formatters/lcov"
	"github.com/codeclimate/test-reporter/formatters/simplecov"
	"github.com/gobuffalo/envy"
	"github.com/pkg/errors"
	"github.com/spf13/cobra"
)

type CoverageFormatter struct {
	CoveragePath string
	In           formatters.Formatter
	InputType    string
	Output       string
	Prefix       string
	writer       io.Writer
}

var formatOptions = CoverageFormatter{}

// a prioritized list of the formatters to use
var formatterList = []string{"simplecov", "lcov", "coverage.py", "clover", "gocov"}

// a map of the formatters to use
var formatterMap = map[string]formatters.Formatter{
	"simplecov":   &simplecov.Formatter{},
	"lcov":        &lcov.Formatter{},
	"coverage.py": &coveragepy.Formatter{},
	"gocov":       &gocov.Formatter{},
	"clover":      &clover.Formatter{},
}

// formatCoverageCmd represents the format command
var formatCoverageCmd = &cobra.Command{
	Use:   "format-coverage",
	Short: "Locate, parse, and re-format supported coverage sources.",
	RunE: func(cmd *cobra.Command, args []string) error {
		if len(args) != 0 {
			logrus.Debugf("coverage path %s", args[0])
			formatOptions.CoveragePath = args[0]
			if formatOptions.InputType == "" {
				return errors.WithStack(errors.Errorf("please specify the format of the coverage file \"%s\" using the --input-type flag", formatOptions.CoveragePath))
			}
		}
		return runFormatter(formatOptions)
	},
}

func runFormatter(formatOptions CoverageFormatter) error {
	envy.Set("PREFIX", formatOptions.Prefix)

	// if a type is specified use that
	if formatOptions.InputType != "" {
		if f, ok := formatterMap[formatOptions.InputType]; ok {
			logrus.Debugf("using formatter %s", formatOptions.InputType)
			if formatOptions.CoveragePath != "" {
				_, err := f.Search(formatOptions.CoveragePath)
				if err != nil {
					logrus.Errorf("could not find coverage file %s\n%s", formatOptions.CoveragePath, err)
					return errors.WithStack(err)
				}
			}
			formatOptions.In = f
		} else {
			return errors.WithStack(errors.Errorf("could not find a formatter of type %s", formatOptions.InputType))
		}
	} else {
		logrus.Debug("searching for a formatter to use")
		// else start searching for files:
		for n, f := range formatterMap {
			logrus.Debugf("checking %s formatter", n)
{'fraction_non_alphanumeric': 0.12847424684159378, 'fraction_numerical': 0.0025267249757045676, 'mean_word_length': 4.11530815109344, 'pattern_counts': {'":': 10, '<': 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': '6163062', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1727, 'n_words': 386}