Skip to content

Commit

Permalink
Merge pull request #13 from FalcoSuessgott/master
Browse files Browse the repository at this point in the history
golangci-lint, ci stages, makefile
  • Loading branch information
sachaos authored Aug 25, 2021
2 parents e032d45 + 4a8b204 commit 4019c17
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 39 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
- main
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.41.1
args: -c .golang-ci.yml

17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Test and coverage

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 2
- uses: actions/setup-go@v2
with:
go-version: '1.16'
- name: Run coverage
run: go test ./...

3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
coverage.out
viddy

15 changes: 15 additions & 0 deletions .golang-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
linters-settings:
lll:
line-length: 130
linters:
enable-all: true
disable:
- testpackage
- forbidigo
- paralleltest
- exhaustivestruct
- wrapcheck
- gomnd
- gochecknoglobals
- maligned
- exhaustive
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
default: help

.PHONY: help
help: ## list makefile targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

PHONY: test
test: ## run go tests
go test -v ./...

PHONY: cover
cover: ## display test coverage
go test -v -race $(shell go list ./... | grep -v /vendor/) -v -coverprofile=coverage.out
go tool cover -func=coverage.out

PHONY: fmt
fmt: ## format go files
gofumpt -w -s .
gci -w .

PHONY: lint
lint: ## lint go files
golangci-lint run -c .golang-ci.yml
15 changes: 8 additions & 7 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ type Arguments struct {
isHelp bool
isVersion bool

shell string
shell string
shellOpts string

cmd string
args []string
}

var (
NoCommand = errors.New("command is required")
IntervalTooSmall = errors.New("interval too small")
errNoCommand = errors.New("command is required")
errIntervalTooSmall = errors.New("interval too small")
)

func parseArguments(args []string) (*Arguments, error) {
Expand Down Expand Up @@ -60,18 +60,20 @@ func parseArguments(args []string) (*Arguments, error) {
if err != nil {
return &argument, err
}

interval = time.Duration(intervalFloat * float64(time.Second))
}

argument.interval = interval

if interval < 10 * time.Millisecond {
return nil, IntervalTooSmall
if interval < 10*time.Millisecond {
return nil, errIntervalTooSmall
}

rest := flagSet.Args()

if len(rest) == 0 {
return &argument, NoCommand
return &argument, errNoCommand
}

argument.cmd = rest[0]
Expand Down Expand Up @@ -99,4 +101,3 @@ Options:
-h, --help display this help and exit
-v, --version output version information and exit`)
}

8 changes: 7 additions & 1 deletion args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)

//nolint:funlen
func Test_parseArguments(t *testing.T) {
testCases := []struct {
name string
Expand All @@ -22,6 +23,7 @@ func Test_parseArguments(t *testing.T) {
isPrecise: false,
isClockwork: false,
cmd: "ls",
shell: "sh",
args: []string{"-l"},
},
},
Expand All @@ -33,6 +35,7 @@ func Test_parseArguments(t *testing.T) {
isPrecise: false,
isClockwork: false,
cmd: "ls",
shell: "sh",
args: []string{"-l"},
},
},
Expand All @@ -44,6 +47,7 @@ func Test_parseArguments(t *testing.T) {
isPrecise: false,
isClockwork: false,
cmd: "tail",
shell: "sh",
args: []string{"-n", "1", "hoge"},
},
},
Expand All @@ -55,6 +59,7 @@ func Test_parseArguments(t *testing.T) {
isPrecise: false,
isClockwork: false,
cmd: "tail",
shell: "sh",
args: []string{"-n", "1", "hoge"},
},
},
Expand All @@ -66,14 +71,15 @@ func Test_parseArguments(t *testing.T) {
isPrecise: false,
isClockwork: false,
cmd: "ls",
shell: "sh",
args: []string{},
},
},
{
name: "invalid interval",
args: []string{"-n", "1ms", "ls", "-l"},
exp: nil,
expErr: IntervalTooSmall,
expErr: errIntervalTooSmall,
},
}

Expand Down
13 changes: 10 additions & 3 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package main

import "time"

type newSnapFunc func(int64, *Snapshot, chan<-struct{}) *Snapshot
type newSnapFunc func(int64, *Snapshot, chan<- struct{}) *Snapshot

func ClockSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
c := make(chan *Snapshot)

go func() {
var s *Snapshot

t := time.Tick(interval)

for now := range t {
Expand All @@ -22,11 +23,12 @@ func ClockSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-c
return c
}

func PreciseSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
func PreciseSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
c := make(chan *Snapshot)

go func() {
var s *Snapshot

begin := time.Now().UnixNano()

for {
Expand All @@ -35,8 +37,11 @@ func PreciseSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <
id := (start.UnixNano() - begin) / int64(time.Millisecond)
ns := newSnap(id, s, finish)
s = ns

c <- ns

<-finish

pTime := time.Since(start)

if pTime > interval {
Expand All @@ -50,18 +55,20 @@ func PreciseSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <
return c
}

func SequentialSnapshot(begin int64, newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
func SequentialSnapshot(newSnap newSnapFunc, interval time.Duration) <-chan *Snapshot {
c := make(chan *Snapshot)

go func() {
var s *Snapshot

begin := time.Now().UnixNano()

for {
finish := make(chan struct{})
id := (time.Now().UnixNano() - begin) / int64(time.Millisecond)
s = newSnap(id, s, finish)
c <- s

<-finish

time.Sleep(interval)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func main() {
os.Exit(1)
}


var mode ViddyIntervalMode

switch {
case arguments.isPrecise:
mode = ViddyIntervalModePrecise
Expand Down
32 changes: 20 additions & 12 deletions snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"bytes"
"errors"
"fmt"
"io"
"os"
Expand All @@ -13,9 +12,8 @@ import (
"unicode"

"github.com/fatih/color"
"github.com/sergi/go-diff/diffmatchpatch"

"github.com/rivo/tview"
"github.com/sergi/go-diff/diffmatchpatch"
)

var dmp = diffmatchpatch.New()
Expand Down Expand Up @@ -46,13 +44,14 @@ type Snapshot struct {
finish chan<- struct{}
}

//nolint:lll
func NewSnapshot(id int64, command string, args []string, shell string, shellOpts string, before *Snapshot, finish chan<- struct{}) *Snapshot {
return &Snapshot{
id: id,
command: command,
args: args,

shell: shell,
shell: shell,
shellOpts: shellOpts,

before: before,
Expand All @@ -62,7 +61,7 @@ func NewSnapshot(id int64, command string, args []string, shell string, shellOpt

func (s *Snapshot) compareFromBefore() error {
if s.before != nil && !s.before.completed {
return errors.New("not completed")
return errNotCompletedYet
}

var beforeResult string
Expand All @@ -75,21 +74,25 @@ func (s *Snapshot) compareFromBefore() error {
s.diff = dmp.DiffCleanupSemantic(dmp.DiffMain(beforeResult, string(s.result), false))
addition := 0
deletion := 0

for _, diff := range s.diff {
//nolint:exhaustive
switch diff.Type {
case diffmatchpatch.DiffInsert:
addition += len(diff.Text)
case diffmatchpatch.DiffDelete:
deletion += len(diff.Text)
}
}

s.diffAdditionCount = addition
s.diffDeletionCount = deletion
s.diffPrepared = true

return nil
}

//nolint:unparam
func (s *Snapshot) run(finishedQueue chan<- int64) error {
s.start = time.Now()
defer func() {
Expand All @@ -102,21 +105,23 @@ func (s *Snapshot) run(finishedQueue chan<- int64) error {
commands = append(commands, s.args...)

var command *exec.Cmd

if runtime.GOOS == "windows" {
command = exec.Command(os.Getenv("COMSPEC"), "/c", strings.Join(commands, " "))
cmdStr := strings.Join(commands, " ")
compSec := os.Getenv("COMSPEC")
command = exec.Command(compSec, "/c", cmdStr)
} else {
var args []string
for _, o := range strings.Fields(s.shellOpts) {
args = append(args, o)
}
args = append(args, strings.Fields(s.shellOpts)...)
args = append(args, "-c")
args = append(args, strings.Join(commands, " "))
command = exec.Command(s.shell, args...)
command = exec.Command(s.shell, args...) //nolint:gosec
}

command.Stdout = &b

if err := command.Start(); err != nil {
return nil
return nil //nolint:nilerr
}

go func() {
Expand All @@ -135,8 +140,10 @@ func (s *Snapshot) run(finishedQueue chan<- int64) error {

func (s *Snapshot) render(w io.Writer, isShowDiff bool, query string) error {
var err error

var src string

//nolint:nestif
if isShowDiff {
if s.diffPrepared {
src = DiffPrettyText(s.diff)
Expand All @@ -153,10 +160,11 @@ func (s *Snapshot) render(w io.Writer, isShowDiff bool, query string) error {
}

if query != "" {
src = strings.Replace(src, query, fmt.Sprintf(`["s"]%s[""]`, query), -1)
src = strings.ReplaceAll(src, query, fmt.Sprintf(`["s"]%s[""]`, query))
}

_, err = io.Copy(tview.ANSIWriter(w), strings.NewReader(src))

return err
}

Expand Down
Loading

0 comments on commit 4019c17

Please sign in to comment.