Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
devfacet committed Apr 14, 2022
0 parents commit b5770a0
Show file tree
Hide file tree
Showing 15 changed files with 1,182 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Release

permissions:
contents: write

on:
push:
tags: [ 'v*.*.*' ]

workflow_dispatch:
inputs:
releaseTag:
description: 'Existing git tag to be released (i.e. v1.0.0)'
required: true

jobs:

test:
runs-on: ubuntu-latest
steps:
- name: Setup Go environment
uses: actions/setup-go@v2
with:
go-version: '1.17'

- name: Checkout code
uses: actions/checkout@v2
with:
# https://github.com/actions/checkout/issues/100
fetch-depth: 0

- name: Install test tools
run: |
make test-tools
- name: Run Tests
run: |
make test
release:
runs-on: ubuntu-latest
needs: test
outputs:
RELEASE_TAG: ${{ steps.env.outputs.RELEASE_TAG }}
steps:
- name: Setup ENV
id: env
run: |
if [[ "${GITHUB_EVENT_NAME}" == "workflow_dispatch" ]]; then
RELEASE_TAG=${{ github.event.inputs.releaseTag }}
elif [[ "${GITHUB_REF}" == refs/tags/v*.*.* ]]; then
RELEASE_TAG=${GITHUB_REF/refs\/tags\//}
fi
if [[ "${RELEASE_TAG}" != v*.*.* ]]; then
echo "invalid release tag (${RELEASE_TAG} - ${GITHUB_REF}), only semver is allowed (i.e v*.*.*)"
exit 1
fi
echo "::set-output name=RELEASE_TAG::${RELEASE_TAG}"
- name: Checkout code
uses: actions/checkout@v2

- name: Release
uses: softprops/[email protected]
with:
tag_name: ${{ steps.env.outputs.RELEASE_TAG }}
46 changes: 46 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Test

on:
push:
branches: [ '*' ]
tags-ignore: [ '*' ]
pull_request:
branches: [ '*' ]

workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
default: 'info'
type: choice
options:
- debug
- error
- fatal
- info
- panic
- warning

jobs:

test:
runs-on: ubuntu-latest
steps:
- name: Setup Go environment
uses: actions/setup-go@v2
with:
go-version: '1.17'

- name: Checkout code
uses: actions/checkout@v2
with:
# https://github.com/actions/checkout/issues/100
fetch-depth: 0

- name: Install test tools
run: |
make test-tools
- name: Run Tests
run: |
make test
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.DS_Store
.vscode
.vs
Thumbs.db

*.exe
*.log
*.out
*.prof
*.so

logs/
node_modules/
reports/
releases/
tmp/
vendor/
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Contributing

- Code contributions must be through pull requests.
- Run tests, linting and formatting before make a pull request.
- Pull requests can not be merged without being reviewed.
- Use "Issues" for bug reports, feature requests, discussions and typos.
- Do not refactor existing code without a discussion.
- Do not add a new third party dependency without a discussion.
- Use semantic versioning and git tags for versioning.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 Fatih Cetinkaya (https://github.com/devfacet/byteman)

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.
96 changes: 96 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# Init vars.
MAKEFILE := $(lastword $(MAKEFILE_LIST))
BASENAME := $(shell basename "$(PWD)")

.PHONY: help
all: help
help: Makefile
@echo
@echo " Commands:"
@echo
@sed -n 's/^##//p' $< | sed -e 's/^/ /' | sort
@echo

## test Run gofmt, golint, staticcheck, go vet and go test.
test:
$(eval FMT=$(shell find . -type f -name '*.go' | grep -v -E '^./vendor|^./third_party' | xargs -L1 dirname | uniq | xargs gofmt -l | wc -l | tr -d ' '))
@if [ "$(FMT)" != "0" ]; then \
echo "some files are not formatted, run 'make fmt'"; \
exit 1; \
fi

$(eval LINT=$(shell find . -type f -name '*.go' | grep -v -E '^./vendor|^./third_party' | xargs -L1 dirname | uniq | xargs golint | wc -l | tr -d ' '))
@if [ "$(LINT)" != "0" ]; then \
echo "some files have linting errors, run 'make lint'"; \
exit 1; \
fi

$(eval STATICCHECK=$(shell find . -type f -name '*.go' | grep -v -E '^./vendor|^./third_party' | xargs -L1 dirname | uniq | xargs staticcheck | wc -l | tr -d ' '))
@if [ "$(STATICCHECK)" != "0" ]; then \
echo "some files have staticcheck errors, run 'make staticcheck'"; \
exit 1; \
fi

$(eval GOVET=$(shell find . -type f -name '*.go' | grep -v -E '^./vendor' | xargs -L1 dirname | uniq | xargs go vet 2>&1 | wc -l | tr -d ' '))
@if [ "$(GOVET)" != "0" ]; then \
echo "some files have vetting errors, run 'make vet'"; \
exit 1; \
fi

@$(MAKE) -f $(MAKEFILE) test-go

## test-go Run go test
test-go:
@find . -type f -name '*.go' | grep -v -E '^./vendor|^./third_party' | xargs -L1 dirname | uniq | xargs go test -v -race

## test-benchmarks Run go benchmarks
test-benchmarks:
@find . -type f -name '*.go' | grep -v -E '^./vendor|^./third_party' | xargs -L1 dirname | uniq | xargs go test -benchmem -bench

## test-ui Launch test UI
test-ui:
$(eval GOCONVEY_PATH=$(shell which goconvey))
@if [ -z "$(GOCONVEY_PATH)" ]; then \
GO111MODULE=off go get github.com/smartystreets/goconvey; \
fi
goconvey -port 8088 -excludedDirs vendor,node_modules,assets

## test-clean Clean test cache
test-clean:
@go clean -testcache

## test-tools Install test tools
test-tools:
$(eval GOLINT_PATH=$(shell which golint))
@if [ -z "$(GOLINT_PATH)" ]; then \
GO111MODULE=off go get golang.org/x/lint/golint; \
fi
$(eval STATICCHECK_PATH=$(shell which staticcheck))
@if [ -z "$(STATICCHECK_PATH)" ]; then \
GO111MODULE=off go get honnef.co/go/tools/cmd/staticcheck; \
fi

## fmt Run formating
fmt:
@find . -type f -name '*.go' | grep -v -E '^./vendor|^./third_party' | xargs -L1 dirname | uniq | xargs gofmt -l

## lint Run linting
lint:
@find . -type f -name '*.go' | grep -v -E '^./vendor|^./third_party' | xargs -L1 dirname | uniq | xargs golint

## staticcheck Run staticcheck
staticcheck:
@find . -type f -name '*.go' | grep -v -E '^./vendor|^./third_party' | xargs -L1 dirname | uniq | xargs staticcheck

## vet Run vetting
vet:
@find . -type f -name '*.go' | grep -v -E '^./vendor' | xargs -L1 dirname | uniq | xargs go vet 2>&1

## release Release a version
release:
@if [ "$(shell echo \$${GIT_TAG:0:1})" != "v" ]; then \
echo "invalid GIT_TAG (${GIT_TAG}). Try something like 'make release GIT_TAG=v1.0.0'"; \
exit 1; \
fi
git tag -a $(GIT_TAG) -m "$(GIT_TAG)"
git push --follow-tags
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Byteman

[![Godoc][doc-image]][doc-url] [![Release][release-image]][release-url] [![Build][build-image]][build-url]

A Golang library that provides functions for bytes and bits.

## Usage

See [byteman_test.go](byteman_test.go), [numbers_test.go](numbers_test.go) and [strings_test.go](strings_test.go).

## Test

```shell
# Test everything:
make test

# For BDD development:
# It will open a new browser window. Make sure:
# 1. There is no errors on the terminal window.
# 2. There is no other open GoConvey page.
make test-ui

# Benchmarks
make test-benchmarks
```

## Release

```shell
# Update and commit CHANGELOG.md first (i.e. git add CHANGELOG.md && git commit -m "v1.0.0").
# Set GIT_TAG using semver (i.e. GIT_TAG=v1.0.0)
make release GIT_TAG=
```

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md)

## License

Licensed under The MIT License (MIT)
For the full copyright and license information, please view the LICENSE.txt file.

[doc-url]: https://pkg.go.dev/github.com/devfacet/byteman
[doc-image]: https://pkg.go.dev/badge/github.com/devfacet/byteman

[release-url]: https://github.com/devfacet/byteman/releases/latest
[release-image]: https://img.shields.io/github/release/devfacet/byteman.svg?style=flat-square

[build-url]: https://github.com/devfacet/byteman/actions/workflows/test.yaml
[build-image]: https://github.com/devfacet/byteman/actions/workflows/test.yaml/badge.svg
32 changes: 32 additions & 0 deletions byteman.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Byteman
// For the full copyright and license information, please view the LICENSE.txt file.

// Package byteman provides functions for bytes and bits.
package byteman

// Combine combines the given byte slices.
func Combine(bs ...[]byte) []byte {
b := []byte{}
for _, v := range bs {
b = append(b, v...)
}
return b
}

// Resize resizes the given byte slice.
func Resize(b []byte, size int) []byte {
if size == 0 {
size = len(b)
} else if size < 0 {
if ns := len(b) + size; ns > 0 {
size = ns
} else {
size = 0
}
}
nb := make([]byte, size)
if b != nil {
copy(nb, b)
}
return nb
}
Loading

0 comments on commit b5770a0

Please sign in to comment.