From e6fd1c4bb254466810ab92dfc39146a908df77bd Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Fri, 28 Jul 2023 19:03:17 +0900 Subject: [PATCH] Replace fmt glue bash with go --- .githooks/pre-commit | 4 +--- Makefile.toml | 6 +++++- cmd/fmt/main.go | 46 ++++++++++++++++++++++++++++++++++++++++++++ scripts/fmt.bash | 12 ------------ 4 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 cmd/fmt/main.go delete mode 100755 scripts/fmt.bash diff --git a/.githooks/pre-commit b/.githooks/pre-commit index 49c51e41..5201caed 100755 --- a/.githooks/pre-commit +++ b/.githooks/pre-commit @@ -2,7 +2,5 @@ set -euxo pipefail -shopt -s globstar - -./scripts/fmt.bash +makers fmt makers lint diff --git a/Makefile.toml b/Makefile.toml index 259c0686..27b993cf 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -27,7 +27,11 @@ args = [ ] [tasks.fmt] -command = './scripts/fmt.bash' +command = 'go' +args = [ + 'run', + './cmd/fmt', +] [tasks.check] alias = "lint" diff --git a/cmd/fmt/main.go b/cmd/fmt/main.go new file mode 100644 index 00000000..7420022f --- /dev/null +++ b/cmd/fmt/main.go @@ -0,0 +1,46 @@ +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{"fmt"}}, + {"shfmt", append([]string{"--write"}, bashPaths...)}, + {"nixpkgs-fmt", nixPaths}, + {"typos", []string{".", ".github", ".config", ".vscode", "--write-changes"}}, + {"go", []string{"fmt", "./..."}}, + } + + 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/scripts/fmt.bash b/scripts/fmt.bash deleted file mode 100755 index c220011a..00000000 --- a/scripts/fmt.bash +++ /dev/null @@ -1,12 +0,0 @@ -#!/usr/bin/env bash - -set -euxo pipefail - -shopt -s globstar - -# fixing typo is a fmt ...? -typos . .github .config .vscode --write-changes - -shfmt --write ./**/*.bash -dprint fmt -go fmt ./...