From 7ad1e6fd1a6fd153d764873e168b52a563ed9b18 Mon Sep 17 00:00:00 2001 From: tycho garen Date: Thu, 7 Jan 2021 17:29:30 -0500 Subject: [PATCH 1/2] add github actions ci --- .github/workflows/test.yaml | 25 +++++++ buildscripts/run-linter.go | 140 ------------------------------------ 2 files changed, 25 insertions(+), 140 deletions(-) create mode 100644 .github/workflows/test.yaml delete mode 100644 buildscripts/run-linter.go diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..9b2d394 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,25 @@ +name: test +on: [push] + +jobs: + run: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions/cache@v1 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: Grip Tests + run: make test + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: golangci-lint + uses: golangci/golangci-lint-action@v2 + with: + version: v1.31.0 + diff --git a/buildscripts/run-linter.go b/buildscripts/run-linter.go deleted file mode 100644 index 1942633..0000000 --- a/buildscripts/run-linter.go +++ /dev/null @@ -1,140 +0,0 @@ -package main - -import ( - "bytes" - "flag" - "fmt" - "os" - "os/exec" - "path/filepath" - "runtime" - "strings" - "time" - - "github.com/cdr/grip" -) - -type result struct { - name string - cmd string - passed bool - duration time.Duration - output []string -} - -// String prints the results of a linter run in gotest format. -func (r *result) String() string { - buf := &bytes.Buffer{} - - fmt.Fprintln(buf, "=== RUN", r.name) - if r.passed { - fmt.Fprintf(buf, "--- PASS: %s (%s)", r.name, r.duration) - } else { - fmt.Fprintf(buf, strings.Join(r.output, "\n")) - fmt.Fprintf(buf, "--- FAIL: %s (%s)", r.name, r.duration) - } - - return buf.String() -} - -// fixup goes through the output and improves the output generated by -// specific linters so that all output includes the relative path to the -// error, instead of mixing relative and absolute paths. -func (r *result) fixup(dirname string) { - for idx, ln := range r.output { - if strings.HasPrefix(ln, dirname) { - r.output[idx] = ln[len(dirname)+1:] - } - } -} - -// runs the golangci-lint on a list of packages; integrating with the "make lint" target. -func main() { - var ( - lintArgs string - lintBin string - customLintersFlag string - customLinters []string - packageList string - output string - packages []string - results []*result - hasFailingTest bool - - gopath = os.Getenv("GOPATH") - ) - - gopath, _ = filepath.Abs(gopath) - - flag.StringVar(&lintArgs, "lintArgs", "", "args to pass to golangci-lint") - flag.StringVar(&lintBin, "lintBin", filepath.Join(gopath, "bin", "golangci-lint"), "path to golangci-lint") - flag.StringVar(&packageList, "packages", "", "list of space separated packages") - flag.StringVar(&customLintersFlag, "customLinters", "", "list of comma-separated custom linter commands") - flag.StringVar(&output, "output", "", "output file for to write results.") - flag.Parse() - - if len(customLintersFlag) != 0 { - customLinters = strings.Split(customLintersFlag, ",") - } - packages = strings.Split(strings.Replace(packageList, "-", "/", -1), " ") - dirname, _ := os.Getwd() - cwd := filepath.Base(dirname) - lintArgs += fmt.Sprintf(" --concurrency=%d", runtime.NumCPU()/2) - - for _, pkg := range packages { - pkgDir := "./" - if cwd != pkg { - pkgDir += pkg - } - args := []string{lintBin, "run", lintArgs, pkgDir} - - startAt := time.Now() - cmd := strings.Join(args, " ") - out, err := exec.Command("sh", "-c", cmd).CombinedOutput() - r := &result{ - cmd: strings.Join(args, " "), - name: "lint-" + strings.Replace(pkg, "/", "-", -1), - passed: err == nil, - duration: time.Since(startAt), - output: strings.Split(string(out), "\n"), - } - for _, linter := range customLinters { - customLinterStart := time.Now() - out, err = exec.Command("sh", "-c", fmt.Sprintf("%s %s", linter, pkgDir)).CombinedOutput() - r.passed = r.passed && err == nil - r.duration += time.Since(customLinterStart) - r.output = append(r.output, strings.Split(string(out), "\n")...) - } - r.fixup(dirname) - - if !r.passed { - hasFailingTest = true - } - - results = append(results, r) - fmt.Println(r) - } - - if output != "" { - f, err := os.Create(output) - if err != nil { - os.Exit(1) - } - defer func() { - if err != f.Close() { - panic(err) - } - }() - - for _, r := range results { - if _, err = f.WriteString(r.String() + "\n"); err != nil { - grip.Error(err) - os.Exit(1) - } - } - } - - if hasFailingTest { - os.Exit(1) - } -} From 05b58491df092fd3d42b618ac7bff4519e7ad81d Mon Sep 17 00:00:00 2001 From: tycho garen Date: Thu, 7 Jan 2021 17:35:14 -0500 Subject: [PATCH 2/2] fix lint --- message/function.go | 2 +- send/desktop.go | 8 ++++++-- send/systemd_linux.go | 2 +- send/twitter_test.go | 6 +++--- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/message/function.go b/message/function.go index 1ffca9d..929e6e1 100644 --- a/message/function.go +++ b/message/function.go @@ -188,7 +188,7 @@ func (cp *composerProducerMessage) resolve() { if cp.cached == nil { cp.cached = NewSimpleFields(cp.level, Fields{}) } else { - cp.cached.SetPriority(cp.level) + _ = cp.cached.SetPriority(cp.level) } } } diff --git a/send/desktop.go b/send/desktop.go index 78aca32..3a12662 100644 --- a/send/desktop.go +++ b/send/desktop.go @@ -39,9 +39,13 @@ func MakeDesktopNotify(name string) (Sender, error) { func (s *desktopNotify) Send(m message.Composer) { if s.Level().ShouldLog(m) { if m.Priority() >= level.Critical { - beeep.Alert(s.Name(), m.String(), "") + if err := beeep.Alert(s.Name(), m.String(), ""); err != nil { + s.ErrorHandler()(err, m) + } } else { - beeep.Notify(s.Name(), m.String(), "") + if err := beeep.Notify(s.Name(), m.String(), ""); err != nil { + s.ErrorHandler()(err, m) + } } } } diff --git a/send/systemd_linux.go b/send/systemd_linux.go index 470101e..0fda05b 100644 --- a/send/systemd_linux.go +++ b/send/systemd_linux.go @@ -6,9 +6,9 @@ import ( "log" "os" - "github.com/coreos/go-systemd/journal" "github.com/cdr/grip/level" "github.com/cdr/grip/message" + "github.com/coreos/go-systemd/journal" ) type systemdJournal struct { diff --git a/send/twitter_test.go b/send/twitter_test.go index 3839bbb..882312d 100644 --- a/send/twitter_test.go +++ b/send/twitter_test.go @@ -34,7 +34,7 @@ func TestTwitter(t *testing.T) { t.Run("Constructors", func(t *testing.T) { t.Run("NilCredentialsPanic", func(t *testing.T) { - assert.Panics(t, func() { MakeTwitterLogger(ctx, nil) }) + assert.Panics(t, func() { _, _ = MakeTwitterLogger(ctx, nil) }) }) t.Run("EmptyCredentialsError", func(t *testing.T) { s, err := MakeTwitterLogger(ctx, &TwitterOptions{}) @@ -65,7 +65,7 @@ func TestTwitter(t *testing.T) { errsender, err := NewInternalLogger("errr", LevelInfo{level.Info, level.Info}) require.NoError(t, err) s := newMockedTwitterSender(mock) - s.SetErrorHandler(ErrorHandlerFromSender(errsender)) + require.NoError(t, s.SetErrorHandler(ErrorHandlerFromSender(errsender))) mock.SendError = errors.New("sendERROR") msg := message.NewSimpleStringMessage(level.Info, "hi") @@ -84,7 +84,7 @@ func TestTwitter(t *testing.T) { Base: NewBase("fake"), } - s.SetErrorHandler(ErrorHandlerFromSender(errsender)) + require.NoError(t, s.SetErrorHandler(ErrorHandlerFromSender(errsender))) msg := message.NewSimpleStringMessage(level.Info, "hi") s.Send(msg)