Skip to content

Commit 087a185

Browse files
authored
Optimizations (#135)
* Simplifies hookz deleter * Sources can be empty * Fixes error checking, updated devcontainer * updates components * Security fixes and test coverage * version bump
1 parent 9f569f9 commit 087a185

28 files changed

+275
-1370
lines changed

.devcontainer/devcontainer.json

+68-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,70 @@
11
{
2-
"name": "hookz-devcontainer",
3-
"image": "mcr.microsoft.com/devcontainers/go:1-1.22-bookworm",
4-
"features": {
5-
"ghcr.io/devcontainers-contrib/features/starship:1": {}
6-
}
2+
"name": "devcontainer-test",
3+
"image": "mcr.microsoft.com/devcontainers/go:1-1.22-bookworm",
4+
"features": {
5+
"ghcr.io/devcontainers-contrib/features/starship:1": {},
6+
"ghcr.io/azutake/devcontainer-features/go-packages-install:0": {
7+
"packages": [
8+
"github.com/devops-kung-fu/hookz@latest",
9+
"github.com/jandelgado/gcov2lcov@latest",
10+
"github.com/kisielk/errcheck@latest",
11+
"github.com/fzipp/gocyclo/cmd/gocyclo@latest",
12+
"golang.org/x/vuln/cmd/govulncheck@latest",
13+
"honnef.co/go/tools/cmd/staticcheck@latest"
14+
]
15+
}
16+
},
17+
"customizations": {
18+
"vscode": {
19+
"settings": {
20+
"terminal.integrated.customGlyphs": true,
21+
"terminal.integrated.fontFamily": "'0xProto Nerd Font', 'Droid Sans Mono', 'monospace', monospace",
22+
"editor.formatOnSave": true,
23+
"go.buildTags": "",
24+
"go.toolsEnvVars": {
25+
"CGO_ENABLED": "0"
26+
},
27+
"go.useLanguageServer": true,
28+
"go.testEnvVars": {
29+
"CGO_ENABLED": "1"
30+
},
31+
"go.testFlags": [
32+
"-v",
33+
"-race"
34+
],
35+
"go.testTimeout": "10s",
36+
"go.coverOnSingleTest": true,
37+
"go.coverOnSingleTestFile": true,
38+
"go.coverOnTestPackage": true,
39+
"go.lintTool": "golangci-lint",
40+
"go.lintOnSave": "package",
41+
"[go]": {
42+
"editor.codeActionsOnSave": {
43+
"source.organizeImports": "always"
44+
}
45+
},
46+
"markiscodecoverage.coverageThreshold": 95,
47+
"markiscodecoverage.enableOnStartup": true,
48+
"markiscodecoverage.searchCriteria": "*.lcov*"
49+
},
50+
"extensions": [
51+
"ms-vscode.go",
52+
"golang.go",
53+
"github.vscode-pull-request-github",
54+
"github.vscode-github-actions",
55+
"aleksandra.go-group-imports",
56+
"oderwat.indent-rainbow",
57+
"yzhang.markdown-all-in-one",
58+
"quicktype.quicktype",
59+
"jebbs.plantuml",
60+
"foxundermoon.shell-format",
61+
"ahebrank.yaml2json",
62+
"amazonwebservices.aws-toolkit-vscode",
63+
"markis.code-coverage",
64+
//"defaltd.go-coverage-viewer",
65+
"Gruntfuggly.todo-tree" // Highlights TODO comments"
66+
]
67+
}
68+
},
69+
"postCreateCommand": "/usr/bin/bash ./.devcontainer/post-create.sh > ~/post-create.log"
770
}

.devcontainer/post-create.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
mkdir -p $HOME/.local/share/fonts
2+
wget https://github.com/ryanoasis/nerd-fonts/releases/download/v3.1.1/0xProto.zip
3+
unzip 0xProto.zip -d $HOME/.local/share/fonts
4+
rm 0xProto.zip
5+
6+
starship preset nerd-font-symbols -o ~/.config/starship.toml

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
hookz
1818
coverage.out
1919
coverage.html
20+
coverage.lcov
2021

2122
.DS_Store

.hookz.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: 2.4.3
1+
version: 2.4.4
22
sources:
33
- source: github.com/devops-kung-fu/hinge@latest
44
- source: github.com/kisielk/errcheck@latest

.vscode/launch.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
55
"version": "0.2.0",
66
"configurations": [
7-
87
{
98
"name": "Debug",
109
"type": "go",
1110
"request": "launch",
1211
"mode": "auto",
1312
"program": "${workspaceFolder}/main.go",
14-
"args": ["reset", "verbose"]
13+
"args": [
14+
"reset",
15+
"--verbose",
16+
"--debug",
17+
"--verbose-output"
18+
]
1519
}
1620
]
1721
}

Makefile

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ title:
1111

1212
build: ## Builds the application
1313
go get -u ./...
14-
@go mod tidy
15-
@go build
14+
go mod tidy
15+
go build
1616

1717
check: build ## Tests the pre-commit hooks if they exist
1818
./hookz reset --verbose --debug --verbose-output
1919
. .git/hooks/pre-commit
2020

2121
test: ## Runs tests and coverage
22-
@go test -v -coverprofile=coverage.out ./... && go tool cover -func=coverage.out
23-
@go tool cover -html=coverage.out -o coverage.html
22+
go test -v -coverprofile=coverage.out ./... && go tool cover -func=coverage.out && gcov2lcov -infile=coverage.out -outfile=coverage.lcov
23+
go tool cover -html=coverage.out -o coverage.html
2424

2525
install: build ## Builds an executable local version of Hookz and puts in in /usr/local/bin
2626
@sudo chmod +x hookz

README.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ Git hooks are a great way to run supplemental commands as you interact with git.
2626

2727
```Hookz``` may return one of three different status codes as it executes the action pipeline:
2828

29-
| Code | Description |
30-
| ---- | ------------------------------------------------------------ |
31-
| PASS | The action has successfully completed |
29+
| Code | Description |
30+
| ---- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
31+
| PASS | The action has successfully completed |
3232
| WARN | An executable defined in the ```.hookz.yaml``` file wasn't found on the local system. In this case, the action is ignored and no attempt is made to run it. Flow will continue without an exit code of PASS or FAIL. |
33-
| FAIL | The action has failed. Execution stops. Consider this like a build break in a CI/CD pipeline that executes on a pull request. Errors must be addressed before the code is allowed to be committed. |
33+
| FAIL | The action has failed. Execution stops. Consider this like a build break in a CI/CD pipeline that executes on a pull request. Errors must be addressed before the code is allowed to be committed. |
3434

3535
## Installation
3636

@@ -66,7 +66,7 @@ Hookz uses a configuration file to generate hooks in your local git repository.
6666
### Example Configuration
6767

6868
``` yaml
69-
version: 2.4.3
69+
version: 2.4.4
7070
tools:
7171
- tool: github.com/devops-kung-fu/lucha@latest
7272
- tool: github.com/devops-kung-fu/hinge@latest
@@ -113,7 +113,7 @@ Quite often, downloadable binaries exist for multiple platforms when downloading
113113
You can use the following to retrieve the right architecture for [hinge](https://github.com/devops-kung-fu/hinge):
114114

115115
``` yaml
116-
version: 2.4.3
116+
version: 2.4.4
117117
hooks:
118118
- type: pre-commit
119119
actions:
@@ -128,12 +128,12 @@ If you are running ```Hookz``` on a Mac, this will bring down the ```hinge-0.1.0
128128

129129
You must have at least an URL, exec, or script defined in your actions. If you select one, then you shouldn't define the others in the YAML. Don't worry if you do, we have you covered and explain what happens in the following table.
130130

131-
|Attribute|Notes|
132-
|---|---|
133-
|```URL```|If this exists, then exec and script are ignored. The URL must be a link to an executable binary| If you are developing in go then use the ```sources``` node in your ```.hookz.yaml``` to define sources to be installed.
134-
|```exec```|If this exists then URL and script are ignored|
135-
|```script```|If this exists then URL, exec, and args are ignored|
136-
|```args```|Optional in all cases|
131+
| Attribute | Notes |
132+
| ------------ | ------------------------------------------------------------------------------------------------ |
133+
| ```URL``` | If this exists, then exec and script are ignored. The URL must be a link to an executable binary | If you are developing in go then use the ```sources``` node in your ```.hookz.yaml``` to define sources to be installed. |
134+
| ```exec``` | If this exists then URL and script are ignored |
135+
| ```script``` | If this exists then URL, exec, and args are ignored |
136+
| ```args``` | Optional in all cases |
137137

138138
### Inline scripting
139139

@@ -253,7 +253,7 @@ Check out the collection [here](tackle/README.md).
253253
Assumes `terraform` is in your `PATH` for `fmt`.
254254

255255
```yaml
256-
version: 2.4.3
256+
version: 2.4.4
257257
hooks:
258258
- type: pre-commit
259259
actions:
@@ -276,7 +276,7 @@ hooks:
276276
### NPM
277277

278278
```yaml
279-
version: 2.4.3
279+
version: 2.4.4
280280
hooks:
281281
- type: pre-commit
282282
actions:

cmd/common.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,38 @@ import (
44
"errors"
55
"fmt"
66
"log"
7-
"os"
87

98
"github.com/devops-kung-fu/common/util"
9+
"github.com/spf13/afero"
1010

1111
"github.com/devops-kung-fu/hookz/lib"
1212
)
1313

14-
func noConfig() {
14+
func NoConfig() {
1515
fmt.Println(".hookz.yaml file not found")
1616
fmt.Println("\nTo create a sample configuration run:")
1717
fmt.Println(" hookz init config")
1818
fmt.Println("\nRun 'hookz --help' for usage.")
1919
fmt.Println()
20-
os.Exit(1)
21-
}
22-
23-
func badYaml() {
24-
util.PrintErr(errors.New("configuration in .hookz.yaml is not valid YAML syntax"))
25-
os.Exit(1)
2620
}
2721

22+
// TODO: add code coverage
2823
// CheckConfig ensures that there is a .hookz.yaml file locally and the version is supported by the current version of hookz
29-
func CheckConfig() (config lib.Configuration) {
30-
config, err := lib.ReadConfig(Afs, version)
24+
func CheckConfig(afs *afero.Afero) (config lib.Configuration, err error) {
25+
config, err = lib.ReadConfig(Afs, version)
26+
var returnErr error
3127
if err != nil && err.Error() == "NO_CONFIG" {
32-
noConfig()
28+
returnErr = errors.New("NO_CONFIG")
3329
} else if err != nil && err.Error() == "BAD_YAML" {
34-
badYaml()
30+
returnErr = errors.New("configuration in .hookz.yaml is not valid YAML syntax")
3531
}
36-
return
32+
return config, returnErr
3733
}
3834

3935
// InstallSources installs all go repositories that are found in the Sources section of the .hookz.yaml file.
4036
func InstallSources(sources []lib.Source) (err error) {
4137
if len(sources) > 0 && Verbose {
42-
util.DoIf(Verbose, func() {
43-
util.PrintInfo("Installing sources...")
44-
})
38+
util.PrintInfo("Installing sources...")
4539
}
4640
for _, s := range sources {
4741
util.DoIf(Verbose, func() {

cmd/common_test.go

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cmd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/devops-kung-fu/common/util"
7+
"github.com/spf13/afero"
8+
"github.com/stretchr/testify/assert"
9+
10+
"github.com/devops-kung-fu/hookz/lib"
11+
)
12+
13+
func Test_InstallSources(t *testing.T) {
14+
sources := []lib.Source{
15+
{
16+
Source: "github.com/devops-kung-fu/hinge@latest",
17+
},
18+
}
19+
output := util.CaptureOutput(func() {
20+
_ = InstallSources(sources)
21+
})
22+
23+
assert.NotNil(t, output)
24+
assert.Contains(t, output, "go install github.com/devops-kung-fu/hinge@latest\n")
25+
26+
sources = []lib.Source{
27+
{
28+
Source: "yeah",
29+
},
30+
}
31+
output = util.CaptureOutput(func() {
32+
_ = InstallSources(sources)
33+
})
34+
assert.Contains(t, output, "exit status 1\n")
35+
}
36+
37+
func TestNoConfig(t *testing.T) {
38+
output := util.CaptureOutput(func() {
39+
NoConfig()
40+
})
41+
assert.NotNil(t, output)
42+
}
43+
44+
func TestCheckConfig(t *testing.T) {
45+
46+
afs := &afero.Afero{Fs: afero.NewMemMapFs()}
47+
48+
_, err := CheckConfig(afs)
49+
assert.Error(t, err, "There should be no config created so an error should be thrown.")
50+
assert.Equal(t, "NO_CONFIG", err.Error())
51+
52+
_ = afs.WriteFile(".hookz.yaml", []byte(""), 0644)
53+
_, err = CheckConfig(afs)
54+
assert.Error(t, err)
55+
56+
}

cmd/init.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ var (
3131
util.DoIf(Verbose, func() {
3232
util.PrintInfo("Creating hooks")
3333
})
34-
config := CheckConfig()
34+
config, err := CheckConfig(Afs)
35+
if err != nil {
36+
if err != nil && err.Error() == "NO_CONFIG" {
37+
NoConfig()
38+
} else {
39+
util.PrintErr(err)
40+
}
41+
os.Exit(1)
42+
}
3543
_ = InstallSources(config.Sources)
3644
if util.IsErrorBool(lib.WriteHooks(Afs, config, Verbose, VerboseOutput)) {
3745
return

cmd/reset.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cmd
22

33
import (
4+
"os"
5+
46
"github.com/devops-kung-fu/common/util"
57
"github.com/spf13/cobra"
68

@@ -19,7 +21,15 @@ var (
1921
if util.IsErrorBool(lib.RemoveHooks(Afs, Verbose)) {
2022
return
2123
}
22-
config := CheckConfig()
24+
config, err := CheckConfig(Afs)
25+
if err != nil {
26+
if err != nil && err.Error() == "NO_CONFIG" {
27+
NoConfig()
28+
} else {
29+
util.PrintErr(err)
30+
}
31+
os.Exit(1)
32+
}
2333
_ = InstallSources(config.Sources)
2434
if util.IsErrorBool(lib.WriteHooks(Afs, config, Verbose, VerboseOutput)) {
2535
return

cmd/root.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
var (
18-
version = "2.4.3"
18+
version = "2.4.4"
1919
//Afs stores a global OS Filesystem that is used throughout hookz
2020
Afs = &afero.Afero{Fs: afero.NewOsFs()}
2121
debug bool

0 commit comments

Comments
 (0)