Run full-file checkers in "check" command.

This commit is contained in:
Amy Boyd
2016-06-25 12:37:56 +01:00
parent 1e5b668197
commit 583c4bd49b

View File

@@ -1,11 +1,11 @@
package editorconfig package editorconfig
import ( import (
"bufio"
"fmt" "fmt"
"github.com/codegangsta/cli" "github.com/codegangsta/cli"
"os" "io/ioutil"
"strconv" "strconv"
"strings"
) )
func CheckCommand(c *cli.Context) error { func CheckCommand(c *cli.Context) error {
@@ -14,30 +14,41 @@ func CheckCommand(c *cli.Context) error {
return err return err
} }
if len(files) == 0 {
ExitBecauseOfInternalError("No files to check in " + strings.Join(c.Args(), ", "))
}
configs := FindConfigFiles(files) configs := FindConfigFiles(files)
for _, f := range files { for _, f := range files {
fh, err := os.Open(f)
if err != nil {
ExitBecauseOfInternalError("Could not read file: " + f)
}
// @todo - add "full file checkers" for end_of_line, insert_final_newline and charset.
scanner := bufio.NewScanner(fh)
rules := GetRulesToApplyToSourcePath(f, configs) rules := GetRulesToApplyToSourcePath(f, configs)
if len(rules) == 0 {
lineNo := 0
for scanner.Scan() {
line := scanner.Text()
lineNo++
for ruleName, ruleValue := range rules {
if lineCheckers[ruleName] == nil {
continue continue
} }
result := lineCheckers[ruleName](ruleValue, line) fileContentInBytes, err := ioutil.ReadFile(f)
if err != nil {
ExitBecauseOfInternalError("Could not read file: " + f)
}
fileContent := string(fileContentInBytes)
// Run full-file checkers.
for ruleName, ruleValue := range rules {
if fullFileChecker, ok := fullFileCheckers[ruleName]; ok {
result := fullFileChecker(ruleValue, fileContent)
if !result.isOk {
fmt.Println(f + ": " + ruleName + ": " + result.messageIfNotOk)
}
}
}
// Run line checkers.
lines := SplitIntoLines(fileContent)
lineNo := 1
for _, line := range lines {
for ruleName, ruleValue := range rules {
if lineChecker, ok := lineCheckers[ruleName]; ok {
result := lineChecker(ruleValue, line)
if !result.isOk { if !result.isOk {
fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk) fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk)
// Don't show more than 1 error per line. // Don't show more than 1 error per line.
@@ -45,6 +56,8 @@ func CheckCommand(c *cli.Context) error {
} }
} }
} }
lineNo++
}
} }
return nil return nil