diff --git a/.buildkite/publish.sh b/.buildkite/publish.sh index d11bc23..3fdd635 100644 --- a/.buildkite/publish.sh +++ b/.buildkite/publish.sh @@ -1,6 +1,6 @@ #!/bin/bash -VERSION=$(cat VERSION) +VERSION=$(go run committer.go --version) UPLOADED_PKG_FOUND=$(aws s3 --region 'us-west-2' ls 's3://vpc-access/' | grep committer-$VERSION) if [ "$UPLOADED_PKG_FOUND" ]; then diff --git a/VERSION b/VERSION deleted file mode 100644 index d917d3e..0000000 --- a/VERSION +++ /dev/null @@ -1 +0,0 @@ -0.1.2 diff --git a/committer.go b/committer.go index 4a2dced..fa945d5 100644 --- a/committer.go +++ b/committer.go @@ -4,18 +4,15 @@ import ( "flag" "fmt" "github.com/gusto/committer/core" - "io/ioutil" "os" ) -func main() { - content, _ := ioutil.ReadFile("VERSION") - VERSION := string(content) +const VERSION = "0.1.3" +func main() { version := flag.Bool("version", false, "Display version") help := flag.Bool("help", false, "Display usage") fix := flag.Bool("fix", false, "Run autocorrect for commands that support it") - changed := flag.Bool("changed", false, "Run autocorrect for commands that support it") configPath := flag.String("config", "committer.yml", "Location of your config file") flag.Parse() @@ -27,16 +24,16 @@ func main() { } if *version { - fmt.Printf(VERSION) + fmt.Printf(VERSION + "\n") return } parsedConfig, err := core.NewConfigFromFile(*configPath) if err != nil { - return + panic(err) } - success := core.NewRunner(*parsedConfig, *fix, *changed).Run() + success := core.NewRunner(*parsedConfig, *fix).Run() if success { os.Exit(0) diff --git a/configure.sh b/configure.sh index f98c57d..c16d600 100755 --- a/configure.sh +++ b/configure.sh @@ -1,6 +1,6 @@ #!/bin/bash -ex -VERSION="0.1.2" +VERSION="0.1.3" GIT_PRE_COMMIT_HOOK=".git/hooks/pre-commit" COMMITTER_YML="committer.yml" COMMITTER_LOCATION="/usr/local/bin/committer" diff --git a/core/runner.go b/core/runner.go index 17ed350..e45fe7b 100644 --- a/core/runner.go +++ b/core/runner.go @@ -7,11 +7,10 @@ type Runner struct { resultChannel chan TaskResult } -func NewRunner(config Config, fix bool, changed bool) *Runner { +func NewRunner(config Config, fix bool) *Runner { return &Runner{ config: config, fix: fix, - changed: changed, resultChannel: make(chan TaskResult), } } @@ -21,7 +20,7 @@ func (this Runner) Run() bool { for i := 0; i < len(this.config.Tasks); i += 1 { task := this.config.Tasks[i] - if task.shouldRun(this.changed) { + if task.shouldRun() { tasksToRun = append(tasksToRun, task) go this.processTask(task) } @@ -36,5 +35,5 @@ func (this Runner) Run() bool { } func (this Runner) processTask(task Task) { - this.resultChannel <- task.Execute(this.changed, this.fix) + this.resultChannel <- task.Execute(this.fix) } diff --git a/core/task.go b/core/task.go index 1ea5a14..b692ff5 100644 --- a/core/task.go +++ b/core/task.go @@ -46,9 +46,9 @@ var execCommand = func(command string, args ...string) ([]byte, error) { return exec.Command(command, args...).CombinedOutput() } -func (task Task) Execute(changed bool, fix bool) TaskResult { +func (task Task) Execute(fix bool) TaskResult { // Generate command based on --fix / --changed - command := task.prepareCommand(changed, fix) + command := task.prepareCommand(fix) // Run command output, err := execCommand(command[0], command[1:]...) @@ -97,7 +97,7 @@ func (task Task) prepareFixedOutput(outputStr string) string { return strings.Join(fixedOutputList, "\n") } -func (task Task) prepareCommand(changed bool, fix bool) []string { +func (task Task) prepareCommand(fix bool) []string { // Use the FixCommand or regular Command depending on the flag passed to CLI var cmdStr string if fix && task.Fix.Command != "" { @@ -107,11 +107,8 @@ func (task Task) prepareCommand(changed bool, fix bool) []string { } // Feed in changed files if we are running with --changed - - if changed { - relevantChangedFilesList := task.relevantChangedFiles(changedFilesList) - cmdStr += " -- " + strings.Join(relevantChangedFilesList, " ") - } + relevantChangedFilesList := task.relevantChangedFiles(changedFilesList) + cmdStr += " " + strings.Join(relevantChangedFilesList, " ") return strings.Split(cmdStr, " ") } @@ -125,24 +122,16 @@ func (task Task) stageRelevantFiles() { } } -func (task Task) shouldRun(changed bool) bool { - // Always run all tasks if we aren't just looking at changed files - if !changed { - return true - } - - if task.Fix.Command != "" { - for _, file := range changedFilesList { - match, err := regexp.MatchString(task.Files, file) +func (task Task) shouldRun() bool { + for _, file := range changedFilesList { + match, err := regexp.MatchString(task.Files, file) - if err != nil { - panic(err) - } - if match { - return true - } + if err != nil { + panic(err) + } + if match { + return true } } - return false } diff --git a/core/task_test.go b/core/task_test.go index 14f6c89..ea45fc6 100644 --- a/core/task_test.go +++ b/core/task_test.go @@ -7,22 +7,13 @@ import ( "testing" ) -func TestShouldRunNotChanged(t *testing.T) { - task := Task{ - Name: "task", - Command: "run-task", - } - - assert.True(t, task.shouldRun(false), "It runs when not in changed mode") -} - func TestShouldRunNotChangedNoFix(t *testing.T) { task := Task{ Name: "task", Command: "run-task", } - assert.False(t, task.shouldRun(true), "It does not run when in changed mode with no fix command") + assert.False(t, task.shouldRun(), "It does not run when in changed mode with no fix command") } func TestShouldRunNotChangedWithFix(t *testing.T) { @@ -32,7 +23,7 @@ func TestShouldRunNotChangedWithFix(t *testing.T) { } task.Fix.Command = "run-fix" - assert.True(t, task.shouldRun(true), "It does not run when in changed mode with no fix command") + assert.True(t, task.shouldRun(), "It does not run when in changed mode with no fix command") } func TestPrepareCommandNoChangeNoFix(t *testing.T) { @@ -43,7 +34,7 @@ func TestPrepareCommandNoChangeNoFix(t *testing.T) { assert.Equal( t, []string{"run-task"}, - task.prepareCommand(false, true), + task.prepareCommand(true), "It runs the fix command when fix is true", ) } @@ -55,7 +46,7 @@ func TestPrepareCommandNoChangeFix(t *testing.T) { assert.Equal( t, []string{"run-fix"}, - task.prepareCommand(false, true), + task.prepareCommand(true), "It runs the fix command when fix is true", ) } @@ -72,7 +63,7 @@ func TestPrepareCommandWithChanged(t *testing.T) { assert.Equal( t, []string{"run-task", "--", "three.txt"}, - task.prepareCommand(true, false), + task.prepareCommand(false), "It correctly passes only the relevant files", ) } @@ -145,7 +136,7 @@ func TestExecuteSuccess(t *testing.T) { Command: "run-task", } - result := task.Execute(false, false) + result := task.Execute(false) assert.True(t, result.success, "The result is successful") assert.Equal(t, result.task, task, "It attaches the task") assert.Equal(t, result.output, "Output!", "It attaches the task") @@ -161,7 +152,7 @@ func TestExecuteFailure(t *testing.T) { Command: "run-task", } - result := task.Execute(false, false) + result := task.Execute(false) assert.False(t, result.success, "The result is failed") assert.Equal(t, result.task, task, "It attaches the task") assert.Equal(t, result.output, "Output!", "It attaches the task") @@ -177,7 +168,7 @@ func TestExecuteFixSuccessNoFixCommand(t *testing.T) { Command: "run-task", } - result := task.Execute(false, true) + result := task.Execute(true) assert.True(t, result.success, "The result is successful") assert.Equal(t, result.task, task, "It attaches the task") assert.Equal(t, result.output, "Output!", "It does not grep through the output") @@ -201,7 +192,7 @@ Linted: app/three.rb task.Fix.Command = "run-fix" task.Fix.Output = "Fixed:" - result := task.Execute(false, true) + result := task.Execute(true) assert.True(t, result.success, "The result is successful") assert.Equal(t, result.task, task, "It attaches the task") assert.Equal(t, result.output, "Linted: app/one.rb\nFixed: app/two.rb\nLinted: app/three.rb\n", "It attaches the entire output") @@ -222,7 +213,7 @@ func TestExecuteFixFailureWithFixCommand(t *testing.T) { task.Fix.Command = "run-fix" task.Fix.Output = "Fixed:" - result := task.Execute(false, true) + result := task.Execute(true) assert.False(t, result.success, "The result is successful") assert.Equal(t, result.task, task, "It attaches the task") assert.Equal(t, result.output, "Failed!", "It attaches the entire output") @@ -250,7 +241,7 @@ Linted: app/three.rb task.Fix.Command = "run-fix" task.Fix.Output = "Fixed:" - result := task.Execute(false, true) + result := task.Execute(true) assert.False(t, result.success, "The result is marked unsuccessful so changes can be staged") assert.Equal(t, result.task, task, "It attaches the task") assert.Equal(t, result.output, "Linted: app/one.rb\nFixed: app/two.rb\nLinted: app/three.rb\n", "It attaches the entire output")