Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor go code #251

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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()
}

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)
}
}
}
33 changes: 11 additions & 22 deletions cmd/deps/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
46 changes: 9 additions & 37 deletions cmd/fmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ package main
import (
"log"
"os"
"os/exec"
"strings"
"sync"

doublestar "github.com/bmatcuk/doublestar/v4"

"github.com/kachick/dotfiles"
)
Expand All @@ -19,40 +14,17 @@ 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)
}

type command struct {
path string
args []string
}
bashPaths := dotfiles.MustGetAllBash(fsys)
nixPaths := dotfiles.MustGetAllNix(fsys)

// 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()
}
52 changes: 12 additions & 40 deletions cmd/lint/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ package main
import (
"log"
"os"
"os/exec"
"strings"
"sync"

doublestar "github.com/bmatcuk/doublestar/v4"

"github.com/kachick/dotfiles"
)
Expand All @@ -19,41 +14,18 @@ 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"}},
{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", "./..."}},
}

type command struct {
path string
args []string
}

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()
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
9 changes: 0 additions & 9 deletions linter.go

This file was deleted.

32 changes: 32 additions & 0 deletions path.go
Original file line number Diff line number Diff line change
@@ -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
}