From d969f952af9e02feea59963671298fc236ca4399 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Fri, 7 May 2021 16:27:37 +0000 Subject: [PATCH] feat: initial implementation Refs https://github.com/talos-systems/talos/issues/3534 Signed-off-by: Alexey Palazhchenko --- .codecov.yml | 18 +++ .conform.yaml | 35 ++++++ .dockerignore | 15 +++ .drone.yml | 194 +++++++++++++++++++++++++++++++ .gitignore | 5 + .golangci.yml | 150 ++++++++++++++++++++++++ .markdownlint.json | 9 ++ Dockerfile | 75 ++++++++++++ Makefile | 135 +++++++++++++++++++++ README.md | 6 +- debug.go | 86 ++++++++++++++ debug_off.go | 10 ++ debug_off_test.go | 18 +++ debug_on.go | 19 +++ debug_on_test.go | 27 +++++ go.mod | 8 ++ go.sum | 19 +++ hack/git-chglog/CHANGELOG.tpl.md | 26 +++++ hack/git-chglog/config.yaml | 32 +++++ hack/release.sh | 60 ++++++++++ 20 files changed, 946 insertions(+), 1 deletion(-) create mode 100644 .codecov.yml create mode 100644 .conform.yaml create mode 100644 .dockerignore create mode 100644 .drone.yml create mode 100644 .gitignore create mode 100644 .golangci.yml create mode 100644 .markdownlint.json create mode 100644 Dockerfile create mode 100644 Makefile create mode 100644 debug.go create mode 100644 debug_off.go create mode 100644 debug_off_test.go create mode 100644 debug_on.go create mode 100644 debug_on_test.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 hack/git-chglog/CHANGELOG.tpl.md create mode 100644 hack/git-chglog/config.yaml create mode 100755 hack/release.sh diff --git a/.codecov.yml b/.codecov.yml new file mode 100644 index 0000000..fbdfc5b --- /dev/null +++ b/.codecov.yml @@ -0,0 +1,18 @@ +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + +codecov: + require_ci_to_pass: false + +coverage: + status: + project: + default: + target: 50% + threshold: 0.5% + base: auto + if_ci_failed: success + patch: off + +comment: false diff --git a/.conform.yaml b/.conform.yaml new file mode 100644 index 0000000..141b82b --- /dev/null +++ b/.conform.yaml @@ -0,0 +1,35 @@ +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + +policies: +- type: commit + spec: + dco: true + gpg: false + spellcheck: + locale: US + maximumOfOneCommit: true + header: + length: 89 + imperative: true + case: lower + invalidLastCharacters: . + body: + required: true + conventional: + types: ["chore","docs","perf","refactor","style","test","release"] + scopes: ["*"] +- type: license + spec: + skipPaths: + - .git/ + includeSuffixes: + - .go + excludeSuffixes: + - .pb.go + - .pb.gw.go + header: | + // This Source Code Form is subject to the terms of the Mozilla Public + // License, v. 2.0. If a copy of the MPL was not distributed with this + // file, You can obtain one at http://mozilla.org/MPL/2.0/. diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..742f0f3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + +** +!debug.go +!debug_off.go +!debug_off_test.go +!debug_on.go +!debug_on_test.go +!go.mod +!go.sum +!.golangci.yml +!README.md +!.markdownlint.json diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..6772e54 --- /dev/null +++ b/.drone.yml @@ -0,0 +1,194 @@ +--- +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + +kind: pipeline +type: kubernetes +name: default + +platform: + os: linux + arch: amd64 + +steps: +- name: setup-ci + pull: always + image: autonomy/build-container:latest + commands: + - sleep 5 + - git fetch --tags + - install-ci-key + - docker buildx create --driver docker-container --platform linux/amd64 --name local --use unix:///var/outer-run/docker.sock + - docker buildx inspect --bootstrap + environment: + SSH_KEY: + from_secret: ssh_key + volumes: + - name: outer-docker-socket + path: /var/outer-run + - name: docker-socket + path: /var/run + - name: buildx + path: /root/.docker/buildx + - name: ssh + path: /root/.ssh + +- name: base + pull: always + image: autonomy/build-container:latest + commands: + - make base + volumes: + - name: outer-docker-socket + path: /var/outer-run + - name: docker-socket + path: /var/run + - name: buildx + path: /root/.docker/buildx + - name: ssh + path: /root/.ssh + depends_on: + - setup-ci + +- name: unit-tests + pull: always + image: autonomy/build-container:latest + commands: + - make unit-tests + volumes: + - name: outer-docker-socket + path: /var/outer-run + - name: docker-socket + path: /var/run + - name: buildx + path: /root/.docker/buildx + - name: ssh + path: /root/.ssh + depends_on: + - base + +- name: unit-tests-race + pull: always + image: autonomy/build-container:latest + commands: + - make unit-tests-race + volumes: + - name: outer-docker-socket + path: /var/outer-run + - name: docker-socket + path: /var/run + - name: buildx + path: /root/.docker/buildx + - name: ssh + path: /root/.ssh + depends_on: + - base + +- name: coverage + pull: always + image: autonomy/build-container:latest + commands: + - make coverage + environment: + CODECOV_TOKEN: + from_secret: CODECOV_TOKEN + volumes: + - name: outer-docker-socket + path: /var/outer-run + - name: docker-socket + path: /var/run + - name: buildx + path: /root/.docker/buildx + - name: ssh + path: /root/.ssh + depends_on: + - unit-tests + +- name: lint + pull: always + image: autonomy/build-container:latest + commands: + - make lint + volumes: + - name: outer-docker-socket + path: /var/outer-run + - name: docker-socket + path: /var/run + - name: buildx + path: /root/.docker/buildx + - name: ssh + path: /root/.ssh + depends_on: + - base + +services: +- name: docker + image: docker:19.03-dind + entrypoint: + - dockerd + commands: + - --dns=8.8.8.8 + - --dns=8.8.4.4 + - --mtu=1500 + - --log-level=error + privileged: true + volumes: + - name: outer-docker-socket + path: /var/outer-run + - name: docker-socket + path: /var/run + - name: buildx + path: /root/.docker/buildx + - name: ssh + path: /root/.ssh + +volumes: +- name: outer-docker-socket + host: + path: /var/ci-docker +- name: docker-socket + temp: + medium: memory +- name: buildx + temp: + medium: memory +- name: ssh + temp: + medium: memory + +--- +kind: pipeline +type: kubernetes +name: notify + +platform: + os: linux + arch: amd64 + +clone: + disable: true + +steps: +- name: slack + image: plugins/slack + settings: + channel: proj-talos-maintainers + link_names: true + template: "{{#if build.pull }}\n*{{#success build.status}}✓ Success{{else}}✕ Fail{{/success}}*: {{ repo.owner }}/{{ repo.name }} - \n{{else}}\n*{{#success build.status}}✓ Success{{else}}✕ Fail{{/success}}: {{ repo.owner }}/{{ repo.name }} - Build #{{ build.number }}* (type: `{{ build.event }}`)\n{{/if}}\nCommit: \nBranch: \nAuthor: {{ build.author }}\n<{{ build.link }}|Visit build page>" + webhook: + from_secret: slack_webhook + when: + status: + - success + - failure + +trigger: + status: + - success + - failure + +depends_on: +- default + +... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dca20fa --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + +_out diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..955d72d --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,150 @@ +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + + +# options for analysis running +run: + timeout: 10m + issues-exit-code: 1 + tests: true + build-tags: [] + skip-dirs: [] + skip-dirs-use-default: true + skip-files: [] + modules-download-mode: readonly + +# output configuration options +output: + format: colored-line-number + print-issued-lines: true + print-linter-name: true + uniq-by-line: true + path-prefix: "" + + +# all available settings of specific linters +linters-settings: + dogsled: + max-blank-identifiers: 2 + dupl: + threshold: 150 + errcheck: + check-type-assertions: true + check-blank: true + exhaustive: + default-signifies-exhaustive: false + funlen: + lines: 60 + statements: 40 + gci: + local-prefixes: github.com/talos-systems/go-debug + gocognit: + min-complexity: 30 + nestif: + min-complexity: 5 + goconst: + min-len: 3 + min-occurrences: 3 + gocritic: + disabled-checks: [] + gocyclo: + min-complexity: 20 + godot: + check-all: false + godox: + keywords: # default keywords are TODO, BUG, and FIXME, these can be overwritten by this setting + - NOTE + - OPTIMIZE # marks code that should be optimized before merging + - HACK # marks hack-arounds that should be removed before merging + gofmt: + simplify: true + goimports: + local-prefixes: github.com/talos-systems/go-debug + golint: + min-confidence: 0.8 + gomnd: + settings: {} + gomodguard: {} + govet: + check-shadowing: true + enable-all: true + depguard: + list-type: blacklist + include-go-root: false + lll: + line-length: 200 + tab-width: 4 + misspell: + locale: US + ignore-words: [] + nakedret: + max-func-lines: 30 + prealloc: + simple: true + range-loops: true # Report preallocation suggestions on range loops, true by default + for-loops: false # Report preallocation suggestions on for loops, false by default + nolintlint: + allow-unused: false + allow-leading-space: false + allow-no-explanation: [] + require-explanation: false + require-specific: true + rowserrcheck: {} + testpackage: + unparam: + check-exported: false + unused: + check-exported: false + whitespace: + multi-if: false # Enforces newlines (or comments) after every multi-line if statement + multi-func: false # Enforces newlines (or comments) after every multi-line function signature + wsl: + strict-append: true + allow-assign-and-call: true + allow-multiline-assign: true + allow-cuddle-declarations: false + allow-trailing-comment: false + force-case-trailing-whitespace: 0 + force-err-cuddling: false + allow-separated-leading-comment: false + gofumpt: + extra-rules: false + cyclop: + # the maximal code complexity to report + max-complexity: 20 + +linters: + enable-all: true + disable: + - gas + - typecheck + - gochecknoglobals + - gochecknoinits + - funlen + - godox + - gomnd + - goerr113 + - nestif + - wrapcheck + - paralleltest + - exhaustivestruct + - forbidigo + disable-all: false + fast: false + + +issues: + exclude: [] + exclude-rules: [] + exclude-use-default: false + exclude-case-sensitive: false + max-issues-per-linter: 10 + max-same-issues: 3 + + new: false + +severity: + default-severity: error + + case-sensitive: false diff --git a/.markdownlint.json b/.markdownlint.json new file mode 100644 index 0000000..939fa22 --- /dev/null +++ b/.markdownlint.json @@ -0,0 +1,9 @@ +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + +{ + "MD013": false, + "MD033": false, + "default": true + } diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fd0aed8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,75 @@ +# syntax = docker/dockerfile-upstream:1.2.0-labs + +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + +ARG TOOLCHAIN + +# cleaned up specs and compiled versions +FROM scratch AS generate + +# runs markdownlint +FROM node:14.8.0-alpine AS lint-markdown +RUN npm i -g markdownlint-cli@0.23.2 +RUN npm i sentences-per-line@0.2.1 +WORKDIR /src +COPY .markdownlint.json . +COPY ./README.md ./README.md +RUN markdownlint --ignore "**/node_modules/**" --ignore '**/hack/chglog/**' --rules /node_modules/sentences-per-line/index.js . + +# base toolchain image +FROM ${TOOLCHAIN} AS toolchain +RUN apk --update --no-cache add bash curl build-base protoc protobuf-dev + +# build tools +FROM toolchain AS tools +ENV GO111MODULE on +ENV CGO_ENABLED 0 +ENV GOPATH /go +RUN curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b /bin v1.38.0 +ARG GOFUMPT_VERSION +RUN cd $(mktemp -d) \ + && go mod init tmp \ + && go get mvdan.cc/gofumpt/gofumports@${GOFUMPT_VERSION} \ + && mv /go/bin/gofumports /bin/gofumports + +# tools and sources +FROM tools AS base +WORKDIR /src +COPY ./go.mod . +COPY ./go.sum . +RUN --mount=type=cache,target=/go/pkg go mod download +RUN --mount=type=cache,target=/go/pkg go mod verify +COPY ./debug.go ./debug.go +COPY ./debug_off.go ./debug_off.go +COPY ./debug_off_test.go ./debug_off_test.go +COPY ./debug_on.go ./debug_on.go +COPY ./debug_on_test.go ./debug_on_test.go +RUN --mount=type=cache,target=/go/pkg go list -mod=readonly all >/dev/null + +# runs gofumpt +FROM base AS lint-gofumpt +RUN find . -name '*.pb.go' | xargs -r rm +RUN find . -name '*.pb.gw.go' | xargs -r rm +RUN FILES="$(gofumports -l -local github.com/talos-systems/go-debug .)" && test -z "${FILES}" || (echo -e "Source code is not formatted with 'gofumports -w -local github.com/talos-systems/go-debug .':\n${FILES}"; exit 1) + +# runs golangci-lint +FROM base AS lint-golangci-lint +COPY .golangci.yml . +ENV GOGC 50 +RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/root/.cache/golangci-lint --mount=type=cache,target=/go/pkg golangci-lint run --config .golangci.yml + +# runs unit-tests with race detector +FROM base AS unit-tests-race +ARG TESTPKGS +RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp CGO_ENABLED=1 go test -v -race -count 1 ${TESTPKGS} + +# runs unit-tests +FROM base AS unit-tests-run +ARG TESTPKGS +RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp go test -v -covermode=atomic -coverprofile=coverage.txt -coverpkg=${TESTPKGS} -count 1 ${TESTPKGS} + +FROM scratch AS unit-tests +COPY --from=unit-tests-run /src/coverage.txt /coverage.txt + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3875814 --- /dev/null +++ b/Makefile @@ -0,0 +1,135 @@ +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + +# common variables + +SHA := $(shell git describe --match=none --always --abbrev=8 --dirty) +TAG := $(shell git describe --tag --always --dirty) +BRANCH := $(shell git rev-parse --abbrev-ref HEAD) +ARTIFACTS := _out +REGISTRY ?= ghcr.io +USERNAME ?= talos-systems +REGISTRY_AND_USERNAME ?= $(REGISTRY)/$(USERNAME) +GOFUMPT_VERSION ?= abc0db2c416aca0f60ea33c23c76665f6e7ba0b6 +GO_VERSION ?= 1.14 +PROTOBUF_GO_VERSION ?= 1.25.0 +GRPC_GO_VERSION ?= 1.1.0 +GRPC_GATEWAY_VERSION ?= 2.4.0 +TESTPKGS ?= ./... +KRES_IMAGE ?= ghcr.io/talos-systems/kres:latest + +# docker build settings + +BUILD := docker buildx build +PLATFORM ?= linux/amd64 +PROGRESS ?= auto +PUSH ?= false +CI_ARGS ?= +COMMON_ARGS = --file=Dockerfile +COMMON_ARGS += --progress=$(PROGRESS) +COMMON_ARGS += --platform=$(PLATFORM) +COMMON_ARGS += --push=$(PUSH) +COMMON_ARGS += --build-arg=ARTIFACTS=$(ARTIFACTS) +COMMON_ARGS += --build-arg=SHA=$(SHA) +COMMON_ARGS += --build-arg=TAG=$(TAG) +COMMON_ARGS += --build-arg=USERNAME=$(USERNAME) +COMMON_ARGS += --build-arg=TOOLCHAIN=$(TOOLCHAIN) +COMMON_ARGS += --build-arg=GOFUMPT_VERSION=$(GOFUMPT_VERSION) +COMMON_ARGS += --build-arg=PROTOBUF_GO_VERSION=$(PROTOBUF_GO_VERSION) +COMMON_ARGS += --build-arg=GRPC_GO_VERSION=$(GRPC_GO_VERSION) +COMMON_ARGS += --build-arg=GRPC_GATEWAY_VERSION=$(GRPC_GATEWAY_VERSION) +COMMON_ARGS += --build-arg=TESTPKGS=$(TESTPKGS) +TOOLCHAIN ?= docker.io/golang:1.16-alpine + +# help menu + +export define HELP_MENU_HEADER +# Getting Started + +To build this project, you must have the following installed: + +- git +- make +- docker (19.03 or higher) + +## Creating a Builder Instance + +The build process makes use of experimental Docker features (buildx). +To enable experimental features, add 'experimental: "true"' to '/etc/docker/daemon.json' on +Linux or enable experimental features in Docker GUI for Windows or Mac. + +To create a builder instance, run: + + docker buildx create --name local --use + + +If you already have a compatible builder instance, you may use that instead. + +## Artifacts + +All artifacts will be output to ./$(ARTIFACTS). Images will be tagged with the +registry "$(REGISTRY)", username "$(USERNAME)", and a dynamic tag (e.g. $(IMAGE):$(TAG)). +The registry and username can be overridden by exporting REGISTRY, and USERNAME +respectively. + +endef + +all: unit-tests lint + +.PHONY: clean +clean: ## Cleans up all artifacts. + @rm -rf $(ARTIFACTS) + +target-%: ## Builds the specified target defined in the Dockerfile. The build result will only remain in the build cache. + @$(BUILD) --target=$* $(COMMON_ARGS) $(TARGET_ARGS) $(CI_ARGS) . + +local-%: ## Builds the specified target defined in the Dockerfile using the local output type. The build result will be output to the specified local destination. + @$(MAKE) target-$* TARGET_ARGS="--output=type=local,dest=$(DEST) $(TARGET_ARGS)" + +lint-golangci-lint: ## Runs golangci-lint linter. + @$(MAKE) target-$@ + +lint-gofumpt: ## Runs gofumpt linter. + @$(MAKE) target-$@ + +.PHONY: fmt +fmt: ## Formats the source code + @docker run --rm -it -v $(PWD):/src -w /src golang:$(GO_VERSION) \ + bash -c "export GO111MODULE=on; export GOPROXY=https://proxy.golang.org; \ + cd /tmp && go mod init tmp && go get mvdan.cc/gofumpt/gofumports@$(GOFUMPT_VERSION) && \ + cd - && gofumports -w -local github.com/talos-systems/go-debug ." + +.PHONY: base +base: ## Prepare base toolchain + @$(MAKE) target-$@ + +.PHONY: unit-tests +unit-tests: ## Performs unit tests + @$(MAKE) local-$@ DEST=$(ARTIFACTS) + +.PHONY: unit-tests-race +unit-tests-race: ## Performs unit tests with race detection enabled. + @$(MAKE) target-$@ + +.PHONY: coverage +coverage: ## Upload coverage data to codecov.io. + bash -c "bash <(curl -s https://codecov.io/bash) -f $(ARTIFACTS)/coverage.txt -X fix" + +.PHONY: lint-markdown +lint-markdown: ## Runs markdownlint. + @$(MAKE) target-$@ + +.PHONY: lint +lint: lint-golangci-lint lint-gofumpt lint-markdown ## Run all linters for the project. + +.PHONY: rekres +rekres: + @docker pull $(KRES_IMAGE) + @docker run --rm -v $(PWD):/src -w /src -e GITHUB_TOKEN $(KRES_IMAGE) + +.PHONY: help +help: ## This help menu. + @echo "$$HELP_MENU_HEADER" + @grep -E '^[a-zA-Z%_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' + diff --git a/README.md b/README.md index fdce786..b1093be 100644 --- a/README.md +++ b/README.md @@ -1 +1,5 @@ -# debug \ No newline at end of file +# go-debug + +[![Go Reference](https://pkg.go.dev/badge/github.com/talos-systems/go-debug.svg)](https://pkg.go.dev/github.com/talos-systems/go-debug) + +`go-debug` is a Sidero-specific library for including debugging facilities for developers in our products when they are compiled with `sidero.debug` build tag. They are not included by default. diff --git a/debug.go b/debug.go new file mode 100644 index 0000000..72f243c --- /dev/null +++ b/debug.go @@ -0,0 +1,86 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +// debug is a Sidero-specific library for including debugging facilities for developers in our products +// when they are compiled with sidero.debug build tag. They are not included by default. +package debug + +import ( + "context" + "fmt" + "net/http" + "reflect" + "runtime" + "sort" +) + +// LogFunc is a simplest logging function. +type LogFunc func(msg string) + +// ListenAndServe runs debug server on given address. +// +// If debugging is enabled (debug.Enabled is true), the function blocks until fatal error in encountered +// (it is returned) or ctx is canceled (in that case, nil is returned). +// +// If debugging is disabled (debug.Enabled is false), the function immediately returns nil. +// +// Both cases can be handled with the following code: +// +// if err := debug.ListenAndServe(ctx, addr, log); err != nil { +// log.Fatal(err) +// } +func ListenAndServe(ctx context.Context, addr string, log LogFunc) error { + if !Enabled { + return nil + } + + // that's defaults, just make them explicit + runtime.SetCPUProfileRate(100) + runtime.MemProfileRate = 512 * 1024 + + // https://github.com/DataDog/go-profiler-notes/blob/main/block.md#overhead + runtime.SetBlockProfileRate(10000) + + // no science behind that value + runtime.SetMutexProfileFraction(10000) + + log("starting debug server") + + for _, h := range handlers() { + log(fmt.Sprintf("http://%s%s", addr, h)) + } + + s := &http.Server{ + Addr: addr, + } + + // no real reason to do a graceful shutdown with s.Shutdown + go func() { + <-ctx.Done() + + _ = s.Close() + }() + + err := s.ListenAndServe() + if err == http.ErrServerClosed { + return nil + } + + return err +} + +// handlers returns all patterns registered on the default HTTP server mux. +// Useful to check for unexpected handlers. +func handlers() []string { + keys := reflect.ValueOf(http.DefaultServeMux).Elem().FieldByName("m").MapKeys() + + res := make([]string, len(keys)) + for i, v := range keys { + res[i] = v.String() + } + + sort.Strings(res) + + return res +} diff --git a/debug_off.go b/debug_off.go new file mode 100644 index 0000000..71f7ad5 --- /dev/null +++ b/debug_off.go @@ -0,0 +1,10 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +// +build !sidero.debug + +package debug + +// Enabled is false when compiled without sidero.debug build tag. +const Enabled = false diff --git a/debug_off_test.go b/debug_off_test.go new file mode 100644 index 0000000..f4da791 --- /dev/null +++ b/debug_off_test.go @@ -0,0 +1,18 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +// +build !sidero.debug + +package debug + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDebugOff(t *testing.T) { + expected := []string{} + assert.Equal(t, expected, handlers()) +} diff --git a/debug_on.go b/debug_on.go new file mode 100644 index 0000000..57f21b4 --- /dev/null +++ b/debug_on.go @@ -0,0 +1,19 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +// Protect with the build tag to avoid unwanted side-effects of imports below by default. +// +build sidero.debug + +package debug + +import ( + _ "expvar" // registers /debug/vars on http.DefaultServeMux + _ "net/http/pprof" // registers /debug/pprof on http.DefaultServeMux + + // imported by google.golang.org/grpc anyway + _ "golang.org/x/net/trace" // registers /debug/events and /debug/requests on http.DefaultServeMux +) + +// Enabled is true when compiled with sidero.debug build tag. +const Enabled = true diff --git a/debug_on_test.go b/debug_on_test.go new file mode 100644 index 0000000..d191579 --- /dev/null +++ b/debug_on_test.go @@ -0,0 +1,27 @@ +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +// +build sidero.debug + +package debug + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDebugOn(t *testing.T) { + expected := []string{ + "/debug/events", + "/debug/pprof/", + "/debug/pprof/cmdline", + "/debug/pprof/profile", + "/debug/pprof/symbol", + "/debug/pprof/trace", + "/debug/requests", + "/debug/vars", + } + assert.Equal(t, expected, handlers()) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bb9ddf3 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module github.com/talos-systems/go-debug + +go 1.16 + +require ( + github.com/stretchr/testify v1.7.0 + golang.org/x/net v0.0.0-20210505214959-0714010a04ed +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b36a387 --- /dev/null +++ b/go.sum @@ -0,0 +1,19 @@ +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/net v0.0.0-20210505214959-0714010a04ed h1:V9kAVxLvz1lkufatrpHuUVyJ/5tR3Ms7rk951P4mI98= +golang.org/x/net v0.0.0-20210505214959-0714010a04ed/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/hack/git-chglog/CHANGELOG.tpl.md b/hack/git-chglog/CHANGELOG.tpl.md new file mode 100644 index 0000000..d09fc6e --- /dev/null +++ b/hack/git-chglog/CHANGELOG.tpl.md @@ -0,0 +1,26 @@ + + + + +{{ range .Versions }} + +## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }} ({{ datetime "2006-01-02" .Tag.Date }}) + +{{ range .CommitGroups -}} +### {{ .Title }} + +{{ range .Commits -}} +* {{ if .Scope }}**{{ .Scope }}:** {{ end }}{{ .Subject }} +{{ end }} +{{ end -}} + +{{- if .NoteGroups -}} +{{ range .NoteGroups -}} +### {{ .Title }} + +{{ range .Notes }} +{{ .Body }} +{{ end }} +{{ end -}} +{{ end -}} +{{ end -}} diff --git a/hack/git-chglog/config.yaml b/hack/git-chglog/config.yaml new file mode 100644 index 0000000..27aa74c --- /dev/null +++ b/hack/git-chglog/config.yaml @@ -0,0 +1,32 @@ +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + +style: github +template: CHANGELOG.tpl.md +info: + title: CHANGELOG + repository_url: https://github.com/talos-systems/go-debug +options: + commits: + # filters: + # Type: + # - feat + # - fix + # - perf + # - refactor + commit_groups: + # title_maps: + # feat: Features + # fix: Bug Fixes + # perf: Performance Improvements + # refactor: Code Refactoring + header: + pattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$" + pattern_maps: + - Type + - Scope + - Subject + notes: + keywords: + - BREAKING CHANGE diff --git a/hack/release.sh b/hack/release.sh new file mode 100755 index 0000000..77a51a6 --- /dev/null +++ b/hack/release.sh @@ -0,0 +1,60 @@ +#!/bin/bash + +# THIS FILE WAS AUTOMATICALLY GENERATED, PLEASE DO NOT EDIT. +# +# Generated on 2021-05-07T17:06:21Z by kres latest. + + +set -e + +function changelog { + if [ "$#" -eq 1 ]; then + git-chglog --output CHANGELOG.md -c ./hack/git-chglog/config.yaml --tag-filter-pattern "^${1}" "${1}.0-alpha.0.." + elif [ "$#" -eq 0 ]; then + git-chglog --output CHANGELOG.md -c ./hack/git-chglog/config.yaml + else + echo 1>&2 "Usage: $0 changelog [tag]" + exit 1 + fi +} + +function release-notes { + git-chglog --output ${1} -c ./hack/git-chglog/config.yaml "${2}" +} + +function cherry-pick { + if [ $# -ne 2 ]; then + echo 1>&2 "Usage: $0 cherry-pick " + exit 1 + fi + + git checkout $2 + git fetch + git rebase upstream/$2 + git cherry-pick -x $1 +} + +function commit { + if [ $# -ne 1 ]; then + echo 1>&2 "Usage: $0 commit " + exit 1 + fi + + git commit -s -m "release($1): prepare release" -m "This is the official $1 release." +} + +if declare -f "$1" > /dev/null +then + cmd="$1" + shift + $cmd "$@" +else + cat <