feat: init

This commit is contained in:
Reinaldy Rafli 2021-10-27 21:59:57 +07:00
commit 76eb472c91
No known key found for this signature in database
GPG Key ID: CFDB9400255D8CB6
8 changed files with 293 additions and 0 deletions

92
.github/COMMIT_CONVENTION.md vendored Normal file
View File

@ -0,0 +1,92 @@
> [conventional-changelog](https://github.com/ajoslin/conventional-changelog) [angular](https://github.com/angular/angular) preset
## Angular Convention
Angular's [commit message guidelines](https://github.com/angular/angular/blob/master/CONTRIBUTING.md#commit).
### Examples
Appears under "Features" header, pencil subheader:
```xml
feat(pencil): add 'graphiteWidth' option
```
Appears under "Bug Fixes" header, graphite subheader, with a link to issue #28:
```xml
fix(graphite): stop graphite breaking when width < 0.1
Closes #28
```
Appears under "Performance Improvements" header, and under "Breaking Changes" with the breaking change explanation:
```xml
perf(pencil): remove graphiteWidth option
BREAKING CHANGE: The graphiteWidth option has been removed. The default graphite width of 10mm is always used for performance reason.
```
The following commit and commit `667ecc1` do not appear in the changelog if they are under the same release. If not, the revert commit appears under the "Reverts" header.
```xml
revert: feat(pencil): add 'graphiteWidth' option
This reverts commit 667ecc1654a317a13331b17617d973392f415f02.
```
### Commit Message Format
A commit message consists of a **header**, **body** and **footer**. The header has a **type**, **scope** and **subject**:
```xml
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
```
The **header** is mandatory and the **scope** of the header is optional.
### Revert
If the commit reverts a previous commit, it should begin with `revert: `, followed by the header of the reverted commit. In the body it should say: `This reverts commit <hash>.`, where the hash is the SHA of the commit being reverted.
### Type
If the prefix is `feat`, `fix` or `perf`, it will appear in the changelog. However if there is any [BREAKING CHANGE](#footer), the commit will always appear in the changelog.
Other prefixes are up to your discretion. Suggested prefixes are `build`, `ci`, `docs` ,`style`, `refactor`, and `test` for non-changelog related tasks.
Details regarding these types can be found in the official [Angular Contributing Guidelines](https://github.com/angular/angular/blob/master/CONTRIBUTING.md#type).
### Scope
The scope could be anything specifying place of the commit change. For example `$location`,
`$browser`, `$compile`, `$rootScope`, `ngHref`, `ngClick`, `ngView`, etc...
### Subject
The subject contains succinct description of the change:
* use the imperative, present tense: "change" not "changed" nor "changes"
* don't capitalize first letter
* no dot (.) at the end
### Body
Just as in the **subject**, use the imperative, present tense: "change" not "changed" nor "changes".
The body should include the motivation for the change and contrast this with previous behavior.
### Footer
The footer should contain any information about **Breaking Changes** and is also the place to
reference GitHub issues that this commit **Closes**.
**Breaking Changes** should start with the word `BREAKING CHANGE:` with a space or two newlines. The rest of the commit message is then used for this.
A detailed explanation can be found in this [document][commit-message-format].
[commit-message-format]: https://docs.google.com/document/d/1QrDFcIiPjSLDn3EL15IJygNPiHORgU1_OOAqWjiDU5Y/edit#

40
.github/workflows/ci.yml vendored Normal file
View File

@ -0,0 +1,40 @@
name: CI
on: [push, pull_request]
jobs:
build-test:
name: Build test
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [1.16.x, 1.17.x]
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Build
run: go build .
coverage:
name: Coverage
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.17.x
- name: Run coverage
run: go test -v -race -coverprofile=coverage.out -covermode=atomic -failfast ./...
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1

19
.gitignore vendored Normal file
View File

@ -0,0 +1,19 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
### Go Patch ###
/vendor/
/Godeps/

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 Reinaldy Rafli <aldy505@tutanota.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

37
README.md Normal file
View File

@ -0,0 +1,37 @@
# Decrr
[![Go Reference](https://pkg.go.dev/badge/github.com/aldy505/decrr.svg)](https://pkg.go.dev/github.com/aldy505/decrr) [![Go Report Card](https://goreportcard.com/badge/github.com/aldy505/decrr)](https://goreportcard.com/report/github.com/aldy505/decrr) ![GitHub](https://img.shields.io/github/license/aldy505/decrr) [![CodeFactor](https://www.codefactor.io/repository/github/aldy505/decrr/badge)](https://www.codefactor.io/repository/github/aldy505/decrr) [![codecov](https://codecov.io/gh/aldy505/decrr/branch/master/graph/badge.svg?token=Noeexg5xEJ)](https://codecov.io/gh/aldy505/decrr) [![Codacy Badge](https://app.codacy.com/project/badge/Grade/9b78970127c74c1a923533e05f65848d)](https://www.codacy.com/gh/aldy505/decrr/dashboard?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=aldy505/decrr&amp;utm_campaign=Badge_Grade) [![Test](https://github.com/aldy505/decrr/actions/workflows/ci.yml/badge.svg)](https://github.com/aldy505/decrr/actions/workflows/ci.yml)
A modification (and a bit of simplification) of the [tracerr](https://github.com/ztrue/tracerr) package.
This essentially does pretty much the same, but instead of returning another struct like tracerr package does, this decorate the error and return another regular error type.
## Usage
Simply put it on the deepest function/method on your project.
```go
package main
import "github.com/aldy505/decrr"
func main() {
err := ExecuteSomething()
log.Fatal(err)
}
func ExecuteSomething() error {
return SomethingElse()
}
func SomethingElse() error {
err := errors.New("a goat just passes by!")
return decrr.Wrap(err)
}
```
And yes, it only has one `.Wrap(error)` function. Nothing else.
## License
[MIT License](./LICENSE)

56
decrr.go Normal file
View File

@ -0,0 +1,56 @@
package decrr
import (
"errors"
"runtime"
"strconv"
"strings"
)
type frame struct {
Func string
Path string
Line int
}
// Decorates a normal error and fill it with a stack trace.
// A modification of tracerr package.
func Wrap(err error) error {
if err == nil {
return nil
}
traced := trace(err)
var stack strings.Builder
for _, v := range traced {
stack.WriteString("\n")
stack.WriteString(v.Func)
stack.WriteString(" ")
stack.WriteString(v.Path)
stack.WriteString(":")
stack.WriteString(strconv.Itoa(v.Line))
}
return errors.New(err.Error() + "\n" + stack.String())
}
func trace(err error) []frame {
skip := 2
frames := make([]frame, 0, 20)
for {
pc, path, line, ok := runtime.Caller(skip)
if !ok {
break
}
fn := runtime.FuncForPC(pc)
frame := frame{
Func: fn.Name(),
Line: line,
Path: path,
}
frames = append(frames, frame)
skip++
}
return frames
}

25
decrr_test.go Normal file
View File

@ -0,0 +1,25 @@
package decrr_test
import (
"errors"
"os"
"testing"
"github.com/aldy505/decrr"
)
func TestWrap(t *testing.T) {
x := errors.New("why does a chicken cross the road?")
w := decrr.Wrap(x)
wd, _ := os.Getwd()
var expected string
expected += "why does a chicken cross the road?\n\n"
expected += "github.com/aldy505/decrr_test.TestWrap " + wd + "/decrr_test.go:13\n"
expected += "testing.tRunner /usr/local/go/src/testing/testing.go:1259\n"
expected += "runtime.goexit /usr/local/go/src/runtime/asm_amd64.s:1581"
if w.Error() != expected {
t.Error("different than expected:", w.Error(), "\nexpected:", expected)
}
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module github.com/aldy505/decrr
go 1.17