Skip to content

Commit

Permalink
Retcode package code (#1)
Browse files Browse the repository at this point in the history
Signed-off-by: Krzysztof Suszyński <[email protected]>
  • Loading branch information
cardil authored Jul 8, 2022
1 parent 3359eb5 commit 6553dd3
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 2 deletions.
19 changes: 19 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 2

[{*.go,go.mod,Makefile}]
indent_style = tab

# Ignore paths
[LICENSE]
indent_size = unset
33 changes: 33 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Go

on:
push:
branches: [ 'main', 'release-*' ]
pull_request:
types: [opened, synchronize, reopened]

jobs:

build:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
go-version:
- '1.18'
steps:

- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- name: Test
run: go run gotest.tools/[email protected] --format testname --
-race -count=1 -short ./...
env:
FORCE_COLOR: true
10 changes: 10 additions & 0 deletions .github/workflows/knative-style.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: Code Style

on:
pull_request:
branches: [ 'main', 'release-*' ]

jobs:

style:
uses: knative/actions/.github/workflows/style.yaml@main
36 changes: 36 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
run:
timeout: 5m
build-tags:
- e2e

linters:
disable-all: false
presets:
- bugs
- unused
- complexity
- format
- performance
- style
enable:
- gci
disable:
- paralleltest
- nlreturn
- exhaustivestruct
- wsl
- godox
- scopelint
- maligned
- interfacer
- golint
- ireturn
- varnamelen
- gochecknoglobals

issues:
exclude-rules:
- path: _test\.go
linters:
- wrapcheck

13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# go-retcode
Deterministic process exit codes
# retcode package for Go

Deterministic process exit codes based on Go errors.

## Usage

```go
err := fmt.Errorf("example error")
os.Exit(retcode.Calc(err))
```

3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/wavesoftware/go-retcode

go 1.18
21 changes: 21 additions & 0 deletions retcode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package retcode

import "hash/crc32"

var (
// LowerBound is the lower bound of the POSIX retcode range. Use this to
// configure the package.
LowerBound = 1
// UpperBound is the upper bound of the POSIX retcode range. Use this to
// configure the package.
UpperBound = 255
)

// Calc will calculate an POSIX retcode from an error.
func Calc(err error) int {
if err == nil {
return 0
}
upper := UpperBound - LowerBound
return int(crc32.ChecksumIEEE([]byte(err.Error())))%upper + LowerBound
}
44 changes: 44 additions & 0 deletions retcode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package retcode_test

import (
"fmt"
"testing"

"github.com/wavesoftware/go-retcode"
)

func TestCalc(t *testing.T) {
cases := testCases()
for i := range cases {
tt := cases[i]
t.Run(tt.name, func(t *testing.T) {
if got := retcode.Calc(tt.err); got != tt.want {
t.Errorf("Calc() = %v, want %v", got, tt.want)
}
})
}
}

func testCases() []testCase {
return []testCase{{
name: "nil",
err: nil,
want: 0,
}, {
name: "errExample",
err: errExample,
want: 133,
}, {
name: "error of wrap caused by 12345",
err: fmt.Errorf("%w: 12345", errExample),
want: 249,
}}
}

var errExample = fmt.Errorf("example error")

type testCase struct {
name string
err error
want int
}

0 comments on commit 6553dd3

Please sign in to comment.