diff --git a/bin/run-check b/bin/run-check new file mode 100755 index 0000000..3832347 --- /dev/null +++ b/bin/run-check @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +go run main.go check editorconfig/tests/ diff --git a/editorconfig/check_command.go b/editorconfig/check_command.go new file mode 100644 index 0000000..77e3222 --- /dev/null +++ b/editorconfig/check_command.go @@ -0,0 +1,51 @@ +package editorconfig + +import ( + "bufio" + "fmt" + "github.com/codegangsta/cli" + "os" + "strconv" +) + +func CheckCommand(c *cli.Context) error { + files, err := FindSourceFiles(c.Args()) + if err != nil { + return err + } + + configs := FindConfigFiles(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) + + lineNo := 0 + for scanner.Scan() { + line := scanner.Text() + lineNo++ + + for ruleName, ruleValue := range rules { + if lineCheckers[ruleName] == nil { + continue + } + + result := lineCheckers[ruleName](ruleValue, line) + if !result.isOk { + fmt.Println(f + ": line " + strconv.Itoa(lineNo) + ": " + ruleName + ": " + result.messageIfNotOk) + // Don't show more than 1 error per line. + break + } + } + } + } + + return nil +} diff --git a/editorconfig/cli.go b/editorconfig/cli.go index 711e7cd..ac7ab43 100644 --- a/editorconfig/cli.go +++ b/editorconfig/cli.go @@ -22,6 +22,11 @@ func CreateCliApp() *cli.App { Usage: "List rules that match a given file", Action: RulesCommand, }, + { + Name: "check", + Usage: "Validate files", + Action: CheckCommand, + }, } return app