Skip to content

Commit

Permalink
Replace the version command (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok authored Aug 17, 2022
1 parent c3b8381 commit 5ecf41d
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 130 deletions.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ builds:
- amd64
- arm64
ldflags:
- -s -w -X github.com/mszostok/codeowners-validator/pkg/version.version={{.Version}} -X github.com/mszostok/codeowners-validator/pkg/version.commit={{.ShortCommit}} -X github.com/mszostok/codeowners-validator/pkg/version.buildDate={{.Date}}
- -s -w -X go.szostok.io/version.version={{.Version}} -X go.szostok.io/version.buildDate={{.Date}}
# List of combinations of GOOS + GOARCH + GOARM to ignore.
# Default is empty.
ignore:
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export BINARY_PATH = $(ROOT_DIR)/codeowners-validator$(BINARY_EXT)
############

build:
go build -o $(BINARY_PATH) ./main.go
go build -o $(BINARY_PATH) .
.PHONY: build

build-race:
go build -race -o codeowners-validator ./main.go
go build -race -o $(BINARY_PATH) .
.PHONY: build-race

###########
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ curl -sfL https://raw.githubusercontent.com/mszostok/codeowners-validator/main/i
# In alpine linux (as it does not come with curl by default)
wget -O - -q https://raw.githubusercontent.com/mszostok/codeowners-validator/main/install.sh | sh -s v0.7.4
# Print version. Add `--short` to print just the version number.
codeowners-validator -v
# Print version. Add `--oshort` to print just the version number.
codeowners-validator version
```

You can also download [latest version](https://github.com/mszostok/codeowners-validator/releases/latest) from release page manually.
Expand Down
28 changes: 26 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,56 @@ require (
github.com/sergi/go-diff v1.1.0 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/afero v1.8.2
github.com/spf13/pflag v1.0.5
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.8.0
github.com/vrischmann/envconfig v1.3.0
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/oauth2 v0.0.0-20210218202405-ba52d332ba99
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/sys v0.0.0-20220209214540-3681064d5158 // indirect
gopkg.in/pipe.v2 v2.0.0-20140414041502-3c2ca4d52544
gopkg.in/src-d/go-git.v4 v4.13.1
gotest.tools v2.2.0+incompatible
)

require (
github.com/spf13/cobra v1.5.0
go.szostok.io/version v0.0.0-20220817223658-1e55930893cf
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/golang-jwt/jwt/v4 v4.0.0 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mitchellh/copystructure v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/reflectwalk v1.0.0 // indirect
github.com/muesli/termenv v0.12.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/src-d/gcfg v1.4.0 // indirect
github.com/xanzy/ssh-agent v0.2.1 // indirect
golang.org/x/text v0.3.4 // indirect
Expand Down
58 changes: 55 additions & 3 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion hack/run-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set -E # needs to be set if we want the ERR trap

CURRENT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
ROOT_PATH=$(cd "${CURRENT_DIR}/.." && pwd)
GOLANGCI_LINT_VERSION="v1.46.2"
GOLANGCI_LINT_VERSION="v1.47.2"
TMP_DIR=$(mktemp -d)

readonly CURRENT_DIR
Expand Down
2 changes: 1 addition & 1 deletion internal/check/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"strings"
"testing"

"go.szostok.io/codeowners-validator/internal/check"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.szostok.io/codeowners-validator/internal/check"

"go.szostok.io/codeowners-validator/pkg/codeowners"
)
Expand Down
110 changes: 64 additions & 46 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import (
"path/filepath"
"syscall"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.szostok.io/version/extension"

"go.szostok.io/codeowners-validator/internal/check"
"go.szostok.io/codeowners-validator/internal/envconfig"
"go.szostok.io/codeowners-validator/internal/load"
"go.szostok.io/codeowners-validator/internal/runner"
"go.szostok.io/codeowners-validator/pkg/codeowners"
"go.szostok.io/codeowners-validator/pkg/version"

"github.com/sirupsen/logrus"
)

// Config holds the application configuration
Expand All @@ -26,45 +27,14 @@ type Config struct {
}

func main() {
version.Init()
if version.ShouldPrintVersion() {
version.PrintVersion(os.Stdout)
os.Exit(0)
}

var cfg Config
err := envconfig.Init(&cfg)
exitOnError(err)

log := logrus.New()

ctx, cancelFunc := context.WithCancel(context.Background())
ctx, cancelFunc := WithStopContext(context.Background())
defer cancelFunc()
cancelOnInterrupt(ctx, cancelFunc)

// init checks
checks, err := load.Checks(ctx, cfg.Checks, cfg.ExperimentalChecks)
exitOnError(err)

// init codeowners entries
codeownersEntries, err := codeowners.NewFromPath(cfg.RepositoryPath)
exitOnError(err)

// run check runner
absRepoPath, err := filepath.Abs(cfg.RepositoryPath)
exitOnError(err)

checkRunner := runner.NewCheckRunner(log, codeownersEntries, absRepoPath, cfg.CheckFailureLevel, checks...)
checkRunner.Run(ctx)

if ctx.Err() != nil {
log.Error("Application was interrupted by operating system")
//nolint:gocritic
os.Exit(2)
}
if checkRunner.ShouldExitWithCheckFailure() {
if err := NewRoot().ExecuteContext(ctx); err != nil {
// error is already handled by `cobra`, we don't want to log it here as we will duplicate the message.
// If needed, based on error type we can exit with different codes.
//nolint:gocritic
os.Exit(3)
os.Exit(1)
}
}

Expand All @@ -74,17 +44,65 @@ func exitOnError(err error) {
}
}

// cancelOnInterrupt calls cancel func when os.Interrupt or SIGTERM is received
func cancelOnInterrupt(ctx context.Context, cancel context.CancelFunc) {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
// WithStopContext returns a copy of parent with a new Done channel. The returned
// context's Done channel is closed on of SIGINT or SIGTERM signals.
func WithStopContext(parent context.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(parent)

sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
select {
case <-ctx.Done():
case <-c:
case <-sigCh:
cancel()
<-c
os.Exit(1) // second signal. Exit directly.
}
}()

return ctx, cancel
}

// NewRoot returns a root cobra.Command for the whole Agent utility.
func NewRoot() *cobra.Command {
rootCmd := &cobra.Command{
Use: "codeowners-validator",
Short: "Ensures the correctness of your CODEOWNERS file.",
SilenceUsage: true,
Run: func(cmd *cobra.Command, args []string) {
var cfg Config
err := envconfig.Init(&cfg)
exitOnError(err)

log := logrus.New()

// init checks
checks, err := load.Checks(cmd.Context(), cfg.Checks, cfg.ExperimentalChecks)
exitOnError(err)

// init codeowners entries
codeownersEntries, err := codeowners.NewFromPath(cfg.RepositoryPath)
exitOnError(err)

// run check runner
absRepoPath, err := filepath.Abs(cfg.RepositoryPath)
exitOnError(err)

checkRunner := runner.NewCheckRunner(log, codeownersEntries, absRepoPath, cfg.CheckFailureLevel, checks...)
checkRunner.Run(cmd.Context())

if cmd.Context().Err() != nil {
log.Error("Application was interrupted by operating system")
os.Exit(2)
}
if checkRunner.ShouldExitWithCheckFailure() {
os.Exit(3)
}
},
}

rootCmd.AddCommand(
extension.NewVersionCobraCmd(),
)

return rootCmd
}
2 changes: 1 addition & 1 deletion pkg/codeowners/owners_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"path"
"testing"

"go.szostok.io/codeowners-validator/pkg/codeowners"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.szostok.io/codeowners-validator/pkg/codeowners"
)

const sampleCodeownerFile = `
Expand Down
11 changes: 0 additions & 11 deletions pkg/version/base.go

This file was deleted.

60 changes: 0 additions & 60 deletions pkg/version/version.go

This file was deleted.

0 comments on commit 5ecf41d

Please sign in to comment.