<commit_before>package main

import (
	"fmt"
	"go/token"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"honnef.co/go/tools/lint/lintutil"
	"honnef.co/go/tools/staticcheck"

	"github.com/codeclimate/cc-engine-go/engine"
	"github.com/kisielk/gotool"
)

func main() {
	config, err := engine.LoadConfig()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading config: %s\n", err)
		os.Exit(1)
	}

	rootPath := "/code"

	pkgPath := filepath.Join(os.Getenv("GOPATH"), "src", "app")
	if engineConfig, ok := config["config"].(map[string]interface{}); ok {
		if packageDir, ok := engineConfig["package"].(string); ok {
			pkgPath = filepath.Join(os.Getenv("GOPATH"), "src", packageDir)
		}
	}

	err = copyDir(pkgPath, rootPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed copying dir to %q. %s\n", pkgPath, err)
		os.Exit(1)
	}

	os.Chdir(pkgPath)

	pkgs := gotool.ImportPaths([]string{"./..."})

	checker := staticcheck.NewChecker()
	problems, program, err := lintutil.Lint(checker, pkgs, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error linting program: %s\n", err)
		os.Exit(1)
	}

	includePaths := engine.IncludePaths(pkgPath, config)

	for _, problem := range problems {

		position := program.Fset.Position(problem.Position)

		if inSlice(position.Filename, includePaths) {
			issue := &engine.Issue{
				Type:              "issue",
				Check:             check(problem.Text),
				Description:       description(problem.Text),
				RemediationPoints: 5000,
				Location:          location(position, pkgPath),
				Categories:        []string{"Style"},
			}

			engine.PrintIssue(issue)
		}
	}
}

func check(s string) string {
	return "Go-StaticCheck/" + strings.TrimRight(strings.Split(s, "(")[1], ")")
}

func description(s string) string {
	return strings.Split(s, "(")[0]
}

func location(pos token.Position, path string) *engine.Location {
	return &engine.Location{
		Path: strings.SplitAfter(pos.Filename, path+"/")[1],
		Lines: &engine.LinesOnlyPosition{
			Begin: pos.Line,
			End:   pos.Line,
		},
	}
}

func copyDir(dst, src string) error {
	walkFn := func(srcPath string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		relPath, err := filepath.Rel(src, srcPath)<commit_msg>refactor pkgPath into a function<commit_after>package main

import (
	"fmt"
	"go/token"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"honnef.co/go/tools/lint/lintutil"
	"honnef.co/go/tools/staticcheck"

	"github.com/codeclimate/cc-engine-go/engine"
	"github.com/kisielk/gotool"
)

func main() {
	config, err := engine.LoadConfig()
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error loading config: %s\n", err)
		os.Exit(1)
	}

	rootPath := "/code"
	pkgPath := getPkgPath(config)

	err = copyDir(pkgPath, rootPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Failed copying dir to %q. %s\n", pkgPath, err)
		os.Exit(1)
	}

	os.Chdir(pkgPath)

	pkgs := gotool.ImportPaths([]string{"./..."})

	checker := staticcheck.NewChecker()
	problems, program, err := lintutil.Lint(checker, pkgs, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error linting program: %s\n", err)
		os.Exit(1)
	}

	includePaths := engine.IncludePaths(pkgPath, config)

	for _, problem := range problems {

		position := program.Fset.Position(problem.Position)

		if inSlice(position.Filename, includePaths) {
			issue := &engine.Issue{
				Type:              "issue",
				Check:             check(problem.Text),
				Description:       description(problem.Text),
				RemediationPoints: 5000,
				Location:          location(position, pkgPath),
				Categories:        []string{"Style"},
			}

			engine.PrintIssue(issue)
		}
	}
}

func getPkgPath(config map[string]interface{}) string {
	pkgPath := filepath.Join(os.Getenv("GOPATH"), "src", "app")
	if engineConfig, ok := config["config"].(map[string]interface{}); ok {
		if pkg, ok := engineConfig["package"].(string); ok {
			pkgPath = filepath.Join(os.Getenv("GOPATH"), "src", pkg)
		}
	}
	return pkgPath
}

func check(s string) string {
	return "Go-StaticCheck/" + strings.TrimRight(strings.Split(s, "(")[1], ")")
}

func description(s string) string {
	return strings.Split(s, "(")[0]
}

func location(pos token.Position, path string) *engine.Location {
	return &engine.Location{
		Path: strings.SplitAfter(pos.Filename, path+"/")[1],
		Lines: &engine.LinesOnlyPosition{
			Begin: pos.Line,
			End:   pos.Line,
		},
	}
}

func copyDir(dst, src string) error {
	walkFn := func(srcPath string, info os.FileInfo, err error) error {
		if err != nil {
			return err
		}
		relPath, err := filepath.Rel(src, srcPath)
{'fraction_non_alphanumeric': 0.16688567674113008, 'fraction_numerical': 0.004380201489268506, 'mean_word_length': 3.6793032786885247, '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': '1430943', 'n_tokens_mistral': 1830, 'n_tokens_neox': 1671, 'n_words': 347}