Skip to content

Commit

Permalink
Add flag utils (#10)
Browse files Browse the repository at this point in the history
* add flag validation error util
* remove unused variable
* fix formatting
* cleanup signal unit
* CI: add newer Go versions to test against
* CI: bump versions of Github actions
  • Loading branch information
basvanbeek authored Aug 4, 2023
1 parent 03555ec commit 70f7053
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 29 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ jobs:
go-version:
- 1.16.x
- 1.17.x
- 1.18.x
- 1.19.x
- 1.20.x
os:
- ubuntu-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- run: go test ./...
Expand Down
3 changes: 1 addition & 2 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ type Group struct {
s []Service
x []ServiceContext

configured bool
hsRegistered bool
configured bool
}

// Register will inspect the provided objects implementing the Unit interface to
Expand Down
32 changes: 32 additions & 0 deletions pkg/flag/flag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package flag

import "fmt"

// ValidationError provides the ability to create constant errors for run.Group
// validation errors, e.g. incorrect flag values.
type ValidationError string

// Error implements the built-in error interface.
func (v ValidationError) Error() string { return string(v) }

// NewValidationError provides a convenient helper function to create flag
// validation errors usable by run.Config implementations.
func NewValidationError(flag string, reason error) error {
return fmt.Errorf(FlagErr, flag, reason)
}

const (
// FlagErr can be used as formatting string for flag related validation
// errors where the first variable lists the flag name and the second
// variable is the actual error.
FlagErr = "--%s error: %w"

// ErrRequired is returned when required config options are not provided.
ErrRequired ValidationError = "required"

// ErrInvalidPath is returned when a path config option is invalid.
ErrInvalidPath ValidationError = "invalid path"

// ErrInvalidVal is returned when the value passed into a flag argument is invalid.
ErrInvalidVal ValidationError = "invalid value"
)
19 changes: 1 addition & 18 deletions pkg/signal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ import (
"github.com/tetratelabs/run"
)

// Error allows for creating constant errors instead of sentinel ones.
type Error string

// Error implements error.
func (e Error) Error() string { return string(e) }

// Handler implements a unix signal handler as run.GroupService.
type Handler struct {
// RefreshCallback is called when a syscall.SIGHUP is received.
Expand All @@ -40,11 +34,10 @@ type Handler struct {
RefreshCallback func() error

signal chan os.Signal
cancel chan struct{}
}

// Name implements run.Unit.
func (h Handler) Name() string {
func (h *Handler) Name() string {
return "signal"
}

Expand Down Expand Up @@ -86,13 +79,3 @@ func (h *Handler) ServeContext(ctx context.Context) error {
}
}
}

// sendHUP is for test purposes
func (h *Handler) sendHUP() {
h.signal <- syscall.SIGHUP
}

// sendQUIT is for test purposes
func (h *Handler) sendQUIT() {
h.signal <- syscall.SIGQUIT
}
11 changes: 11 additions & 0 deletions pkg/signal/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package signal

import (
"errors"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -108,3 +109,13 @@ func TestSignalHandlerSignals(t *testing.T) {

}
}

// sendHUP is for test purposes
func (h *Handler) sendHUP() {
h.signal <- syscall.SIGHUP
}

// sendQUIT is for test purposes
func (h *Handler) sendQUIT() {
h.signal <- syscall.SIGQUIT
}
18 changes: 11 additions & 7 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (

// gitDescribeHashIndexPattern matches the git describe hash index pattern in the version string.
// The version string should be in the format:
// <release tag>-<commits since release tag>-g<commit hash>-<branch name>
//
// <release tag>-<commits since release tag>-g<commit hash>-<branch name>
//
// As an example: 0.6.6-rc1-15-g12345678-want-more-branch, the "g" prefix stands for "git"
// (see: https://git-scm.com/docs/git-describe).
var gitDescribeHashIndexPattern = regexp.MustCompile(`-[0-9]+(-g+)+`)
Expand All @@ -37,11 +39,12 @@ var gitCommitsAheadPattern = regexp.MustCompile(`[0-9]+`)
// build is to be populated at build time using -ldflags -X.
//
// Example:
// VERSION_PATH := github.com/tetratelabs/run/pkg/version
// VERSION_STRING := $(shell git describe --tags --long)
// GIT_BRANCH_NAME := $(shell git rev-parse --abbrev-ref HEAD)
// GO_LINK_VERSION := -X ${VERSION_PATH}.build=${VERSION_STRING}-${GIT_BRANCH_NAME}
// go build -ldflags '${GO_LINK_VERSION}'
//
// VERSION_PATH := github.com/tetratelabs/run/pkg/version
// VERSION_STRING := $(shell git describe --tags --long)
// GIT_BRANCH_NAME := $(shell git rev-parse --abbrev-ref HEAD)
// GO_LINK_VERSION := -X ${VERSION_PATH}.build=${VERSION_STRING}-${GIT_BRANCH_NAME}
// go build -ldflags '${GO_LINK_VERSION}'
var build string

// mainBranches is a list of (sorted) main branches/revisions.
Expand Down Expand Up @@ -85,7 +88,8 @@ func (g Git) String() string {

// parseGit the given version string into a version object. The input version string
// is in the format:
// <release tag>-<commits since release tag>-g<commit hash>-<branch name>
//
// <release tag>-<commits since release tag>-g<commit hash>-<branch name>
func parseGit(v string) Git {
// Here we try to find the "-<commits since release tag>-g"-part.
found := gitDescribeHashIndexPattern.FindStringIndex(v)
Expand Down

0 comments on commit 70f7053

Please sign in to comment.