diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..1bbfeef --- /dev/null +++ b/.envrc @@ -0,0 +1,4 @@ +if ! has nix_direnv_version || ! nix_direnv_version 2.1.0; then + source_url "https://raw.githubusercontent.com/nix-community/nix-direnv/2.1.0/direnvrc" "sha256-FAT2R9yYvVg516v3LiogjIc8YfsbWbMM/itqWsm5xTA=" +fi +use flake diff --git a/.github/workflows/dagger.yaml b/.github/workflows/dagger.yaml new file mode 100644 index 0000000..d8bd946 --- /dev/null +++ b/.github/workflows/dagger.yaml @@ -0,0 +1,41 @@ +name: Dagger + +on: + push: + branches: + - master + pull_request: + +jobs: + test: + name: Test + runs-on: ubuntu-latest + strategy: + matrix: + go: ['1.14', '1.15', '1.16', '1.17', '1.18'] + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Dagger + uses: dagger/dagger-for-github@v3 + with: + cmds: | + project update + do check test go ${{ matrix.go }} + + lint: + name: Lint + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Dagger + uses: dagger/dagger-for-github@v3 + with: + cmds: | + project update + do check lint diff --git a/.gitignore b/.gitignore index 373715a..39d3e11 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/.direnv/ /tmp/ /vendor/ diff --git a/BUILD b/BUILD.plz similarity index 100% rename from BUILD rename to BUILD.plz diff --git a/Taskfile.yaml b/Taskfile.yaml new file mode 100644 index 0000000..7ba6546 --- /dev/null +++ b/Taskfile.yaml @@ -0,0 +1,14 @@ +version: '3' + +tasks: + check: + cmds: + - dagger do check + + lint: + cmds: + - dagger do check lint + + fmt: + cmds: + - golangci-lint run --fix diff --git a/ci/codecov/image.cue b/ci/codecov/image.cue new file mode 100644 index 0000000..3fe6c81 --- /dev/null +++ b/ci/codecov/image.cue @@ -0,0 +1,74 @@ +package codecov + +import ( + "universe.dagger.io/docker" + // "universe.dagger.io/alpine" + "universe.dagger.io/bash" +) + +// Build a codecov base image +#Image: { + _packages: [pkgName=string]: { + // NOTE(samalba, gh issue #1532): + // it's not recommended to pin the version as it is already pinned by the major Alpine version + // version pinning is for future use (as soon as we support custom repositories like `community`, + // `testing` or `edge`) + version: string | *"" + } + _packages: { + bash: _ + curl: _ + git: _ + gnupg: _ + coreutils: _ + "perl-utils": _ + } + docker.#Build & { + steps: [ + // alpine.#Build & { + // packages: { + // bash: _ + // curl: _ + // gnupg: _ + // coreutils: _ + // "perl-utils": _ + // } + // }, + docker.#Pull & { + source: "index.docker.io/alpine:3.15.0@sha256:21a3deaa0d32a8057914f36584b5288d2e5ecc984380bc0118285c70fa8c9300" + }, + for pkgName, pkg in _packages { + docker.#Run & { + command: { + name: "apk" + args: ["add", "\(pkgName)\(pkg.version)"] + flags: { + "-U": true + "--no-cache": true + } + } + } + }, + bash.#Run & { + script: contents: """ + curl https://keybase.io/codecovsecurity/pgp_keys.asc | gpg --no-default-keyring --keyring trustedkeys.gpg --import + + curl -Os https://uploader.codecov.io/latest/alpine/codecov + + curl -Os https://uploader.codecov.io/latest/alpine/codecov.SHA256SUM + + curl -Os https://uploader.codecov.io/latest/alpine/codecov.SHA256SUM.sig + + gpgv codecov.SHA256SUM.sig codecov.SHA256SUM + + shasum -a 256 -c codecov.SHA256SUM + + chmod +x codecov + + mv codecov /usr/local/bin + rm codecov.SHA256SUM codecov.SHA256SUM.sig + """ + }, + ] + } +} diff --git a/ci/codecov/test/image.cue b/ci/codecov/test/image.cue new file mode 100644 index 0000000..4b309a3 --- /dev/null +++ b/ci/codecov/test/image.cue @@ -0,0 +1,11 @@ +package codecov + +import ( + "dagger.io/dagger" + + "emperror.dev/errors/ci/codecov" +) + +dagger.#Plan & { + actions: test: codecov.#Image & {} +} diff --git a/ci/codecov/upload.cue b/ci/codecov/upload.cue new file mode 100644 index 0000000..e3cb073 --- /dev/null +++ b/ci/codecov/upload.cue @@ -0,0 +1,52 @@ +package codecov + +import ( + "dagger.io/dagger" + + "universe.dagger.io/docker" +) + +#Upload: { + // Source code + source: dagger.#FS + + // Coverage files + file: string + + // Codecov token (required for local runs and private repos) + token?: dagger.#Secret + + // Don't upload files to Codecov + dryRun: bool | *false + + _image: #Image + + _sourcePath: "/src" + + docker.#Run & { + input: *_image.output | docker.#Image + command: { + name: "codecov" + flags: { + "--file": file + "--verbose": true + + if dryRun { + "--dryRun": true + } + } + } + env: { + if token != _|_ { + CODECOV_TOKEN: token + } + } + workdir: _sourcePath + mounts: { + "source": { + dest: _sourcePath + contents: source + } + } + } +} diff --git a/cue.mod/.gitignore b/cue.mod/.gitignore new file mode 100644 index 0000000..089b067 --- /dev/null +++ b/cue.mod/.gitignore @@ -0,0 +1,3 @@ +/gen/ +/pkg/ +/tmp/ diff --git a/cue.mod/dagger.mod b/cue.mod/dagger.mod new file mode 100644 index 0000000..5f38a63 --- /dev/null +++ b/cue.mod/dagger.mod @@ -0,0 +1 @@ +github.com/sagikazarmark/dagger main diff --git a/cue.mod/dagger.sum b/cue.mod/dagger.sum new file mode 100644 index 0000000..65ec048 --- /dev/null +++ b/cue.mod/dagger.sum @@ -0,0 +1 @@ +github.com/sagikazarmark/dagger h1:ekTs7tQbia01P9hMcMFSmA5TMFybb5F15Nlx8vYwU2g= diff --git a/cue.mod/module.cue b/cue.mod/module.cue new file mode 100644 index 0000000..859c833 --- /dev/null +++ b/cue.mod/module.cue @@ -0,0 +1 @@ +module: "emperror.dev/errors" \ No newline at end of file diff --git a/dagger.cue b/dagger.cue new file mode 100644 index 0000000..7739c86 --- /dev/null +++ b/dagger.cue @@ -0,0 +1,124 @@ +package main + +import ( + "dagger.io/dagger" + "dagger.io/dagger/core" + + "universe.dagger.io/go" + + "github.com/sagikazarmark/dagger/go/golangci" + "emperror.dev/errors/ci/codecov" +) + +dagger.#Plan & { + client: filesystem: ".": read: exclude: [ + ".github", + "bin", + "build", + "tmp", + ] + client: network: "unix:///var/run/docker.sock": connect: dagger.#Socket + client: env: { + CI: string | *"" + GITHUB_ACTIONS: string | *"" + GITHUB_ACTION: string | *"" + GITHUB_HEAD_REF: string | *"" + GITHUB_REF: string | *"" + GITHUB_REPOSITORY: string | *"" + GITHUB_RUN_ID: string | *"" + GITHUB_SERVER_URL: string | *"" + GITHUB_SHA: string | *"" + GITHUB_WORKFLOW: string | *"" + CODECOV_TOKEN?: dagger.#Secret + } + + actions: { + _source: client.filesystem["."].read.contents + + check: { + test: { + "go": { + "1.14": _ + "1.15": _ + "1.16": _ + "1.17": _ + "1.18": _ + + [v=string]: { + _test: go.#Test & { + source: _source + name: "go_test_\(v)" // necessary to keep cache for different versions separate + package: "./..." + + _image: go.#Image & { + version: v + } + + input: _image.output + command: flags: { + "-race": true + "-covermode": "atomic" + "-coverprofile": "/coverage.out" + } + + export: files: "/coverage.out": _ + } + + _coverage: codecov.#Upload & { + _write: core.#WriteFile & { + input: _source + path: "/coverage.out" + contents: _test.export.files."/coverage.out" + } + + source: _write.output + file: "/src/coverage.out" + + // Fixes https://github.com/dagger/dagger/issues/2680 + // _env: client.env + + // if _env.CODECOV_TOKEN != _|_ { + // token: _env.CODECOV_TOKEN + // } + + // Fixes https://github.com/dagger/dagger/issues/2680 + _token: client.env.CODECOV_TOKEN + + if client.env.CODECOV_TOKEN != _|_ { + token: client.env.CODECOV_TOKEN + } + + dryRun: client.env.CI != "true" + + // token: client.env.CODECOV_TOKEN + + env: { + // if client.env.CODECOV_TOKEN != _|_ { + // CODECOV_TOKEN: client.env.CODECOV_TOKEN + // } + GITHUB_ACTIONS: client.env.GITHUB_ACTIONS + GITHUB_ACTION: client.env.GITHUB_ACTION + GITHUB_HEAD_REF: client.env.GITHUB_HEAD_REF + GITHUB_REF: client.env.GITHUB_REF + GITHUB_REPOSITORY: client.env.GITHUB_REPOSITORY + GITHUB_RUN_ID: client.env.GITHUB_RUN_ID + GITHUB_SERVER_URL: client.env.GITHUB_SERVER_URL + GITHUB_SHA: client.env.GITHUB_SHA + GITHUB_WORKFLOW: client.env.GITHUB_WORKFLOW + } + } + + export: files: "/coverage.out": _test.export.files."/coverage.out" + } + } + } + + lint: { + "golangci": golangci.#Lint & { + source: _source + version: "1.46" + } + } + } + } +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..ccab86a --- /dev/null +++ b/flake.lock @@ -0,0 +1,84 @@ +{ + "nodes": { + "flake-utils": { + "locked": { + "lastModified": 1653893745, + "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "gobin": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1652111277, + "narHash": "sha256-jxl+fhzykhPP708r+WMotsg8Bjti9fpRoh4KiwPJ7XM=", + "owner": "sagikazarmark", + "repo": "go-bin-flake", + "rev": "7520b0da6c41e9d698ff5b408688de426c0218ea", + "type": "github" + }, + "original": { + "owner": "sagikazarmark", + "repo": "go-bin-flake", + "type": "github" + } + }, + "goflake": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1655154025, + "narHash": "sha256-2CI379dRvr1rJbkdHGzjrp+WqhdARbZlzinRecuSUrY=", + "owner": "sagikazarmark", + "repo": "go-flake", + "rev": "1ec7a1219b4b6698bc7b8f3263761c23c66d520a", + "type": "github" + }, + "original": { + "owner": "sagikazarmark", + "repo": "go-flake", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1655221618, + "narHash": "sha256-ht8HRFthDKzYt+il+sGgkBwrv+Ex2l8jdGVpsrPfFME=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "6616de389ed55fba6eeba60377fc04732d5a207c", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-unstable", + "type": "indirect" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "gobin": "gobin", + "goflake": "goflake", + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..c660a52 --- /dev/null +++ b/flake.nix @@ -0,0 +1,41 @@ +{ + description = "Drop-in replacement for the standard library errors package"; + + inputs = { + nixpkgs.url = "nixpkgs/nixos-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + goflake.url = "github:sagikazarmark/go-flake"; + goflake.inputs.nixpkgs.follows = "nixpkgs"; + gobin.url = "github:sagikazarmark/go-bin-flake"; + gobin.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = { self, nixpkgs, flake-utils, goflake, gobin, ... }: + flake-utils.lib.eachDefaultSystem ( + system: + let + pkgs = import nixpkgs { + inherit system; + + overlays = [ + goflake.overlay + + ( + final: prev: { + golangci-lint = gobin.packages.${system}.golangci-lint-bin; + } + ) + ]; + }; + + buildDeps = with pkgs; [ git go_1_17 gnumake ]; + devDeps = with pkgs; buildDeps ++ [ + golangci-lint + gotestsum + dagger + go-task + ]; + in + { devShell = pkgs.mkShell { buildInputs = devDeps; }; } + ); +}