From 0fd96b0d487aa469b9dd1bbd2dd489806f5244ec Mon Sep 17 00:00:00 2001 From: Amy Boyd Date: Fri, 24 Jun 2016 21:31:19 +0100 Subject: [PATCH] Add 'check' command with support for indent style/size, trailing whitespace. --- bin/run-check | 2 ++ editorconfig/check_command.go | 51 +++++++++++++++++++++++++++++++++++ editorconfig/cli.go | 5 ++++ 3 files changed, 58 insertions(+) create mode 100755 bin/run-check create mode 100644 editorconfig/check_command.go 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