Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Commit

Permalink
I forgot many things of golang
Browse files Browse the repository at this point in the history
  • Loading branch information
kachick committed Jun 16, 2023
1 parent 0d0004a commit aaf7a39
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: 'github-actions'
directory: '/'
schedule:
interval: 'weekly'
- package-ecosystem: 'gomod'
directory: '/'
schedule:
interval: 'weekly'
23 changes: 23 additions & 0 deletions .github/workflows/actionlint.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"nixpkgs"
]
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
10 changes: 10 additions & 0 deletions default.nix
Original file line number Diff line number Diff line change
@@ -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
];
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module nix-headbump

go 1.20
65 changes: 65 additions & 0 deletions nix-headbump.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit aaf7a39

Please sign in to comment.