From b59565bf7460b1a487e8e4d9b92834e3fdeed418 Mon Sep 17 00:00:00 2001 From: Franck Cuny Date: Tue, 19 Dec 2023 08:57:04 -0800 Subject: add goreleaser to go template --- templates/go/.github/workflows/release.yaml | 25 ++++++ templates/go/.github/workflows/update.yaml | 28 ++++++ templates/go/.github/workflows/update.yml | 28 ------ templates/go/.gitignore | 4 + templates/go/.goreleaser.yaml | 28 ++++++ templates/go/Makefile | 19 ++++ templates/go/README.md | 3 + templates/go/flake.nix | 132 +++++++++++++--------------- templates/go/go.mod | 2 +- templates/go/main.go | 4 + 10 files changed, 175 insertions(+), 98 deletions(-) create mode 100644 templates/go/.github/workflows/release.yaml create mode 100644 templates/go/.github/workflows/update.yaml delete mode 100644 templates/go/.github/workflows/update.yml create mode 100644 templates/go/.gitignore create mode 100644 templates/go/.goreleaser.yaml create mode 100644 templates/go/Makefile (limited to 'templates/go') diff --git a/templates/go/.github/workflows/release.yaml b/templates/go/.github/workflows/release.yaml new file mode 100644 index 0000000..a828de1 --- /dev/null +++ b/templates/go/.github/workflows/release.yaml @@ -0,0 +1,25 @@ +name: release +on: + push: + tags: + - "*" +permissions: + contents: write +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - run: git fetch --force --tags + - uses: actions/setup-go@v4 + with: + go-version: stable + - uses: goreleaser/goreleaser-action@v5 + with: + distribution: goreleaser + version: latest + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/templates/go/.github/workflows/update.yaml b/templates/go/.github/workflows/update.yaml new file mode 100644 index 0000000..2723a6f --- /dev/null +++ b/templates/go/.github/workflows/update.yaml @@ -0,0 +1,28 @@ +name: 'Update flake.lock' +on: + workflow_dispatch: + schedule: + - cron: '30 5 * * 0' +# you need to grant permissions to create PR: +# https://github.com/DeterminateSystems/update-flake-lock/issues/75 +jobs: + update-flake-lock: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Install Nix + uses: cachix/install-nix-action@v23 + with: + extra_nix_config: | + access-tokens = github.com=${{ secrets.GITHUB_TOKEN }} + - name: Update flake.lock + id: update-flake-lock + uses: DeterminateSystems/update-flake-lock@v20 + with: + pr-title: "Update flake.lock" + commit-msg: "chore: update flake" + pr-assignees: fcuny + pr-labels: | + dependencies + automated diff --git a/templates/go/.github/workflows/update.yml b/templates/go/.github/workflows/update.yml deleted file mode 100644 index 2723a6f..0000000 --- a/templates/go/.github/workflows/update.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: 'Update flake.lock' -on: - workflow_dispatch: - schedule: - - cron: '30 5 * * 0' -# you need to grant permissions to create PR: -# https://github.com/DeterminateSystems/update-flake-lock/issues/75 -jobs: - update-flake-lock: - runs-on: ubuntu-latest - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - name: Install Nix - uses: cachix/install-nix-action@v23 - with: - extra_nix_config: | - access-tokens = github.com=${{ secrets.GITHUB_TOKEN }} - - name: Update flake.lock - id: update-flake-lock - uses: DeterminateSystems/update-flake-lock@v20 - with: - pr-title: "Update flake.lock" - commit-msg: "chore: update flake" - pr-assignees: fcuny - pr-labels: | - dependencies - automated diff --git a/templates/go/.gitignore b/templates/go/.gitignore new file mode 100644 index 0000000..efec39a --- /dev/null +++ b/templates/go/.gitignore @@ -0,0 +1,4 @@ +/result +/.pre-commit-config.yaml +/bin/ +/dist/ diff --git a/templates/go/.goreleaser.yaml b/templates/go/.goreleaser.yaml new file mode 100644 index 0000000..d907209 --- /dev/null +++ b/templates/go/.goreleaser.yaml @@ -0,0 +1,28 @@ +project_name: fixproject +builds: + - id: default + main: . + binary: fixproject + flags: + - -tags + - -trimpath + env: + - CGO_ENABLED=0 + ldflags: + - -s -w -X main.Version={{.Version}} -X main.BuildDate={{ .CommitDate }} + goos: + - darwin + - linux + goarch: + - amd64 + - arm64 +archives: + - id: default + builds: + - default + name_template: "fixproject_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}" + format: tar.gz +checksum: + name_template: "checksums.txt" +snapshot: + name_template: "{{ incpatch .Version }}-next" diff --git a/templates/go/Makefile b/templates/go/Makefile new file mode 100644 index 0000000..34fe639 --- /dev/null +++ b/templates/go/Makefile @@ -0,0 +1,19 @@ +.PHONY: build run clean + +BUILD_DIR=bin +PROGRAM_FILE=fixproject +VERSION=$(shell git describe --tag --always) +BUILD_DATE ?= $(shell TZ=UTC0 git show -s --format=%cd --date=format-local:'%Y-%m-%dT%H:%M:%SZ' HEAD) + +build: + @go build \ + -o ${BUILD_DIR}/${PROGRAM_FILE} \ + -ldflags "-X main.Version=${VERSION} -X main.BuildDate=${BUILD_DATE}" \ + -trimpath main.go + +run: build + @./${BUILD_DIR}/${PROGRAM_FILE} + +clean: + @go clean + @rm -rf ${BUILD_DIR}/${PROGRAM_FILE} diff --git a/templates/go/README.md b/templates/go/README.md index e69de29..f27d226 100644 --- a/templates/go/README.md +++ b/templates/go/README.md @@ -0,0 +1,3 @@ +New shiny project + +TODO: replace all instances of `fixproject` with the name of the project. diff --git a/templates/go/flake.nix b/templates/go/flake.nix index ce7fa0d..4746a98 100644 --- a/templates/go/flake.nix +++ b/templates/go/flake.nix @@ -1,83 +1,77 @@ { - description = "Go project template"; - inputs = { - futils.url = "github:numtide/flake-utils"; - nixpkgs.url = "github:NixOS/nixpkgs"; - pre-commit-hooks = { - url = "github:cachix/pre-commit-hooks.nix"; - inputs = { - flake-utils.follows = "futils"; - nixpkgs.follows = "nixpkgs"; - }; - }; + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-parts = { url = "github:hercules-ci/flake-parts"; inputs.nixpkgs-lib.follows = "nixpkgs"; }; + treefmt-nix = { url = "github:numtide/treefmt-nix"; inputs.nixpkgs.follows = "nixpkgs"; }; + devshell.url = "github:numtide/devshell"; + pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix"; }; - outputs = - { self - , futils - , nixpkgs - , pre-commit-hooks - }: - futils.lib.eachDefaultSystem - (system: - let - pkgs = import nixpkgs { - inherit system; - }; - pname = "project-name"; - version = "0.0.1"; - tools = with pkgs; [ - # https://github.com/golang/vscode-go/blob/master/docs/tools.md - delve - golangci-lint - gopls - ]; - in - rec { - # `nix build` - packages."${pname}" = pkgs.buildGoModule { - inherit pname version; - src = ./.; - vendorSha256 = null; - }; - defaultPackage = packages."${pname}"; + outputs = inputs: + inputs.flake-parts.lib.mkFlake { inherit inputs; } { + systems = [ "x86_64-linux" "aarch64-darwin" ]; + imports = [ + inputs.treefmt-nix.flakeModule + inputs.pre-commit-hooks.flakeModule + inputs.devshell.flakeModule + ]; - # `nix run` - apps = { - "${pname}" = futils.lib.mkApp { - drv = packages."${pname}"; - exePath = "/bin/changeme"; + perSystem = { pkgs, config, ... }: + let + src = ./.; + package = { + # Replace the following throws with strings with the appropriate values + name = throw "package.name: missing value"; + version = throw "package.name: missing value"; + vendorHash = null; }; - default = apps."${pname}"; - }; + in + { + packages = { + ${package.name} = pkgs.buildGoModule { + pname = package.name; + inherit (package) + version + vendorHash; + inherit src; + }; + default = config.packages.${package.name}; + }; + + formatter = pkgs.treefmt; - # `nix develop` - checks = { - pre-commit = pre-commit-hooks.lib.${system}.run { - src = ./.; - hooks = { - nixpkgs-fmt.enable = true; - yamllint.enable = true; - govet.enable = true; - gotest.enable = true; - gofmt.enable = true; - staticcheck.enable = true; + devshells.default = { + commands = [ + { + name = "build"; + category = "dev"; + help = "Build the binary"; + command = "make"; + } + ]; + packages = with pkgs; [ + go_1_21 + gopls + golangci-lint + ]; + devshell.startup = { + pre-commit.text = config.pre-commit.installationScript; }; + }; + + treefmt = { + projectRootFile = "go.mod"; + programs.gofmt.enable = true; + programs.nixpkgs-fmt.enable = true; + }; + + pre-commit = { settings = { - yamllint.relaxed = true; + hooks = { + treefmt.enable = true; + }; }; }; }; - - devShell = pkgs.mkShell { - buildInputs = with pkgs; [ go ] ++ tools; - inherit (self.checks.${system}.pre-commit) shellHook; - }; - }) - // { - overlay = _: prev: { - XXX = self.defaultPackage.${prev.system}; - }; }; } diff --git a/templates/go/go.mod b/templates/go/go.mod index aa1e0fb..d85760b 100644 --- a/templates/go/go.mod +++ b/templates/go/go.mod @@ -1,3 +1,3 @@ -module github.com/fcuny/changeme +module github.com/fcuny/fixproject go 1.21 diff --git a/templates/go/main.go b/templates/go/main.go index 7776ec8..b5b1c7a 100644 --- a/templates/go/main.go +++ b/templates/go/main.go @@ -2,6 +2,10 @@ package main import "fmt" +var ( + Version, BuildDate string +) + func main() { fmt.Println("hello world!") } -- cgit 1.4.1