Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add GitHub ci #1

Closed
wants to merge 4 commits into from
Closed
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
63 changes: 63 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Linter

on: [pull_request]

jobs:
go-staticcheck:
name: go-staticcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: 1.23.x

- name: Install go-staticcheck
run: |
go get honnef.co/go/tools/cmd/staticcheck@latest # ideally we should version pin

- name: Run go-staticcheck
run: |
PKGS=$(go list ./... | grep -v /vendor/ | grep -v example )
go run honnef.co/go/tools/cmd/staticcheck $PKGS

go-vet:
name: go-vet
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: 1.23.x

- name: Run go-vet
run: |
PKGS=$(go list ./... | grep -v /vendor/ | grep -v example )
go vet -json $PKGS | tee vet-report.json

- name: Upload results
uses: actions/upload-artifact@v3
with:
name: go-vet-results
path: vet-report.json


golangci-lint:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v3
with:
go-version: 1.23.x

- name: Install golangci-lint
run: |
go get github.com/golangci/golangci-lint/cmd/golangci-lint@latest

- name: Run golangci-lint
run: |
go run github.com/golangci/golangci-lint/cmd/golangci-lint run -v --timeout 10m0s
84 changes: 84 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This workflow will install Go dependencies, run tests for 4 versions of Go
# For more information see: https://support.github.com/features/actions

name: Unit tests

on:
pull_request:
paths:
- ".github/workflows/*.yml"
- "**/*.go"
- "**/go.mod"
- "**/go.sum"

permissions:
contents: read
pull-requests: write

jobs:
go-test:
name: Run go unit tests
strategy:
matrix:
go-version: ["1.18", "1.19", "1.23"]
runs-on: ubuntu-latest

steps:
- name: "Install Go"
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- uses: actions/checkout@v3

- name: Install test dependencies
run: |
go get github.com/boumenot/gocover-cobertura

- name: Install project dependencies
run: go build

- name: Run test
run: |
# run test:
PKGS=$(go list ./... | grep -v /vendor/)
DEPS=$(go list ./... | grep -v vendor | grep -v test | xargs | sed 's/ /,/g')
go test ${PKGS} -v \
-coverprofile=coverage_${{ matrix.go-version }}.out \
-covermode=count \
-coverpkg ${DEPS} 2>&1

- name: Generate code coverage report
run: |
go run github.com/boumenot/gocover-cobertura < coverage_${{ matrix.go-version }}.out > coverage-unit_${{ matrix.go-version }}.xml

- name: compute valid coverage total
run: |
go tool cover -func=coverage_${{ matrix.go-version }}.out

- name: Code Coverage Report
uses: irongut/[email protected]
if: matrix.go-version == '1.19'
with:
# will generate code-coverage-results.md
filename: coverage-unit_*.xml
badge: true
fail_below_min: true
format: markdown
hide_branch_rate: false
hide_complexity: true
indicators: true
output: both
thresholds: "5 10"

- name: Add coverage comment to PR
uses: marocchino/sticky-pull-request-comment@v2
if: matrix.go-version == '1.19' && github.event_name == 'pull_request'
with:
path: code-coverage-results.md

- name: Upload coverage results
uses: actions/upload-artifact@v3
with:
name: go-coverage-results
path: coverage-unit_*.xml
30 changes: 30 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
linters:
# Enable all available linters.
# Default: false
enable-all: true
# Disable specific linter
# https://golangci-lint.run/usage/linters/#disabled-by-default
disable:
- copyloopvar # we use a too old go version
- intrange # we use a too old go version
- execinquery # deprecated
- exportloopref # deprecated
# - depguard
# - gci
# - godox
# - gofumpt
- gomnd
- wsl
- wrapcheck # old code => no wrapped error
- varnamelen
- paralleltest
- ireturn
- mnd
- testpackage
- err113 # old code => no wrapped error
- stylecheck
- cyclop
# - revive
- nlreturn
- exhaustruct

9 changes: 7 additions & 2 deletions console.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

// ConsoleLogger logs to the std err.
var ConsoleLogger = consoleLogger{}
var ConsoleLogger = consoleLogger{} //nolint:gochecknoglobals

type consoleLogger struct {
info, warn, err *log.Logger
}

func init() {
func init() { //nolint:gochecknoinits
ConsoleLogger.info = log.New(os.Stderr, "I: ", log.Ltime)
ConsoleLogger.warn = log.New(os.Stderr, "W: ", log.Ltime)
ConsoleLogger.err = log.New(os.Stderr, "E: ", log.Ltime)
Expand All @@ -26,22 +26,27 @@ func (c consoleLogger) Error(v ...interface{}) error {
c.err.Print(v...)
return nil
}

func (c consoleLogger) Warning(v ...interface{}) error {
c.warn.Print(v...)
return nil
}

func (c consoleLogger) Info(v ...interface{}) error {
c.info.Print(v...)
return nil
}

func (c consoleLogger) Errorf(format string, a ...interface{}) error {
c.err.Printf(format, a...)
return nil
}

func (c consoleLogger) Warningf(format string, a ...interface{}) error {
c.warn.Printf(format, a...)
return nil
}

func (c consoleLogger) Infof(format string, a ...interface{}) error {
c.info.Printf(format, a...)
return nil
Expand Down
23 changes: 16 additions & 7 deletions example/logging/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
"log"
"time"

"github.com/kardianos/service"
"github.com/kardianos/service" //nolint:depguard
)

var logger service.Logger
var logger service.Logger //nolint:gochecknoglobals

// Program structures.
//
Expand All @@ -22,7 +22,8 @@ type program struct {
exit chan struct{}
}

func (p *program) Start(s service.Service) error {
//nolint:errcheck
func (p *program) Start(_ service.Service) error {
if service.Interactive() {
logger.Info("Running in terminal.")
} else {
Expand All @@ -34,6 +35,8 @@ func (p *program) Start(s service.Service) error {
go p.run()
return nil
}

//nolint:errcheck,unparam
func (p *program) run() error {
logger.Infof("I'm running %v.", service.Platform())
ticker := time.NewTicker(2 * time.Second)
Expand All @@ -47,7 +50,9 @@ func (p *program) run() error {
}
}
}
func (p *program) Stop(s service.Service) error {

//nolint:errcheck
func (p *program) Stop(_ service.Service) error {
// Any work in Stop should be quick, usually a few seconds at most.
logger.Info("I'm Stopping!")
close(p.exit)
Expand All @@ -74,7 +79,8 @@ func main() {
Description: "This is an example Go service that outputs log messages.",
Dependencies: []string{
"Requires=network.target",
"After=network-online.target syslog.target"},
"After=network-online.target syslog.target",
},
Option: options,
}

Expand All @@ -101,13 +107,16 @@ func main() {
if len(*svcFlag) != 0 {
err := service.Control(s, *svcFlag)
if err != nil {
log.Printf("Valid actions: %q\n", service.ControlAction)
log.Printf("Valid actions: %q\n", []string{
service.ControlActionStart, service.ControlActionStop,
service.ControlActionRestart, service.ControlActionInstall, service.ControlActionUninstall,
})
log.Fatal(err)
}
return
}
err = s.Run()
if err != nil {
logger.Error(err)
_ = logger.Error(err)
}
}
Loading
Loading