about summary refs log tree commit diff
path: root/templates/go
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2023-12-19 08:57:04 -0800
committerFranck Cuny <franck@fcuny.net>2023-12-19 08:58:54 -0800
commitb59565bf7460b1a487e8e4d9b92834e3fdeed418 (patch)
tree4d3c0d3d31f885dd0ab671a4b3029d7ace449065 /templates/go
parentchore: update flake (diff)
downloadworld-b59565bf7460b1a487e8e4d9b92834e3fdeed418.tar.gz
add goreleaser to go template
Diffstat (limited to '')
-rw-r--r--templates/go/.github/workflows/release.yaml25
-rw-r--r--templates/go/.github/workflows/update.yaml (renamed from templates/go/.github/workflows/update.yml)0
-rw-r--r--templates/go/.gitignore4
-rw-r--r--templates/go/.goreleaser.yaml28
-rw-r--r--templates/go/Makefile19
-rw-r--r--templates/go/README.md3
-rw-r--r--templates/go/flake.nix132
-rw-r--r--templates/go/go.mod2
-rw-r--r--templates/go/main.go4
9 files changed, 147 insertions, 70 deletions
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.yml b/templates/go/.github/workflows/update.yaml
index 2723a6f..2723a6f 100644
--- a/templates/go/.github/workflows/update.yml
+++ b/templates/go/.github/workflows/update.yaml
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!")
 }