From 3dc724d0e80b394e5ba405c9cdca528d3e007144 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Fri, 28 Jul 2023 18:02:19 +0900 Subject: [PATCH 1/2] Rewrite linter glue bash with go --- .githooks/pre-commit | 2 +- Makefile.toml | 6 +++++- cmd/lint/main.go | 48 ++++++++++++++++++++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ scripts/lint.bash | 13 ------------ 6 files changed, 58 insertions(+), 15 deletions(-) create mode 100644 cmd/lint/main.go create mode 100644 go.sum delete mode 100755 scripts/lint.bash diff --git a/.githooks/pre-commit b/.githooks/pre-commit index b3e96b20..49c51e41 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -5,4 +5,4 @@ set -euxo pipefail shopt -s globstar ./scripts/fmt.bash -./scripts/lint.bash +makers lint diff --git a/Makefile.toml b/Makefile.toml index 9beeeee6..736650f5 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -20,7 +20,11 @@ script = [ ] [tasks.lint] -command = './scripts/lint.bash' +command = 'go' +args = [ + 'run', + './cmd/lint', +] [tasks.fmt] command = './scripts/fmt.bash' diff --git a/cmd/lint/main.go b/cmd/lint/main.go new file mode 100644 index 00000000..a6fe035d --- /dev/null +++ b/cmd/lint/main.go @@ -0,0 +1,48 @@ +package main + +import ( + "log" + "os" + "os/exec" + "strings" + + doublestar "github.com/bmatcuk/doublestar/v4" +) + +func main() { + wd, err := os.Getwd() + if err != nil { + log.Fatalln(err) + } + fsys := os.DirFS(wd) + + bashPaths, err := doublestar.Glob(fsys, "./**/*.bash") + if err != nil { + log.Fatalln(err) + } + nixPaths, err := doublestar.Glob(fsys, "./**/*.nix") + if err != nil { + log.Fatalln(err) + } + + cmds := []struct { + path string + args []string + }{ + {"dprint", []string{"check"}}, + {"shfmt", append([]string{"--diff"}, bashPaths...)}, + {"shellcheck", bashPaths}, + {"nixpkgs-fmt", append([]string{"--check"}, nixPaths...)}, + {"typos", []string{".", ".github", ".config", ".vscode"}}, + {"gitleaks", []string{"detect"}}, + {"go", []string{"vet", "./..."}}, + } + + for _, cmd := range cmds { + output, err := exec.Command(cmd.path, cmd.args...).Output() + log.Printf("%s %s\n%s\n", cmd.path, strings.Join(cmd.args, " "), output) + if err != nil { + log.Fatalln(err) + } + } +} diff --git a/go.mod b/go.mod index 764067fe..76f944eb 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/kachick/dotfiles go 1.20 + +require github.com/bmatcuk/doublestar/v4 v4.6.0 diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..ce9f561f --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/bmatcuk/doublestar/v4 v4.6.0 h1:HTuxyug8GyFbRkrffIpzNCSK4luc0TY3wzXvzIZhEXc= +github.com/bmatcuk/doublestar/v4 v4.6.0/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc= diff --git a/scripts/lint.bash b/scripts/lint.bash deleted file mode 100755 index 9ec3e12e..00000000 --- a/scripts/lint.bash +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env bash - -set -euxo pipefail - -shopt -s globstar - -dprint check -shfmt --diff ./**/*.bash -shellcheck ./**/*.bash -nixpkgs-fmt --check ./**/*.nix -typos . .github .config .vscode -gitleaks detect -go vet ./... From 8250756fbba6838efab0ca3068998f870d1b6256 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Fri, 28 Jul 2023 18:08:12 +0900 Subject: [PATCH 2/2] Add simple build task --- Makefile.toml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Makefile.toml b/Makefile.toml index 736650f5..259c0686 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -43,6 +43,17 @@ args = [ command = 'mkdir' args = ['-p', 'dist'] +[tasks.build] +dependencies = ['prepare-build'] +command = 'go' +args = [ + 'build', + '-v', + '-o', + 'dist', + './...', +] + [tasks.test-race] dependencies = ['prepare-build'] command = 'go'