From 03770b120e9a4dddcaf3662059ac0948e97c1825 Mon Sep 17 00:00:00 2001 From: Adam Babik Date: Fri, 31 Dec 2021 05:28:19 +0100 Subject: [PATCH] feat(structslop): add support for structslop (#21) Learn More: * https://github.com/orijtech/structslop Co-Authors: * Adam Babik : https://github.com/adambabik * TekWizely : https://github.com/TekWizely --- .pre-commit-hooks.yaml | 66 +++++++++++++++++++++++++++++++++++++++ README.md | 35 +++++++++++++++++++++ go-structslop-mod.sh | 3 ++ go-structslop-pkg.sh | 3 ++ go-structslop-repo-mod.sh | 3 ++ go-structslop-repo-pkg.sh | 3 ++ sample-config.yaml | 7 +++++ 7 files changed, 120 insertions(+) create mode 100755 go-structslop-mod.sh create mode 100644 go-structslop-pkg.sh create mode 100755 go-structslop-repo-mod.sh create mode 100755 go-structslop-repo-pkg.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index dfeb303..a5703ea 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -542,6 +542,72 @@ description: "Run 'staticcheck [$ARGS] ./...' in repo root folder" pass_filenames: false +# ============================================================================== +# go-structslop-mod +# * Folder-Based +# * Recursive +# * Targets first parent folder with a go.mod file +# * Executes if any .go files modified +# * Executes if go.mod modified +# ============================================================================== +- id: go-structslop-mod + name: 'go-structslop-mod' + entry: go-structslop-mod.sh + files: '(\.go$)|(\bgo\.mod$)' + exclude: '(^|/)vendor/' + language: 'script' + description: "Run 'cd $(mod_root $FILE); structslop [$ARGS] ./...' for each staged .go file" + pass_filenames: true + require_serial: true + +# ============================================================================== +# go-structslop-pkg +# * Folder-Based +# * Targets folder containing staged file +# * Executes if any .go files modified +# ============================================================================== +- id: go-structslop-pkg + name: 'go-structslop-pkg' + entry: go-structslop-pkg.sh + types: [go] + exclude: '(^|/)vendor/' + language: 'script' + description: "Run 'structslop [$ARGS] ./$(dirname $FILE)' for each staged .go file" + pass_filenames: true + require_serial: true + +# ============================================================================== +# go-structslop-repo-mod +# * Repo-Based +# * Recursive +# * Targets ALL folders with a go.mod file +# * Executes if any .go files modified +# * Executes if go.mod modified +# ============================================================================== +- id: go-structslop-repo-mod + name: 'go-structslop-repo-mod' + entry: go-structslop-repo-mod.sh + files: '(\.go$)|(\bgo\.mod$)' + exclude: '(^|/)vendor/' + language: 'script' + description: "Run 'cd $(mod_root); structslop [$ARGS] ./...' for each module in the repo" + pass_filenames: false + +# ============================================================================== +# go-structslop-repo-pkg +# * Repo-Based +# * Recursive +# * Executes if any .go files modified +# ============================================================================== +- id: go-structslop-repo-pkg + name: 'go-structslop-repo-pkg' + entry: go-structslop-repo-pkg.sh + types: [go] + exclude: '(^|/)vendor/' + language: 'script' + description: "Run 'structslop [$ARGS] ./...' in repo root folder" + pass_filenames: false + # ============================================================================== # go-test-mod # * Folder-Based diff --git a/README.md b/README.md index f1219b6..04d8061 100644 --- a/README.md +++ b/README.md @@ -79,6 +79,13 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil - id: go-staticcheck-repo-mod - id: go-staticcheck-repo-pkg # + # StructSlop + # + - id: go-structslop-mod + - id: go-structslop-pkg + - id: go-structslop-repo-mod + - id: go-structslop-repo-pkg + # # Formatters # - id: go-fmt @@ -265,6 +272,7 @@ This can be useful, for example, for hooks that display warnings, but don't gene - [go-vet](#go-vet) - [go-sec](#go-sec) - [go-staticcheck](#go-staticcheck) + - [go-structslop](#go-structslop) - Formatters - [go-fmt](#go-fmt) - [go-fumpt](#go-fumpt) @@ -371,6 +379,33 @@ bingo install honnef.co/go/tools/cmd/staticcheck - https://staticcheck.io/ - `staticcheck -h` +----------------- +### go-structslop +Recommends struct field rearrangements to provide for maximum space/allocation efficiency. + + - Can modify files (see `-apply`) + +| Hook ID | Description +|--------------------------|------------ +| `go-structslop-mod` | Run `'cd $(mod_root $FILE); structslop [$ARGS] ./...'` for each staged .go file +| `go-structslop-pkg` | Run `'structslop [$ARGS] ./$(dirname $FILE)'` for each staged .go file +| `go-structslop-repo-mod` | Run `'cd $(mod_root); structslop [$ARGS] ./...'` for each module in the repo +| `go-structslop-repo-pkg` | Run `'structslop [$ARGS] ./...'` in repo root folder + +##### Install (via [bingo](https://github.com/TekWizely/bingo)) +``` +bingo install github.com/orijtech/structslop/cmd/structslop +``` + +##### Useful Args +``` +-apply : apply suggested fixes +``` + +##### Help + - https://github.com/orijtech/structslop#usage + - `structslop -h` + ---------- ### go-vet Examines Go source code and reports suspicious constructs, such as diff --git a/go-structslop-mod.sh b/go-structslop-mod.sh new file mode 100755 index 0000000..e63d0af --- /dev/null +++ b/go-structslop-mod.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cmd=(structslop) +. "$(dirname "${0}")/lib/cmd-mod.bash" diff --git a/go-structslop-pkg.sh b/go-structslop-pkg.sh new file mode 100644 index 0000000..f1ac76d --- /dev/null +++ b/go-structslop-pkg.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cmd=(structslop) +. "$(dirname "${0}")/lib/cmd-pkg.bash" diff --git a/go-structslop-repo-mod.sh b/go-structslop-repo-mod.sh new file mode 100755 index 0000000..b16280a --- /dev/null +++ b/go-structslop-repo-mod.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cmd=(structslop) +. "$(dirname "${0}")/lib/cmd-repo-mod.bash" diff --git a/go-structslop-repo-pkg.sh b/go-structslop-repo-pkg.sh new file mode 100755 index 0000000..4220bf1 --- /dev/null +++ b/go-structslop-repo-pkg.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash +cmd=(structslop) +. "$(dirname "${0}")/lib/cmd-repo-pkg.bash" diff --git a/sample-config.yaml b/sample-config.yaml index 48b1c19..b61f1cb 100644 --- a/sample-config.yaml +++ b/sample-config.yaml @@ -127,6 +127,13 @@ repos: - id: go-staticcheck-repo-mod - id: go-staticcheck-repo-pkg # + # StructSlop + # + - id: go-structslop-mod + - id: go-structslop-pkg + - id: go-structslop-repo-mod + - id: go-structslop-repo-pkg + # # Formatters # - id: go-fmt