From 686481f6508e19516c5222340c9ea2e66bedd7d7 Mon Sep 17 00:00:00 2001 From: Matthieu Vachon Date: Thu, 21 Apr 2022 15:46:05 -0400 Subject: [PATCH] Updated go releaser, updated release script --- .goreleaser.yml | 107 +++++++++++++++++++++++++----------------- bin/release | 59 ----------------------- bin/release.sh | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 8 ++-- 4 files changed, 190 insertions(+), 106 deletions(-) delete mode 100755 bin/release create mode 100755 bin/release.sh diff --git a/.goreleaser.yml b/.goreleaser.yml index 6c428e3..7fad788 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -1,46 +1,62 @@ project_name: zap-pretty -before: - hooks: - - go mod download - - go generate ./... +release: + github: + owner: maoueh + name: zap-pretty + name_template: '{{.Tag}}' +builds: + - id: zap-pretty + goos: + - linux + - darwin + - windows + goarch: + - arm64 + - amd64 + targets: + - linux_amd64 + - darwin_amd64 + - darwin_arm64 + - windows_amd64 + main: ./ + ldflags: -s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}} + binary: zap-pretty + env: + - CGO_ENABLED=0 + +archives: + # sfeth + - name_template: '{{ .Binary }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' + id: zap-pretty + builds: + - zap-pretty + replacements: + amd64: x86_64 + darwin: macOS + linux: linux + format: tar.gz + files: + - LICENSE.md + - README.md + +snapshot: + name_template: '{{ .Tag }}-next' + +checksum: + name_template: checksums.txt changelog: - sort: asc filters: exclude: - '^docs:' - '^test:' - - '^support:' - - '^chore:' - -builds: -- goos: - - linux - - darwin - - windows - goarch: - - amd64 - targets: - - linux_amd64 - - darwin_amd64 - - windows_amd64 + sort: asc -archive: - replacements: - darwin: MacOS - linux: Linux - windows: Windows - amd64: x86_64 - files: - - LICENSE.md - - README.md +dist: dist -checksum: - name_template: 'checksums.txt' - -sign: - cmd: keybase +signs: +- cmd: keybase args: - sign - --infile @@ -51,14 +67,19 @@ sign: - --detached signature: ${artifact}.sig artifacts: checksum - -snapshot: - name_template: "{{ .Tag }}-dirty" - -brew: - github: - owner: maoueh - name: homebrew-tap - homepage: "https://github.com/maoueh/zap-pretty" - description: "A tiny binary to pretty-print Zap production JSON lines" +env_files: + github_token: ~/.config/goreleaser/github_token +brews: + - name: zap-pretty + ids: + - zap-pretty + tap: + owner: maoueh + name: homebrew-tap + commit_author: + name: goreleaserbot + email: matthieu.o.vachon@gmail.com + homepage: "https://github.com/maoueh/zap-pretty" + description: "A tiny binary to pretty-print Zap production JSON lines" + license: "MIT" diff --git a/bin/release b/bin/release deleted file mode 100755 index 4e7fed9..0000000 --- a/bin/release +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env bash - -ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" - -main() { - pushd "$ROOT" &> /dev/null - - dry_run="true" - - while getopts "hf" opt; do - case $opt in - h) usage && exit 0;; - f) dry_run="false";; - \?) usage_error "Invalid option: -$OPTARG";; - esac - done - shift $((OPTIND-1)) - - version="$1"; shift - - if [[ $version == "" ]]; then - usage_error "parameter is required" - fi - - args="" - if [[ $dry_run == "true" ]]; then - args="--skip-publish" - fi - - goreleaser --rm-dist $args -} - -usage_error() { - message="$1" - exit_code="$2" - - echo "ERROR: $message" - echo "" - usage - exit ${exit_code:-1} -} - -usage() { - echo "usage: release [-f] " - echo "" - echo "Perform the commands necessary to release a new version of the project, mainly" - echo "it does its tasks by calling 'goreleaser' and performing some Git commands." - echo "" - echo "The script runs in dry run automatically unless the '-f' option is provided." - echo "" - echo "Tasks" - echo " ?" - echo "" - echo "" - echo "Options" - echo " -h Display help about this script" -} - -main "$@" \ No newline at end of file diff --git a/bin/release.sh b/bin/release.sh new file mode 100755 index 0000000..0e1494c --- /dev/null +++ b/bin/release.sh @@ -0,0 +1,122 @@ +#!/usr/bin/env bash + +ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" + +dry_run="" +force="false" + +main() { + pushd "$ROOT" &> /dev/null + + while getopts "hnf" opt; do + case $opt in + h) usage && exit 0;; + n) dry_run="true";; + f) force="true";; + \?) usage_error "Invalid option: -$OPTARG";; + esac + done + shift $((OPTIND-1)) + + verify_github_token + verify_keybase + + if [[ "$dry_run" == "true" && "$force" == "true" ]]; then + usage_error "Only one of -n (dry run) or -f (force) can be provided at a time" + fi + + version="$1"; shift + if [[ "$version" == "" ]]; then + printf "What version do you want to release (current latest is `git describe --tags --abbrev=0`)? " + read version + fi + + if [[ ! "$version" =~ ^v ]]; then + echo "Version $version is invalid, must start with a 'v'" + exit 1 + fi + + mode="Dry Run, use -f flag to switch to publishing mode" + if [[ "$force" == "true" ]]; then + mode="Publishing" + fi + + echo "About to release version tagged $version ($mode)" + sleep 3 + + if [[ "$force" == "true" ]]; then + echo "Pushing to ensure GitHub knowns about the latest commit(s)" + git push + fi + + args="--rm-dist" + if [[ "$force" == "false" ]]; then + args="--skip-publish --skip-validate $args" + fi + + set -e + git tag "$version" + set +e + + goreleaser release $args + if [[ $? -gt 0 || "$force" == "false" ]]; then + git tag -d "$version" + fi +} + +verify_github_token() { + if [[ ! -f "$HOME/.config/goreleaser/github_token" && "$GITHUB_TOKEN" = "" ]]; then + echo "No GitHub token could be found in enviornment variable GITHUB_TOKEN" + echo "nor at ~/.config/goreleaser/github_token." + echo "" + echo "You will need to create one on GitHub website and make it available through" + echo "one of the accept way mentionned above." + exit 1 + fi +} + +verify_keybase() { + if ! command keybase &> /dev/null; then + echo "Keybase is required to sign the release (the checksum of all the artifacts" + echo "to be precise)." + echo "" + echo "You will need to have it available ('brew install keybase' on Mac OS X) and" + echo "configure it, just setting your Git username and a password should be enough." + exit 1 + fi +} + +usage_error() { + message="$1" + exit_code="$2" + + echo "ERROR: $message" + echo "" + usage + exit ${exit_code:-1} +} + +usage() { + echo "usage: release.sh [-h] [-f] [-n] []" + echo "" + echo "Perform the necessary commands to perform a release of the project." + echo "The is optional, if not provided, you'll be asked the question." + echo "" + echo "The release being performed against GitHub, you need a valid GitHub API token" + echo "with the necessary rights to upload release and push to repositories. It needs to" + echo "be provided in file ~/.config/goreleaser/github_token or through an environment" + echo "variable GITHUB_TOKEN." + echo "" + echo "Keybase is required to sign the release (the checksum of all the artifacts" + echo "to be precise)." + echo "" + echo "You will need to have it available ('brew install keybase' on Mac OS X) and" + echo "configure it, just setting your Git username and a password should be enough." + echo "" + echo "Options" + echo " -f Run in write mode publishing the release to GitHub" + echo " -n Run in dry-run mode skipping validation and publishing" + echo " -h Display help about this script" +} + +main "$@" \ No newline at end of file diff --git a/main.go b/main.go index 352586c..4e06f78 100644 --- a/main.go +++ b/main.go @@ -19,10 +19,6 @@ import ( . "github.com/logrusorgru/aurora" ) -var debug = log.New(ioutil.Discard, "", 0) -var debugEnabled = false -var severityToColor map[string]Color - // Provided via ldflags by goreleaser automatically var ( version = "dev" @@ -30,6 +26,10 @@ var ( date = "unknown" ) +var debug = log.New(ioutil.Discard, "", 0) +var debugEnabled = false +var severityToColor map[string]Color + var errNonZapLine = errors.New("non-zap line") func init() {