Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Homebrew
uses: Homebrew/actions/setup-homebrew@main
uses: Homebrew/actions/setup-homebrew@0fd01fffe2bb7ba090ade9655fe7e89ab5c613d4 # main
- name: Configure git
run: |
git config --global user.name "GitHub Actions"
git config --global user.email "actions@github.com"
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
- name: Calculate SHA256
id: sha
run: |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ The suggested way to start running crie is to run `chk` at the top of your proje
crie chk --continue
```

## 📚 Documentation
## Documentation

See the **[documentation](doc/README.md)** for installation, configuration, and usage guides.

Expand Down
2 changes: 1 addition & 1 deletion internal/cli/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/tyhal/crie/internal/errchain"
"github.com/tyhal/crie/pkg/errchain"
)

// addCrieCommand is the same as addLintCommand but only to ensure the languages are loaded
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tyhal/crie/internal/config/language"
"github.com/tyhal/crie/internal/errchain"
"github.com/tyhal/crie/internal/runner"
"github.com/tyhal/crie/pkg/errchain"
)

var crieRun runner.RunConfiguration
Expand Down
2 changes: 2 additions & 0 deletions internal/cli/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"os"
"sort"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -32,6 +33,7 @@ func setLogging() {
log.SetFormatter(&log.JSONFormatter{})
projectConfig.Lint.StrictLogging = true
} else {
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
SortingFunc: msgLast,
DisableQuote: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tyhal/crie/internal/config/project"
"github.com/tyhal/crie/internal/errchain"
"github.com/tyhal/crie/pkg/errchain"
)

//`
Expand Down
2 changes: 1 addition & 1 deletion internal/config/language/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"os"

"github.com/tyhal/crie/internal/errchain"
"github.com/tyhal/crie/pkg/errchain"
"gopkg.in/yaml.v3"
)

Expand Down
2 changes: 1 addition & 1 deletion internal/filelist/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/tyhal/crie/internal/errchain"
"github.com/tyhal/crie/pkg/errchain"
)

// GetGitRepo opens a git repository rooted at dir (or its parents when .git is detected)
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/olekukonko/tablewriter"
log "github.com/sirupsen/logrus"
"github.com/tyhal/crie/internal/errchain"
"github.com/tyhal/crie/pkg/errchain"
"github.com/tyhal/crie/pkg/linter"
)

Expand Down
File renamed without changes.
File renamed without changes.
39 changes: 39 additions & 0 deletions pkg/folding/folding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Package folding provides a way to fold / collapse log messages when it detects it's running in a CI environment'
package folding

import (
"io"
"os"
)

// Folder represents a CI environment that supports folding / collapsing log messages
type Folder interface {
Start(file string, msg string, open bool) (string, error)
Stop(id string) error
}

func isEnvSet(key string) bool {
_, exists := os.LookupEnv(key)
return exists
}

func newFrom(w io.Writer, isSet func(string) bool) Folder {
switch {
case isSet("GITHUB_ACTIONS"):
return NewGithub(w)
case isSet("GITLAB_CI"):
return NewGitlab(w)
default:
return NewPlain(w)
}
}

// NewW returns a Folding instance based on a custom writer
func NewW(w io.Writer) Folder {
return newFrom(w, isEnvSet)
}

// New returns a Folding instance based on if a CI environment variable is set
func New() Folder {
return newFrom(os.Stdout, isEnvSet)
}
44 changes: 44 additions & 0 deletions pkg/folding/folding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package folding

import (
"bytes"
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func getFoldWithEnv(t *testing.T, env string) Folder {
t.Helper()
folder := newFrom(os.Stdout, func(s string) bool {
return s == env
})
return folder
}

func TestSetEnv(t *testing.T) {
t.Setenv("CRIE_RANDOM_ENV", "RANDOM_VALUE")
is := isEnvSet("CRIE_RANDOM_ENV")
assert.True(t, is)
isnt := isEnvSet("CRIE_UNSET_ENV")
assert.False(t, isnt)
}

func TestNewFolder(t *testing.T) {
var b bytes.Buffer
folder := NewW(&b)
folder.Start("a/b", "hello", true)
assert.Greater(t, b.Len(), 0)

folder = New()
assert.NotNil(t, folder)

folder = getFoldWithEnv(t, "FALLBACK")
assert.IsType(t, &plainFolder{}, folder)

folder = getFoldWithEnv(t, "GITLAB_CI")
assert.IsType(t, &gitlabFolder{}, folder)

folder = getFoldWithEnv(t, "GITHUB_ACTIONS")
assert.IsType(t, &githubFolder{}, folder)
}
29 changes: 29 additions & 0 deletions pkg/folding/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package folding

import (
"fmt"
"io"
)

type githubFolder struct {
io.Writer
}

// NewGithub uses the GitHub Actions log syntax
func NewGithub(w io.Writer) Folder {
return &githubFolder{w}
}

func (g githubFolder) Start(file string, msg string, _ bool) (string, error) {
_, err := fmt.Fprintf(g, "::error file=%s::%s\n", file, file)
if err != nil {
return "", err
}
_, err = fmt.Fprintf(g, "::group::%s see logs\n", msg)
return "", err
}

func (g githubFolder) Stop(_ string) error {
_, err := fmt.Fprintf(g, "::endgroup::\n")
return err
}
17 changes: 17 additions & 0 deletions pkg/folding/github_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package folding

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGithubFolder(t *testing.T) {
f := NewGithub(os.Stdout)
id, err := f.Start("a/b", "hello", true)
assert.NoError(t, err)
assert.Equal(t, "", id)
err = f.Stop(id)
assert.NoError(t, err)
}
40 changes: 40 additions & 0 deletions pkg/folding/gitlab.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package folding

import (
"fmt"
"io"
"math/rand"
"time"
)

type gitlabFolder struct {
io.Writer
}

// NewGitlab uses the Gitlab CI log syntax
func NewGitlab(w io.Writer) Folder {
return &gitlabFolder{w}
}

func (g gitlabFolder) Start(file, msg string, open bool) (string, error) {
id := fmt.Sprintf("%08x\n", rand.Int31())
collapsed := "true"
if open {
collapsed = "false"
}
_, err := fmt.Fprintf(g, "\033[0Ksection_start:%d:%s[collapsed=%s]\r\033[0K%s %v\n",
time.Now().Unix(),
id,
collapsed,
msg,
file)
if err != nil {
return "", err
}
return id, nil
}

func (g gitlabFolder) Stop(id string) error {
_, err := fmt.Fprintf(g, "\033[0Ksection_end:%d:%s\r\033[0K", time.Now().Unix(), id)
return err
}
17 changes: 17 additions & 0 deletions pkg/folding/gitlab_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package folding

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGitlabFolder(t *testing.T) {
f := NewGitlab(os.Stdout)
id, err := f.Start("a/b", "hello", true)
assert.NoError(t, err)
assert.NotEqual(t, "", id)
err = f.Stop(id)
assert.NoError(t, err)
}
27 changes: 27 additions & 0 deletions pkg/folding/plain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package folding

import (
"fmt"
"io"
)

type plainFolder struct {
io.Writer
}

// NewPlain is a basic implementation of the Folder interface
func NewPlain(w io.Writer) Folder {
return &plainFolder{w}
}

func (p plainFolder) Start(file, msg string, _ bool) (string, error) {
_, err := fmt.Fprintf(p, "%s %v\n\n", msg, file)
if err != nil {
return "", err
}
return "", nil
}

func (p plainFolder) Stop(_ string) error {
return nil
}
2 changes: 1 addition & 1 deletion pkg/linter/cli/limits_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"syscall"

log "github.com/sirupsen/logrus"
"github.com/tyhal/crie/internal/errchain"
"github.com/tyhal/crie/pkg/errchain"
)

var maxFilesPerRoutine = 5
Expand Down
2 changes: 1 addition & 1 deletion pkg/linter/dockfmt/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"strings"

"github.com/reteps/dockerfmt/lib"
"github.com/tyhal/crie/internal/errchain"
"github.com/tyhal/crie/pkg/errchain"
)

func (l *LintDockFmt) format(filepath string) error {
Expand Down
10 changes: 8 additions & 2 deletions pkg/linter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"sync"

log "github.com/sirupsen/logrus"
"github.com/tyhal/crie/pkg/folding"
)

// Runner will handle parallel runs of linters
type Runner struct {
ShowPass bool
StrictLogging bool
folder folding.Folder
}

func (r *Runner) logConditional(reader io.Reader, typeField string, level log.Level) {
Expand All @@ -38,10 +40,11 @@ func (r *Runner) Log(rep *Report) error {
r.logConditional(rep.StdOut, "stdout", log.DebugLevel)
}
} else {
var id string
if r.StrictLogging {
log.Printf("fail %v", rep.File)
} else {
fmt.Printf("\u274C %v\n\n", rep.File)
id, _ = r.folder.Start(rep.File, "\u2716", false)
}
var failedResultErr *FailedResultError
if errors.As(rep.Err, &failedResultErr) {
Expand All @@ -51,12 +54,15 @@ func (r *Runner) Log(rep *Report) error {
} else {
r.logConditional(strings.NewReader(rep.Err.Error()), "toolerr", log.ErrorLevel)
}
if id != "" {
_ = r.folder.Stop(id)
}
}

return rep.Err
}

func (r *Runner) listen(results chan error, linterReport chan Report) {
r.folder = folding.New()
for report := range linterReport {
err := r.Log(&report)
results <- err
Expand Down
Loading