diff --git a/.bumpversion.cfg b/.bumpversion.cfg new file mode 100644 index 0000000..af8294b --- /dev/null +++ b/.bumpversion.cfg @@ -0,0 +1,10 @@ +[bumpversion] +current_version = 0.0.1 +commit = True +tag = True + + + +[bumpversion:file:flatbson.go] +search = const Version \= "{current_version}" +replace = {new_version} \ No newline at end of file diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..dcbb798 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +tasks.py linguist-vendored \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c54d070 --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +# Ignore all +* + +# Unignore all with extensions +!*.* + +# Unignore all dirs +!*/ + +# Unignore bazel files without extension +!**/WORKSPACE +!**/BUILD + +# Unignore Dockerfile and other useful files +!**/Dockerfile +!**/.dockerignore +!**/.gitattributes +!**/LICENSE + +### Above combination will ignore all files without extension ### + +.DS_Store +.vscode/ +.idea/ +coverage* +*.exe +build/ \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..b80064b --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,10 @@ +- repo: git://github.com/dnephin/pre-commit-golang + rev: master + hooks: + - id: go-fmt + - id: go-vet + - id: go-lint + - id: go-imports + - id: go-cyclo + args: [-over=15] + - id: go-build \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..8ab57e1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,30 @@ +dist: bionic +language: go +go: + - 1.13.x + - master + - tip +os: + - linux + - osx +env: MOD=github.com/romnnn/flatbson BINARY=flatbson + +jobs: + allow_failures: + - go: tip + fast_finish: true + +install: true +notifications: + email: false +before_script: + - pip install -U pip && pip install pre-commit invoke ruamel.yaml + - go get -u golang.org/x/lint/golint + - go get github.com/fzipp/gocyclo + - go get github.com/mitchellh/gox +script: + - invoke pre-commit + - env GO111MODULE=on go build ${MOD} + - env GO111MODULE=on go test -v -race -coverprofile=coverage.txt -coverpkg=all -covermode=atomic ./... +after_success: + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..caafc68 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020, romnnn + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..4519a8b --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +## flatbson + +[![Build Status](https://travis-ci.com/romnnn/flatbson.svg?branch=master)](https://travis-ci.com/romnnn/flatbson) +[![GitHub](https://img.shields.io/github/license/romnnn/flatbson)](https://github.com/romnnn/flatbson) +[![GoDoc](https://godoc.org/github.com/romnnn/flatbson?status.svg)](https://godoc.org/github.com/romnnn/flatbson) [![Test Coverage](https://codecov.io/gh/romnnn/flatbson/branch/master/graph/badge.svg)](https://codecov.io/gh/romnnn/flatbson) +[![Release](https://img.shields.io/github/release/romnnn/flatbson)](https://github.com/romnnn/flatbson/releases/latest) + +Your description goes here... + + + +#### Usage as a library + +```golang +import "github.com/romnnn/flatbson" +``` + +For more examples, see `examples/`. + + +#### Development + +###### Prerequisites + +Before you get started, make sure you have installed the following tools:: + + $ python3 -m pip install -U cookiecutter>=1.4.0 + $ python3 -m pip install pre-commit bump2version invoke ruamel.yaml halo + $ go get -u golang.org/x/tools/cmd/goimports + $ go get -u golang.org/x/lint/golint + $ go get -u github.com/fzipp/gocyclo + $ go get -u github.com/mitchellh/gox # if you want to test building on different architectures + +**Remember**: To be able to excecute the tools downloaded with `go get`, +make sure to include `$GOPATH/bin` in your `$PATH`. +If `echo $GOPATH` does not give you a path make sure to run +(`export GOPATH="$HOME/go"` to set it). In order for your changes to persist, +do not forget to add these to your shells `.bashrc`. + +With the tools in place, it is strongly advised to install the git commit hooks to make sure checks are passing in CI: +```bash +invoke install-hooks +``` + +You can check if all checks pass at any time: +```bash +invoke pre-commit +``` + +Note for Maintainers: After merging changes, tag your commits with a new version and push to GitHub to create a release: +```bash +bump2version (major | minor | patch) +git push --follow-tags +``` + +#### Note + +This project is still in the alpha stage and should not be considered production ready. diff --git a/examples/example1/example1.go b/examples/example1/example1.go new file mode 100644 index 0000000..5514f4e --- /dev/null +++ b/examples/example1/example1.go @@ -0,0 +1,15 @@ +package main + +import ( + "fmt" + + "github.com/romnnn/flatbson" +) + +func run() string { + return flatbson.Shout("This is an example") +} + +func main() { + fmt.Println(run()) +} diff --git a/examples/example1/example1_test.go b/examples/example1/example1_test.go new file mode 100644 index 0000000..504e383 --- /dev/null +++ b/examples/example1/example1_test.go @@ -0,0 +1,13 @@ +package main + +import ( + "testing" +) + +func TestCli(t *testing.T) { + out := run() + expected := "This is an example!" + if out != expected { + t.Errorf("Got %s but expected %s", out, expected) + } +} diff --git a/flatbson.go b/flatbson.go new file mode 100644 index 0000000..a112671 --- /dev/null +++ b/flatbson.go @@ -0,0 +1,9 @@ +package flatbson + +// Version is incremented using bump2version +const Version = "0.0.1" + +// Shout returns the input message with an exclamation mark +func Shout(s string) string { + return s + "!" +} diff --git a/flatbson_test.go b/flatbson_test.go new file mode 100644 index 0000000..f7149f7 --- /dev/null +++ b/flatbson_test.go @@ -0,0 +1,12 @@ +package flatbson + +import "testing" + +func TestShout(t *testing.T) { + if Shout("Test") != "Test!" { + t.Errorf("Got %s but want \"Test!\"", Shout("Test")) + } + if Shout("") != "!" { + t.Errorf("Got %s but want \"!\"", Shout("")) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c29fb7b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/romnnn/flatbson + +go 1.13 diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..615d768 --- /dev/null +++ b/tasks.py @@ -0,0 +1,218 @@ +""" +Tasks for maintaining the project. +Execute 'invoke --list' for guidance on using Invoke +""" +import shutil +import pprint +import sys +import os + +from invoke import task +import webbrowser + +PKG = "github.com/romnnn/flatbson" +CMD_PKG = PKG + + + +ROOT_DIR = os.path.dirname(os.path.realpath(__file__)) +BUILD_DIR = os.path.join(ROOT_DIR, "build") +TRAVIS_CONFIG_FILE = os.path.join(ROOT_DIR, ".travis.yml") + + +def _delete_file(file): + try: + file.unlink(missing_ok=True) + except TypeError: + # missing_ok argument added in 3.8 + try: + file.unlink() + except FileNotFoundError: + pass + + +@task +def format(c): + """Format code + """ + c.run("pre-commit run go-fmt --all-files") + c.run("pre-commit run go-imports --all-files") + + +@task +def test(c): + """Run tests + """ + c.run("env GO111MODULE=on go test -v -race ./...") + + +@task +def cyclo(c): + """Check code complexity + """ + c.run("pre-commit run go-cyclo --all-files") + + +@task +def lint(c): + """Lint code + """ + c.run("pre-commit run go-lint --all-files") + c.run("pre-commit run go-vet --all-files") + + +@task +def install_hooks(c): + """Install pre-commit hooks + """ + c.run("pre-commit install") + + +@task +def pre_commit(c): + """Run all pre-commit checks + """ + c.run("pre-commit run --all-files") + + +@task(help=dict(publish="Publish the coverage result to codecov.io (default False)",),) +def coverage(c, publish=False): + """Create coverage report + """ + c.run( + "env GO111MODULE=on go test -v -race -coverprofile=coverage.txt -coverpkg=all -covermode=atomic ./..." + ) + if publish: + # Publish the results via codecov + c.run("bash <(curl -s https://codecov.io/bash)") + + + + + +@task +def build(c): + """Build the project + """ + c.run("pre-commit run go-build --all-files") + + + + + +@task +def clean_build(c): + """Clean up files from package building + """ + c.run("rm -fr build/") + + +@task +def clean_coverage(c): + """Clean up files from coverage measurement + """ + c.run("find . -name 'coverage.txt' -exec rm -fr {} +") + + +@task(pre=[clean_build, clean_coverage]) +def clean(c): + """Runs all clean sub-tasks + """ + pass + + +def _create(d, *keys): + current = d + for key in keys: + try: + current = current[key] + except (TypeError, KeyError): + current[key] = dict() + current = current[key] + + +def _fix_token(config_file=None, force=False, verify=True): + from ruamel.yaml import YAML + yaml = YAML() + config_file = config_file or TRAVIS_CONFIG_FILE + with open(config_file, "r") as _file: + try: + travis_config = yaml.load(_file) + except Exception: + raise ValueError( + "Failed to parse the travis configuration. " + "Make sure the config only contains valid YAML and keys as specified by travis." + ) + + # Get the generated token from the top level deploy config added by the travis cli + try: + real_token = travis_config["deploy"]["api_key"]["secure"] + except (TypeError, KeyError): + raise AssertionError("Can't find any top level deployment tokens") + + try: + # Find the build stage that deploys to releases + releases_stages = [ + stage + for stage in travis_config["jobs"]["include"] + if stage.get("deploy", dict()).get("provider") == "releases" + ] + assert ( + len(releases_stages) > 0 + ), "Can't set the new token because there are no stages deploying to releases" + assert ( + len(releases_stages) < 2 + ), "Can't set the new token because there are multiple stages deploying to releases" + except (TypeError, KeyError): + raise AssertionError( + "Can't set the new token because there are no deployment stages") + + try: + is_mock_token = releases_stages[0]["deploy"]["token"]["secure"] == "REPLACE_ME" + is_same_token = releases_stages[0]["deploy"]["token"]["secure"] == real_token + + unmodified = is_mock_token or is_same_token + except (TypeError, KeyError): + unmodified = False + + # Set the new generated token as the stages deploy token + _create(releases_stages[0], "deploy", "token", "secure") + releases_stages[0]["deploy"]["token"]["secure"] = real_token + + # Make sure it is fine to overwrite the config file + assert unmodified or force, ( + 'The secure token in the "{}" stage has already been changed. ' + "Retry with --force if you are sure about replacing it.".format( + releases_stages[0].get("stage", "releases deployment") + ) + ) + + # Remove the top level deploy config added by the travis cli + travis_config.pop("deploy") + + if not unmodified and verify: + pprint.pprint(travis_config) + if ( + not input("Do you want to save this configuration? (y/n) ") + .strip() + .lower() + == "y" + ): + return + + # Save the new travis config + assert travis_config + with open(config_file, "w") as _file: + yaml.dump(travis_config, _file) + print("Fixed!") + + +@task(help=dict( + force="Force overriding the current travis configuration", + verify="Verify config changes by asking for the user's approval" +)) +def fix_token(c, force=False, verify=True): + """ + Add the token generated by the travis cli script to the correct entry + """ + _fix_token(force=force, verify=verify)