From aaf7a3901ddfbf900313a4587f8fe76b7c0d66b0 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Fri, 16 Jun 2023 12:35:30 +0900 Subject: [PATCH] I forgot many things of golang --- .editorconfig | 16 ++++++++ .github/dependabot.yml | 10 +++++ .github/workflows/actionlint.yml | 23 +++++++++++ .github/workflows/ci.yml | 20 ++++++++++ .gitignore | 24 ++++++++++++ .vscode/settings.json | 5 +++ LICENSE | 21 +++++++++++ README.md | 16 ++++++++ default.nix | 10 +++++ go.mod | 3 ++ nix-headbump.go | 65 ++++++++++++++++++++++++++++++++ 11 files changed, 213 insertions(+) create mode 100644 .editorconfig create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/actionlint.yml create mode 100644 .github/workflows/ci.yml create mode 100644 .gitignore create mode 100644 .vscode/settings.json create mode 100644 LICENSE create mode 100644 README.md create mode 100644 default.nix create mode 100644 go.mod create mode 100644 nix-headbump.go diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..98eb526 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +[*] +end_of_line = lf +charset = utf-8 +indent_style = space +indent_size = 2 +trim_trailing_whitespace = true +insert_final_newline = true + +[{Makefile,*.mk,go.mod,go.sum,*.go,.gitmodules,*.sh}] +indent_style = tab +indent_size = 4 + +[*.md] +trim_trailing_whitespace = false diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..2785022 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,10 @@ +version: 2 +updates: + - package-ecosystem: 'github-actions' + directory: '/' + schedule: + interval: 'weekly' + - package-ecosystem: 'gomod' + directory: '/' + schedule: + interval: 'weekly' diff --git a/.github/workflows/actionlint.yml b/.github/workflows/actionlint.yml new file mode 100644 index 0000000..debd64b --- /dev/null +++ b/.github/workflows/actionlint.yml @@ -0,0 +1,23 @@ +name: Lint GitHub Actions workflows +on: + push: + branches: + - main + paths: + - '.github/**' + pull_request: + paths: + - '.github/**' + +jobs: + actionlint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Download actionlint + id: get_actionlint + run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash) + shell: bash + - name: Check workflow files + run: ${{ steps.get_actionlint.outputs.executable }} -color -shellcheck= + shell: bash diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..84169f0 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,20 @@ +name: CI + +on: + push: + branches: + - main + pull_request: + +jobs: + go: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version-file: 'go.mod' + cache-dependency-path: 'go.sum' + - name: Build + run: go build -v diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..192ddc8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +# Binary +nix-headbump diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..bce2b4d --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "cSpell.words": [ + "nixpkgs" + ] +} diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cf2e296 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Kenichi Kamiya + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b18fb1f --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +# nix-headbump + +For my personal use. + +I'm a new to the Nix ecosystem. +(If you know a better way, please let me know!) + +I have `default.nix` and `shell.nix` in many repositories. They have different nixpath(?) in the ref from the created timing. +Personally, I use the latest [nixpkgs](https://github.com/NixOS/nixpkgs) ref. +When I want to bump it, I always visit the nixpkgs repository and copy and paste. It is a tedious task. + +## NOTE + +* I guess there are many other syntax patterns in Nix files that I have not used. This code will not care about them. +* I don't know [nix-community/go-nix](https://github.com/nix-community/go-nix) will fit or not. +* I don't know if Nix provides this feature with the CLI or not. diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..4a95a92 --- /dev/null +++ b/default.nix @@ -0,0 +1,10 @@ +{ pkgs ? import (fetchTarball "https://github.com/NixOS/nixpkgs/archive/e57b65abbbf7a2d5786acc86fdf56cde060ed026.tar.gz") { } }: + +pkgs.mkShell { + buildInputs = [ + pkgs.nil + pkgs.nixpkgs-fmt + pkgs.dprint + pkgs.actionlint + ]; +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..13a03b3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module nix-headbump + +go 1.20 diff --git a/nix-headbump.go b/nix-headbump.go new file mode 100644 index 0000000..3864191 --- /dev/null +++ b/nix-headbump.go @@ -0,0 +1,65 @@ +package main + +import ( + "encoding/json" + "io" + "log" + "net/http" + "os" + "regexp" +) + +func main() { + path := "default.nix" + isNixFileExist := true + if _, err := os.Stat(path); os.IsNotExist(err) { + path = "shell.nix" + if _, err := os.Stat(path); os.IsNotExist(err) { + isNixFileExist = false + } + } + + if isNixFileExist { + err := bump(path) + if err != nil { + log.Fatalf("Bumping the version has been failed: %s", err.Error()) + } + } else { + log.Fatalln("Both default.nix and shell.nix are not found") + } +} + +type Commit struct { + Sha string `json:"sha"` +} + +type Response struct { + Commit Commit `json:"commit"` +} + +func bump(path string) error { + bytes, err := os.ReadFile(path) + if err != nil { + return err + } + re := regexp.MustCompile(`(import \(fetchTarball "https://github.com/NixOS/nixpkgs/archive/)(?:[^"]+?)(\.tar\.gz"\))`) + req, _ := http.NewRequest("GET", "https://api.github.com/repos/NixOS/nixpkgs/branches/master", nil) + req.Header.Set("Accept", "application/vnd.github+json") + req.Header.Set("X-GitHub-Api-Version", "2022-11-28") + client := new(http.Client) + res, err := client.Do(req) + if err != nil { + return err + } + defer res.Body.Close() + body, err := io.ReadAll(res.Body) + if err != nil { + return err + } + jsonRes := &Response{} + if json.Unmarshal(body, jsonRes) != nil { + return err + } + replaced := re.ReplaceAll(bytes, []byte("${1}"+jsonRes.Commit.Sha+"${2}")) + return os.WriteFile(path, replaced, os.ModePerm) +}