diff --git a/.gitignore b/.gitignore index 7f31071..d52c6e8 100644 --- a/.gitignore +++ b/.gitignore @@ -17,4 +17,4 @@ hookz coverage.out -.gitorade \ No newline at end of file +.gitorade.DS_Store diff --git a/.hookz.yaml b/.hookz.yaml index 6c75a04..7e4439c 100644 --- a/.hookz.yaml +++ b/.hookz.yaml @@ -23,7 +23,7 @@ args: ["-s", "-w", "**/*.go"] - name: "golint: Lint all go files" exec: golint - args: ["./..."] #to error out, add the arg "-set_exit_status" + args: ["-set_exit_status", "./..."] #to error out, add the arg "-set_exit_status" - name: "errcheck: Ensure that errors are checked" exec: errcheck args: ["-ignoretests", "./..."] @@ -47,4 +47,9 @@ args: ["app", "-json", "-output", "hookz-sbom.json"] - name: "git: Add all changed files during the pre-commit stage" exec: git - args: ["add", "."] \ No newline at end of file + args: ["add", "."] + - type: pre-push + actions: + - name: "hookz: example pre-push hook" + exec: /bin/echo + args: ["Pushing changes..."] \ No newline at end of file diff --git a/.vscode/configurationCache.log b/.vscode/configurationCache.log deleted file mode 100644 index 4da2f15..0000000 --- a/.vscode/configurationCache.log +++ /dev/null @@ -1 +0,0 @@ -{"buildTargets":[".PHONY","all","build","help","install","test","title"],"launchTargets":[],"customConfigurationProvider":{"workspaceBrowse":{"browsePath":[],"compilerArgs":[]},"fileIndex":[]}} \ No newline at end of file diff --git a/.vscode/dryrun.log b/.vscode/dryrun.log deleted file mode 100644 index b95f467..0000000 --- a/.vscode/dryrun.log +++ /dev/null @@ -1,5 +0,0 @@ -make --dry-run --always-make --keep-going --print-directory -make: Entering directory `/Users/dj/code/hookz' -awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $1, $2}' Makefile -make: Leaving directory `/Users/dj/code/hookz' - diff --git a/.vscode/targets.log b/.vscode/targets.log deleted file mode 100644 index cf79041..0000000 --- a/.vscode/targets.log +++ /dev/null @@ -1,287 +0,0 @@ -make all --print-data-base --no-builtin-variables --no-builtin-rules --question -# GNU Make 3.81 -# Copyright (C) 2006 Free Software Foundation, Inc. -# This is free software; see the source for copying conditions. -# There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. - -# This program built for i386-apple-darwin11.3.0 - - -# Make data base, printed on Mon May 23 08:31:31 2022 - -# Variables - -# automatic -/dev/null || echo /Developer)/Makefiles -# environment -VSCODE_CODE_CACHE_PATH = /Users/dj/Library/Application Support/Code/CachedData/c3511e6c69bb39013c4a4b7b9566ec1ca73fc4d5 -# environment -LOGNAME = dj -# environment -APPLICATION_INSIGHTS_NO_DIAGNOSTIC_CHANNEL = 1 -# environment -VSCODE_HANDLES_UNCAUGHT_ERRORS = true -# automatic -^D = $(patsubst %/,%,$(dir $^)) -# environment -XPC_FLAGS = 0x0 -# default -MAKE = $(MAKE_COMMAND) -# default -MAKECMDGOALS := all -# environment -SHLVL = 2 -# default -MAKE_VERSION := 3.81 -# environment -USER = dj -# environment -CR_PAT = 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 -# makefile (from `Makefile', line 9) -.DEFAULT_GOAL := help -# automatic -%D = $(patsubst %/,%,$(dir $%)) -# default -MAKE_COMMAND := /Library/Developer/CommandLineTools/usr/bin/make -# default -.VARIABLES := -# environment -TMPDIR = /var/folders/nm/ppzx3wrs5t5b4s92j5lzr5t00000gn/T/ -# automatic -*F = $(notdir $*) -# environment -VSCODE_IPC_HOOK = /Users/dj/Library/Application Support/Code/1.67.2-main.sock -# makefile -MAKEFLAGS = Rrqp -# environment -MFLAGS = -Rrqp -# automatic -*D = $(patsubst %/,%,$(dir $*)) -# environment -XPC_SERVICE_NAME = application.com.microsoft.VSCode.16094751.16094757 -# automatic -+D = $(patsubst %/,%,$(dir $+)) -# automatic -+F = $(notdir $+) -# environment -TEMP = /tmp -# environment -__CF_USER_TEXT_ENCODING = 0x1F5:0x0:0x0 -# environment -COMMAND_MODE = unix2003 -# default -MAKEFILES := -# automatic - 0 && Verbose { util.DoIf(Verbose, func() { util.PrintInfo("Installing sources...") }) - return } for _, s := range sources { util.DoIf(Verbose, func() { - util.PrintTabbedf("Installing Source: %s", s.Source) + util.PrintTabbedf("Installing Source: %s\n", s.Source) }) err = lib.InstallSource(s) if err != nil { diff --git a/cmd/init_config.go b/cmd/init_config.go index 99f3c70..24a498d 100644 --- a/cmd/init_config.go +++ b/cmd/init_config.go @@ -25,12 +25,16 @@ var ( } }, Run: func(cmd *cobra.Command, args []string) { - util.PrintInfo("Creating Sample Config") + util.DoIf(Verbose, func() { + util.PrintInfo("Creating Sample Config") + }) _, err := lib.CreateConfig(Afs, version) if util.IsErrorBool(err) { return } - util.PrintSuccess("Done") + util.DoIf(Verbose, func() { + util.PrintSuccess("Done") + }) }, } ) diff --git a/cmd/reset.go b/cmd/reset.go index 48b7fe6..4988a6a 100644 --- a/cmd/reset.go +++ b/cmd/reset.go @@ -1,8 +1,6 @@ package cmd import ( - "fmt" - "github.com/devops-kung-fu/common/util" "github.com/spf13/cobra" @@ -15,9 +13,9 @@ var ( Short: "Rebuilds the hooks as defined in the .hookz.yaml file.", Long: "Rebuilds the hooks as defined in the .hookz.yaml file.", Run: func(cmd *cobra.Command, args []string) { - util.PrintInfo("Resetting Hooks") - fmt.Println() - + util.DoIf(Verbose, func() { + util.PrintInfo("Resetting Hooks") + }) if util.IsErrorBool(lib.RemoveHooks(Afs, Verbose)) { return } @@ -26,7 +24,9 @@ var ( if util.IsErrorBool(lib.WriteHooks(Afs, config, Verbose, VerboseOutput)) { return } - util.PrintSuccess("Done") + util.DoIf(Verbose, func() { + util.PrintSuccess("Done") + }) }, } ) diff --git a/cmd/root.go b/cmd/root.go index b3abf7d..9e8dcd4 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -15,10 +15,13 @@ import ( ) var ( - version = "2.4.0" - Afs = &afero.Afero{Fs: afero.NewOsFs()} - debug bool - Verbose bool + version = "2.4.0" + //Afs stores a global OS Filesystem that is used throughout hookz + Afs = &afero.Afero{Fs: afero.NewOsFs()} + debug bool + //Verbose determines if the execution of hookz should output verbose information + Verbose bool + //VerboseOutput is set to true if you wish to see debug information in the hooks as they execute in bash VerboseOutput bool rootCmd = &cobra.Command{ Use: "hookz", diff --git a/cmd/update.go b/cmd/update.go index bcef9cc..d7dfb44 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -13,13 +13,18 @@ var ( Short: "Updates any defined sources and/or executable defined as an URL attribute in .hookz.yaml.", Long: "Updates any defined sources and/or executable defined as an URL attribute in .hookz.yaml.", Run: func(cmd *cobra.Command, args []string) { - util.PrintInfo("Updating sources and executables") + util.DoIf(Verbose, func() { + util.PrintInfo("Updating sources and executables") + }) config := CheckConfig() _ = InstallSources(config.Sources) - if util.IsErrorBool(lib.UpdateExecutables(Afs, config)) { - return - } - util.PrintSuccess("Done!") + updateCount, _ := lib.UpdateExecutables(Afs, config) + util.DoIf(updateCount == 0, func() { + util.PrintInfo("Nothing to Update!") + }) + util.DoIf(Verbose, func() { + util.PrintSuccess("Done") + }) }, } ) diff --git a/go.mod b/go.mod index 9deca77..e1e9238 100644 --- a/go.mod +++ b/go.mod @@ -15,7 +15,7 @@ require ( require ( github.com/davecgh/go-spew v1.1.1 // indirect - github.com/devops-kung-fu/common v0.2.0 + github.com/devops-kung-fu/common v0.2.1 github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/spf13/pflag v1.0.5 // indirect diff --git a/go.sum b/go.sum index ea16c05..1a6c271 100644 --- a/go.sum +++ b/go.sum @@ -50,8 +50,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/devops-kung-fu/common v0.2.0 h1:a9Gk1caowxArQdNgwxgLxocp8PRvThu+HzT6/mbabq4= -github.com/devops-kung-fu/common v0.2.0/go.mod h1:1eupDyY9wxRUGpb/d5kdEHO2fVLGnoIPDP7pIBhyX/g= +github.com/devops-kung-fu/common v0.2.1 h1:Wy3bW2O269CWdCdsJlrOvRAqInd/vR9LPISFR8U3NX0= +github.com/devops-kung-fu/common v0.2.1/go.mod h1:1eupDyY9wxRUGpb/d5kdEHO2fVLGnoIPDP7pIBhyX/g= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= diff --git a/hookz-sbom.json b/hookz-sbom.json index c4e3721..eebf2a4 100644 --- a/hookz-sbom.json +++ b/hookz-sbom.json @@ -1,10 +1,10 @@ { "bomFormat": "CycloneDX", "specVersion": "1.4", - "serialNumber": "urn:uuid:1bf4aa8c-b369-4fb2-9523-019e9555dd41", + "serialNumber": "urn:uuid:b7d96642-a2e1-4476-9291-1cf1b5ec1797", "version": 1, "metadata": { - "timestamp": "2022-05-23T08:48:15-06:00", + "timestamp": "2022-05-31T11:36:07-06:00", "tools": [ { "vendor": "CycloneDX", @@ -78,18 +78,18 @@ }, "components": [ { - "bom-ref": "pkg:golang/github.com/devops-kung-fu/common@v0.2.0?type=module", + "bom-ref": "pkg:golang/github.com/devops-kung-fu/common@v0.2.1?type=module", "type": "library", "name": "github.com/devops-kung-fu/common", - "version": "v0.2.0", + "version": "v0.2.1", "scope": "required", "hashes": [ { "alg": "SHA-256", - "content": "6bd1a4d5c6a8c3102b41d360c3180bc68729f0f46f4e1bbe1f34fafe66da6eae" + "content": "5b2ddb5b63b6ebd09674276c265acebd102a22777fbd1f4b3c848547c537357d" } ], - "purl": "pkg:golang/github.com/devops-kung-fu/common@v0.2.0?type=module", + "purl": "pkg:golang/github.com/devops-kung-fu/common@v0.2.1?type=module", "externalReferences": [ { "url": "https://github.com/devops-kung-fu/common", @@ -276,7 +276,7 @@ { "ref": "pkg:golang/github.com/devops-kung-fu/hookz@v2.3.1-0.20210706231702-1c879079cb03?type=module", "dependsOn": [ - "pkg:golang/github.com/devops-kung-fu/common@v0.2.0?type=module", + "pkg:golang/github.com/devops-kung-fu/common@v0.2.1?type=module", "pkg:golang/github.com/dustin/go-humanize@v1.0.0?type=module", "pkg:golang/github.com/gookit/color@v1.5.0?type=module", "pkg:golang/github.com/segmentio/ksuid@v1.0.4?type=module", @@ -286,7 +286,7 @@ ] }, { - "ref": "pkg:golang/github.com/devops-kung-fu/common@v0.2.0?type=module", + "ref": "pkg:golang/github.com/devops-kung-fu/common@v0.2.1?type=module", "dependsOn": [ "pkg:golang/github.com/gookit/color@v1.5.0?type=module", "pkg:golang/github.com/xo/terminfo@v0.0.0-20210125001918-ca9a967f8778?type=module" diff --git a/img/run-hookz.png b/img/run-hookz.png index deeba64..7db22db 100644 Binary files a/img/run-hookz.png and b/img/run-hookz.png differ diff --git a/lib/configvalidator_test.go b/lib/configvalidator_test.go index 516a2cc..f460c1f 100644 --- a/lib/configvalidator_test.go +++ b/lib/configvalidator_test.go @@ -15,7 +15,7 @@ func Test_generateShasum(t *testing.T) { shasum, err := generateShasum(afs) assert.NoError(t, err, "Likely .hookz.yaml couldn't be read") - assert.Equal(t, "0213f04ee70cc7b48d6de58e7dd62338259d16ae8e52016d19f83559051dd57c", shasum, "shasums do not match, but should") + assert.Equal(t, "d6e393b32ffa1a804b705d0a60acedd9c983a6d2e01cd1871a2e75ec358a5c20", shasum, "shasums do not match, but should") } func Test_WriteShasum(t *testing.T) { @@ -31,7 +31,7 @@ func Test_WriteShasum(t *testing.T) { exists, _ := afs.Exists(filename) assert.True(t, exists) - contains, _ := afs.FileContainsBytes(filename, []byte("0213f04ee70cc7b48d6de58e7dd62338259d16ae8e52016d19f83559051dd57c")) + contains, _ := afs.FileContainsBytes(filename, []byte("d6e393b32ffa1a804b705d0a60acedd9c983a6d2e01cd1871a2e75ec358a5c20")) assert.True(t, contains, "The expected shasum was not written to the hookz.shasum file") } diff --git a/lib/configwriter.go b/lib/configwriter.go index bea10ec..c3f39b8 100644 --- a/lib/configwriter.go +++ b/lib/configwriter.go @@ -9,7 +9,7 @@ import ( //CreateConfig creates a starter .hookz.yaml file func CreateConfig(afs *afero.Afero, version string) (config Configuration, err error) { - command := "echo" + command := "/bin/echo" config = Configuration{ Version: version, Sources: []Source{ diff --git a/lib/hookdeleter.go b/lib/hookdeleter.go index cf89f18..cbae615 100644 --- a/lib/hookdeleter.go +++ b/lib/hookdeleter.go @@ -40,7 +40,7 @@ func RemoveHooks(afs *afero.Afero, verbose bool) (err error) { } parts := strings.Split(hookName, "/") util.DoIf(verbose, func() { - util.PrintTabbedf("Deleted %s", parts[len(parts)-1]) + util.PrintTabbedf("Deleted %s\n", parts[len(parts)-1]) }) } } diff --git a/lib/hookwriter.go b/lib/hookwriter.go index 5895d8c..cebc483 100644 --- a/lib/hookwriter.go +++ b/lib/hookwriter.go @@ -90,7 +90,7 @@ func WriteHooks(afs *afero.Afero, config Configuration, verbose bool, verboseOut for _, hook := range config.Hooks { var commands []command util.DoIf(verbose, func() { - util.PrintInfof("Writing %s", hook.Type) + util.PrintInfof("Writing %s\n", hook.Type) }) for _, action := range hook.Actions { @@ -99,7 +99,7 @@ func WriteHooks(afs *afero.Afero, config Configuration, verbose bool, verboseOut return err } util.DoIf(verbose, func() { - util.PrintTabbedf("Adding %s action: %s", hook.Type, action.Name) + util.PrintTabbedf("Adding %s action: %s\n", hook.Type, action.Name) }) fullCommand := buildFullCommand(action, verboseOutput) @@ -202,9 +202,10 @@ func genTemplate(hookType string) (t *template.Template) { # This file was generated by Hookz # For more information, check out https://github.com/devops-kung-fu/hookz -echo -e "$(tput bold)Hookz$(tput sgr0)" +echo -e "\n$(tput bold)Hookz$(tput sgr0)" +echo -e "DKFM - DevOps Kung Fu Mafia" echo -e "https://github.com/devops-kung-fu/hookz" -echo -e "Version: 2.3.0" +echo -e "Version: 2.4.0" echo shasum=$(cat .git/hooks/hookz.shasum) diff --git a/lib/hookwriter_test.go b/lib/hookwriter_test.go index dc74484..db1fcfd 100644 --- a/lib/hookwriter_test.go +++ b/lib/hookwriter_test.go @@ -39,7 +39,7 @@ func Test_buildFullCommand(t *testing.T) { assert.NotNil(t, action, "Action should not be nil") command := buildFullCommand(action, true) - assert.Equal(t, "echo -e Hello Hookz!", command, "Values are not equal") + assert.Equal(t, "/bin/echo -e Hello Hookz!", command, "Values are not equal") } func Test_WriteHooks(t *testing.T) { diff --git a/lib/sourcerunner.go b/lib/sourcerunner.go index da8393a..aa6fac5 100644 --- a/lib/sourcerunner.go +++ b/lib/sourcerunner.go @@ -5,11 +5,10 @@ import ( "os/exec" ) +//InstallSource installs a go repository that is found in the Sources section of the .hookz.yaml file. func InstallSource(source Source) (err error) { - - log.Printf("installing: %s", source.Source) cmd := exec.Command("go", "install", source.Source) - + log.Println(cmd.String()) err = cmd.Run() if err != nil { log.Print(err) diff --git a/lib/sourcerunner_test.go b/lib/sourcerunner_test.go index 09df00a..1346d05 100644 --- a/lib/sourcerunner_test.go +++ b/lib/sourcerunner_test.go @@ -18,7 +18,7 @@ func TestInstallSources(t *testing.T) { }) assert.NotNil(t, output) - assert.Contains(t, output, "installing: github.com/devops-kung-fu/hinge@latest\n") + assert.Contains(t, output, "go install github.com/devops-kung-fu/hinge@latest\n") sources = []Source{ { diff --git a/lib/structs.go b/lib/structs.go index d783a6a..5f42d45 100644 --- a/lib/structs.go +++ b/lib/structs.go @@ -24,6 +24,7 @@ type Action struct { Script *string `json:"script,omitempty"` } +//Source defines a go repository that should be installed when hookz is initializing, updating, or resetting type Source struct { - Source string `json:"source"` + Source string `json:"source,omitempty"` } diff --git a/lib/web.go b/lib/web.go index 78b75e3..8ee97e8 100644 --- a/lib/web.go +++ b/lib/web.go @@ -11,7 +11,6 @@ import ( "runtime" "strings" - "github.com/devops-kung-fu/common/util" "github.com/dustin/go-humanize" "github.com/spf13/afero" ) @@ -33,13 +32,12 @@ func (wc *WriteCounter) Write(p []byte) (int, error) { //PrintProgress prints the current download progress to STDOUT func (wc WriteCounter) PrintProgress() { fmt.Printf("\r%s", strings.Repeat(" ", 35)) - fmt.Printf("\r Downloading %s... %s complete", wc.FileName, humanize.Bytes(wc.Total)) + fmt.Printf("\r Downloading %s... %s complete", wc.FileName, humanize.Bytes(wc.Total)) } //UpdateExecutables parses the configuration for URL's and re-downloads //the contents into the .git/hooks folder -func UpdateExecutables(afs *afero.Afero, config Configuration) (err error) { - var updateCount = 0 +func UpdateExecutables(afs *afero.Afero, config Configuration) (updateCount int, err error) { for _, hook := range config.Hooks { for _, action := range hook.Actions { if action.URL != nil { @@ -48,10 +46,6 @@ func UpdateExecutables(afs *afero.Afero, config Configuration) (err error) { } } } - if updateCount == 0 { - util.PrintInfo("Nothing to Update!") - } - return } diff --git a/lib/web_test.go b/lib/web_test.go index 782b5a0..1620023 100644 --- a/lib/web_test.go +++ b/lib/web_test.go @@ -24,7 +24,7 @@ func TestUpdateExecutables(t *testing.T) { }, }, } - err := UpdateExecutables(afs, botchedConfig) + _, err := UpdateExecutables(afs, botchedConfig) assert.NoError(t, err, "UpdateExecutables should only happen if action.URL != nil") }