Skip to content

Commit

Permalink
Merge pull request #4 from Enapiuz/2-error-log
Browse files Browse the repository at this point in the history
show error log on fail + some visual improvements
  • Loading branch information
Enapiuz authored May 13, 2019
2 parents 546dacf + 2b0ef32 commit 203a240
Show file tree
Hide file tree
Showing 35 changed files with 4,698 additions and 32 deletions.
28 changes: 28 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This is an example goreleaser.yaml file with some sane defaults.
# Make sure to check the documentation at http://goreleaser.com
before:
hooks:
# you may remove this if you don't use vgo
- go mod download
# you may remove this if you don't need go generate
- go generate ./...
builds:
- env:
- CGO_ENABLED=0
archives:
- replacements:
darwin: Darwin
linux: Linux
windows: Windows
386: i386
amd64: x86_64
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ .Tag }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ script: make test

after_success:
- bash <(curl -s https://codecov.io/bash)

deploy:
- provider: script
skip_cleanup: true
script: curl -sL https://git.io/goreleaser | bash
on:
tags: true
condition: $TRAVIS_OS_NAME = linux
25 changes: 23 additions & 2 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 16 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
GOPACKAGES=$(shell find . -name '*.go' -not -path "./vendor/*" -exec dirname {} \; | uniq)
GOFILES_NOVENDOR=$(shell find . -type f -name '*.go' -not -path "./vendor/*")

build:
mkdir -p release/macos
mkdir -p release/linux
Expand All @@ -9,5 +12,17 @@ build:
GOOS=windows go build -o release/windows/multiwatch.exe
zip release/windows.zip release/windows/multiwatch.exe

watch:
go run main.go

test:
go test -race -coverprofile=coverage.txt -covermode=atomic -v ./...
go test -race -coverprofile=coverage.txt -covermode=atomic -v $(GOPACKAGES)

vet:
go vet $(GOPACKAGES)

lint:
ls $(GOFILES_NOVENDOR) | xargs -L1 golint -set_exit_status

imports:
goimports -w $(GOFILES_NOVENDOR)
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[![Go Report Card](https://goreportcard.com/badge/github.com/Enapiuz/multiwatch)](https://goreportcard.com/report/github.com/Enapiuz/multiwatch)
[![codecov](https://codecov.io/gh/Enapiuz/multiwatch/branch/master/graph/badge.svg)](https://codecov.io/gh/Enapiuz/multiwatch)
[![Maintainability](https://api.codeclimate.com/v1/badges/61bf67df2cdf15e5262f/maintainability)](https://codeclimate.com/github/Enapiuz/multiwatch/maintainability)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![Open Source Helpers](https://www.codetriage.com/enapiuz/multiwatch/badges/users.svg)](https://www.codetriage.com/enapiuz/multiwatch)
[![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://github.com/Enapiuz/multiwatch/blob/master/LICENSE)


Simple task runner on directory changes.
Expand Down
8 changes: 3 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ package main
import (
"log"

"github.com/Enapiuz/multiwatch/types"

"github.com/BurntSushi/toml"
"github.com/Enapiuz/multiwatch/printer"
"github.com/Enapiuz/multiwatch/types"
"github.com/Enapiuz/multiwatch/watcher"
)

func main() {
var config types.Config
var watchers = make([]*watcher.Watcher, 0)
var watchers = make([]watcher.Interface, 0)
needReprint := make(chan bool)
_, err := toml.DecodeFile("multiwatch.toml", &config)
if err != nil {
Expand All @@ -29,6 +28,5 @@ func main() {
statusPrinter.Start(needReprint)
needReprint <- true

done := make(chan bool)
<-done
select {}
}
27 changes: 21 additions & 6 deletions multiwatch.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
delay=500

[[watch]]
name = "linter"
paths = ["vendor/github.com"]
commands = ["sleep 1", "sleep 1", "ls /sfsdfsdf"]
name = "tests"
paths = ["printer", "watcher", "types"]
commands = ["make test"]

[[watch]]
name = "tests"
paths = ["."]
name = "vet"
paths = ["printer", "watcher", "types"]
commands = ["make vet"]

[[watch]]
name = "lint"
paths = ["printer", "watcher", "types"]
commands = ["make lint"]

[[watch]]
name = "goimports"
paths = ["printer", "watcher", "types"]
commands = ["make imports"]

[[watch]]
name = "something"
paths = ["vendor/github.com"]
ignorePrefixes = ["vendor"]
commands = ["sleep 5"]
commands = ["sleep 1", "sleep 1", "ls /sfsdfsdf"]
73 changes: 64 additions & 9 deletions printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@ package printer

import (
"fmt"
"github.com/Enapiuz/multiwatch/watcher"
"os"
"os/exec"
"runtime"
"strings"
"time"
"unicode/utf8"

"github.com/Enapiuz/multiwatch/watcher"
terminal "github.com/wayneashleyberry/terminal-dimensions"
)

// Printer watch for needReprint events and print all workers' statuses
type Printer struct {
watchers []*watcher.Watcher
watchers []watcher.Interface
clear map[string]func()
}

// NewPrinter makes new printer object
func NewPrinter() *Printer {
clear := make(map[string]func())
clear["darwin"] = func() {
Expand All @@ -33,33 +40,81 @@ func NewPrinter() *Printer {
return &Printer{clear: clear}
}

func (p *Printer) RegisterWatchers(watchers []*watcher.Watcher) {
// RegisterWatchers gets watchers to print when needReprint is toggled
func (p *Printer) RegisterWatchers(watchers []watcher.Interface) {
p.watchers = watchers
}

// Start watching for reprint event
func (p *Printer) Start(needReprint chan bool) {
go func() {
for {
select {
case _ = <-needReprint:
p.printWatchers()
p.padToTop()
}
}
}()
}

func (p *Printer) printWatchers() {
p.callClear()
for _, localWatcher := range p.watchers {
fmt.Println(localWatcher.GetStatus())
var statuses strings.Builder
for idx, localWatcher := range p.watchers {
if errorText := localWatcher.GetErrors(); errorText != "" {
fmt.Println(errorText)
}
var appendText strings.Builder
if idx == 0 {
appendText.WriteString(fmt.Sprint(localWatcher.GetStatus()))
width, err := terminal.Width()
if err == nil {
currentTime := fmt.Sprintf("%s\n", time.Now().Format("15:04:05"))
toRepeat := int(width) - utf8.RuneCountInString(currentTime) - utf8.RuneCountInString(appendText.String())
if toRepeat < 0 {
toRepeat = 0
}
appendText.WriteString(
strings.Repeat(
" ",
toRepeat,
),
)
appendText.WriteString(currentTime)
}
} else {
appendText.WriteString(fmt.Sprintln(localWatcher.GetStatus()))
}
statuses.WriteString(appendText.String())
}
fmt.Println(strings.Trim(statuses.String(), "\n"))
}

func (p *Printer) callClear() {
value, ok := p.clear[runtime.GOOS] //runtime.GOOS -> linux, windows, darwin etc.
if ok { //if we defined a clear func for that platform:
value() //we execute it
} else { //unsupported platform
value, ok := p.clear[runtime.GOOS]
if ok {
value()
} else {
panic("Your platform is unsupported! I can't clear terminal screen :(")
}
}

func (p *Printer) padToTop() {
y, err := terminal.Height()
if err == nil {
toRepeat := int(y) - len(p.watchers) - 1
if toRepeat < 0 {
toRepeat = 0
}
fmt.Print(strings.Repeat("\n", toRepeat))
}
x, err := terminal.Width()
if err == nil {
toRepeat := int(x) - 1
if toRepeat < 0 {
toRepeat = 0
}
fmt.Print(strings.Repeat(" ", toRepeat))
}
}
61 changes: 61 additions & 0 deletions printer/printer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package printer

import (
"io/ioutil"
"os"
"testing"
"time"

"github.com/Enapiuz/multiwatch/watcher"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

type WatcherMock struct {
mock.Mock
}

func (w *WatcherMock) Run(chan bool) {
panic("implement me")
}

func (w *WatcherMock) GetStatus() string {
return "status_log"
}

func (w *WatcherMock) GetErrors() string {
return "error_log"
}

func TestNewPrinter(t *testing.T) {
samplePrinter := NewPrinter()
assert.Len(t, samplePrinter.watchers, 0)
}

func TestPrinter_RegisterWatchers(t *testing.T) {
testPrinter := NewPrinter()
dumbWatcher := &WatcherMock{}
testPrinter.RegisterWatchers([]watcher.Interface{dumbWatcher})
assert.Len(t, testPrinter.watchers, 1)
}

func TestPrinter_Start(t *testing.T) {
rescueStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

testPrinter := NewPrinter()
dumbWatcher := &WatcherMock{}
testPrinter.RegisterWatchers([]watcher.Interface{dumbWatcher})
needReprint := make(chan bool)
testPrinter.Start(needReprint)
needReprint <- true

time.Sleep(1 * time.Second)
w.Close()
out, _ := ioutil.ReadAll(r)
os.Stdout = rescueStdout

assert.Contains(t, string(out), "status_log")
assert.Contains(t, string(out), "error_log")
}
2 changes: 2 additions & 0 deletions types/config.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package types

// DirectoryConfig Config for one worker
type DirectoryConfig struct {
Name string
Paths []string
IgnorePrefixes []string
Commands []string
}

// Config Application config
type Config struct {
Delay int32
Watch []DirectoryConfig
Expand Down
13 changes: 13 additions & 0 deletions vendor/github.com/stretchr/objx/.codeclimate.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 203a240

Please sign in to comment.