From d5ae646f562a1a777881a8a5655c85bcfc233bbc Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Fri, 4 Aug 2023 22:39:47 +0900 Subject: [PATCH 1/3] Share parallel processing logics --- cmd.go | 31 +++++++++++++++++++++++++++++++ cmd/fmt/main.go | 34 +++++++--------------------------- cmd/lint/main.go | 38 +++++++++----------------------------- 3 files changed, 47 insertions(+), 56 deletions(-) create mode 100644 cmd.go diff --git a/cmd.go b/cmd.go new file mode 100644 index 00000000..f4ec1fe9 --- /dev/null +++ b/cmd.go @@ -0,0 +1,31 @@ +package dotfiles + +import ( + "log" + "os/exec" + "strings" + "sync" +) + +type Cmd struct { + Path string + Args []string +} + +type Commands []Cmd + +func (cmds Commands) ParallelRun() { + wg := &sync.WaitGroup{} + for _, cmd := range cmds { + wg.Add(1) + go func(cmd Cmd) { + defer wg.Done() + 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) + } + }(cmd) + } + wg.Wait() +} diff --git a/cmd/fmt/main.go b/cmd/fmt/main.go index e60364b2..2cb08448 100644 --- a/cmd/fmt/main.go +++ b/cmd/fmt/main.go @@ -3,9 +3,6 @@ package main import ( "log" "os" - "os/exec" - "strings" - "sync" doublestar "github.com/bmatcuk/doublestar/v4" @@ -28,31 +25,14 @@ func main() { log.Fatalln(err) } - type command struct { - path string - args []string - } - // Do not cover the same files in another formatter for parallel processing - cmds := []command{ - {"dprint", []string{"fmt"}}, - {"shfmt", append([]string{"--language-dialect", "bash", "--write"}, bashPaths...)}, - {"nixpkgs-fmt", nixPaths}, - {"typos", append(dotfiles.GetTyposTargetedRoots(), "--write-changes")}, - {"go", []string{"fmt", "./..."}}, + cmds := dotfiles.Commands{ + {Path: "dprint", Args: []string{"fmt"}}, + {Path: "shfmt", Args: append([]string{"--language-dialect", "bash", "--write"}, bashPaths...)}, + {Path: "nixpkgs-fmt", Args: nixPaths}, + {Path: "typos", Args: append(dotfiles.GetTyposTargetedRoots(), "--write-changes")}, + {Path: "go", Args: []string{"fmt", "./..."}}, } - wg := &sync.WaitGroup{} - for _, cmd := range cmds { - wg.Add(1) - go func(cmd command) { - defer wg.Done() - 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) - } - }(cmd) - } - wg.Wait() + cmds.ParallelRun() } diff --git a/cmd/lint/main.go b/cmd/lint/main.go index 7feb8a40..23646dfa 100644 --- a/cmd/lint/main.go +++ b/cmd/lint/main.go @@ -3,9 +3,6 @@ package main import ( "log" "os" - "os/exec" - "strings" - "sync" doublestar "github.com/bmatcuk/doublestar/v4" @@ -28,32 +25,15 @@ func main() { log.Fatalln(err) } - type command struct { - path string - args []string + cmds := dotfiles.Commands{ + {Path: "dprint", Args: []string{"check"}}, + {Path: "shfmt", Args: append([]string{"--language-dialect", "bash", "--diff"}, bashPaths...)}, + {Path: "shellcheck", Args: bashPaths}, + {Path: "nixpkgs-fmt", Args: append([]string{"--check"}, nixPaths...)}, + {Path: "typos", Args: dotfiles.GetTyposTargetedRoots()}, + {Path: "gitleaks", Args: []string{"detect"}}, + {Path: "go", Args: []string{"vet", "./..."}}, } - cmds := []command{ - {"dprint", []string{"check"}}, - {"shfmt", append([]string{"--language-dialect", "bash", "--diff"}, bashPaths...)}, - {"shellcheck", bashPaths}, - {"nixpkgs-fmt", append([]string{"--check"}, nixPaths...)}, - {"typos", dotfiles.GetTyposTargetedRoots()}, - {"gitleaks", []string{"detect"}}, - {"go", []string{"vet", "./..."}}, - } - - wg := &sync.WaitGroup{} - for _, cmd := range cmds { - wg.Add(1) - go func(cmd command) { - defer wg.Done() - 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) - } - }(cmd) - } - wg.Wait() + cmds.ParallelRun() } From 57edbc4a59357004a7118bca3776a0b6a72f2cb4 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Fri, 4 Aug 2023 22:56:32 +0900 Subject: [PATCH 2/3] Adjust sequential run with parallel coding --- cmd.go | 10 ++++++++++ cmd/deps/main.go | 33 +++++++++++---------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cmd.go b/cmd.go index f4ec1fe9..d866e704 100644 --- a/cmd.go +++ b/cmd.go @@ -29,3 +29,13 @@ func (cmds Commands) ParallelRun() { } wg.Wait() } + +func (cmds Commands) SequentialRun() { + 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/cmd/deps/main.go b/cmd/deps/main.go index 4b6b1477..f139bdca 100644 --- a/cmd/deps/main.go +++ b/cmd/deps/main.go @@ -1,31 +1,20 @@ package main import ( - "log" - "os/exec" - "strings" + "github.com/kachick/dotfiles" ) func main() { - cmds := []struct { - path string - args []string - }{ - {"go", []string{"version"}}, - {"makers", []string{"--version"}}, - {"nix", []string{"--version"}}, - {"dprint", []string{"--version"}}, - {"shellcheck", []string{"--version"}}, - {"shfmt", []string{"--version"}}, - {"typos", []string{"--version"}}, - {"gitleaks", []string{"version"}}, + cmds := dotfiles.Commands{ + {Path: "go", Args: []string{"version"}}, + {Path: "makers", Args: []string{"--version"}}, + {Path: "nix", Args: []string{"--version"}}, + {Path: "dprint", Args: []string{"--version"}}, + {Path: "shellcheck", Args: []string{"--version"}}, + {Path: "shfmt", Args: []string{"--version"}}, + {Path: "typos", Args: []string{"--version"}}, + {Path: "gitleaks", Args: []string{"version"}}, } - 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) - } - } + cmds.SequentialRun() } From 3986404bbdd03993c038db43697f35f16e835d79 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Fri, 4 Aug 2023 22:51:52 +0900 Subject: [PATCH 3/3] Share paths getter with module --- cmd/fmt/main.go | 12 ++---------- cmd/lint/main.go | 12 ++---------- go.mod | 4 +++- go.sum | 2 ++ linter.go | 9 --------- path.go | 32 ++++++++++++++++++++++++++++++++ 6 files changed, 41 insertions(+), 30 deletions(-) delete mode 100644 linter.go create mode 100644 path.go diff --git a/cmd/fmt/main.go b/cmd/fmt/main.go index 2cb08448..b30f2292 100644 --- a/cmd/fmt/main.go +++ b/cmd/fmt/main.go @@ -4,8 +4,6 @@ import ( "log" "os" - doublestar "github.com/bmatcuk/doublestar/v4" - "github.com/kachick/dotfiles" ) @@ -16,14 +14,8 @@ func main() { } fsys := os.DirFS(wd) - bashPaths, err := doublestar.Glob(fsys, "./**/{*.bash,.bash*}") - if err != nil { - log.Fatalln(err) - } - nixPaths, err := doublestar.Glob(fsys, "./**/*.nix") - if err != nil { - log.Fatalln(err) - } + bashPaths := dotfiles.MustGetAllBash(fsys) + nixPaths := dotfiles.MustGetAllNix(fsys) // Do not cover the same files in another formatter for parallel processing cmds := dotfiles.Commands{ diff --git a/cmd/lint/main.go b/cmd/lint/main.go index 23646dfa..74d026e0 100644 --- a/cmd/lint/main.go +++ b/cmd/lint/main.go @@ -4,8 +4,6 @@ import ( "log" "os" - doublestar "github.com/bmatcuk/doublestar/v4" - "github.com/kachick/dotfiles" ) @@ -16,14 +14,8 @@ func main() { } fsys := os.DirFS(wd) - bashPaths, err := doublestar.Glob(fsys, "./**/{*.bash,.bash*}") - if err != nil { - log.Fatalln(err) - } - nixPaths, err := doublestar.Glob(fsys, "./**/*.nix") - if err != nil { - log.Fatalln(err) - } + bashPaths := dotfiles.MustGetAllBash(fsys) + nixPaths := dotfiles.MustGetAllNix(fsys) cmds := dotfiles.Commands{ {Path: "dprint", Args: []string{"check"}}, diff --git a/go.mod b/go.mod index 76f944eb..b43f26cc 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,6 @@ module github.com/kachick/dotfiles go 1.20 -require github.com/bmatcuk/doublestar/v4 v4.6.0 +require ( + github.com/bmatcuk/doublestar/v4 v4.6.0 +) diff --git a/go.sum b/go.sum index ce9f561f..9b88f6f0 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/bmatcuk/doublestar v1.3.4 h1:gPypJ5xD31uhX6Tf54sDPUOBXTqKH4c9aPY66CyQrS0= +github.com/bmatcuk/doublestar v1.3.4/go.mod h1:wiQtGV+rzVYxB7WIlirSN++5HPtPlXEo9MEoZQC/PmE= 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/linter.go b/linter.go deleted file mode 100644 index 4f51bd43..00000000 --- a/linter.go +++ /dev/null @@ -1,9 +0,0 @@ -package dotfiles - -func GetTyposTargetedRoots() []string { - return []string{ - ".", - ".github", ".vscode", - "home/.config", "home/.local", "home/.stack", - } -} diff --git a/path.go b/path.go new file mode 100644 index 00000000..19ee590d --- /dev/null +++ b/path.go @@ -0,0 +1,32 @@ +package dotfiles + +import ( + "io/fs" + "log" + + "github.com/bmatcuk/doublestar/v4" +) + +func GetTyposTargetedRoots() []string { + return []string{ + ".", + ".github", ".vscode", + "home/.config", "home/.local", "home/.stack", + } +} + +func MustGetAllBash(fsys fs.FS) []string { + bashPaths, err := doublestar.Glob(fsys, "./**/{*.bash,.bash*}") + if err != nil { + log.Fatalln(err) + } + return bashPaths +} + +func MustGetAllNix(fsys fs.FS) []string { + nixPaths, err := doublestar.Glob(fsys, "./**/*.nix") + if err != nil { + log.Fatalln(err) + } + return nixPaths +}