From a9c5d5a95367c75d3862296b9eca7472563cc6fb Mon Sep 17 00:00:00 2001 From: Anton Postov Date: Sat, 17 Feb 2018 10:44:30 +0300 Subject: [PATCH] Code cleanup and badges --- README.md | 3 +++ checks.go | 10 ++++------ leaks.go | 15 +++++++++------ leaks_test.go | 8 ++++---- main.go | 14 ++++++++++---- options.go | 13 ++----------- 6 files changed, 32 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 3414bd1d..4da763f1 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # GitLeaks +[![godoc](https://godoc.org/github.com/zricethezav/gitleaks?status.svg)](http://godoc.org/github.com/zricethezav/gitleaks) +[![GolangCI](https://golangci.com/badges/github.com/zricethezav/gitleaks.svg)](https://golangci.com) + ## Check git repos for secrets and keys ### Features diff --git a/checks.go b/checks.go index 338b8f11..de25a3eb 100644 --- a/checks.go +++ b/checks.go @@ -1,8 +1,9 @@ package main import ( - "github.com/nbutton23/zxcvbn-go" "strings" + + "github.com/nbutton23/zxcvbn-go" ) // check each line of a diff and see if there are any potential secrets @@ -38,7 +39,7 @@ func checkEntropy(target string) bool { } // TODO check for stop words here - target = strings.Trim(target[index[1]:len(target)], " ") + target = strings.Trim(target[index[1]:], " ") if len(target) > 70 { return false @@ -47,8 +48,5 @@ func checkEntropy(target string) bool { entropy := zxcvbn.PasswordStrength(target, nil).Entropy // tune this/make option - if entropy > 70 { - return true - } - return false + return entropy > 70 } diff --git a/leaks.go b/leaks.go index 6bdd5a82..135b9033 100644 --- a/leaks.go +++ b/leaks.go @@ -19,16 +19,16 @@ type LeakElem struct { Commit string `json:"commit"` } -func start(opts *Options, repoUrl string) { +func start(_ *Options, repoURL string) { c := make(chan os.Signal, 2) signal.Notify(c, os.Interrupt, syscall.SIGTERM) - err := exec.Command("git", "clone", repoUrl).Run() + err := exec.Command("git", "clone", repoURL).Run() if err != nil { log.Fatalf("failed to clone repo %v", err) } - repoName := getLocalRepoName(repoUrl) - if err := os.Chdir(repoName); err != nil { + repoName := getLocalRepoName(repoURL) + if err = os.Chdir(repoName); err != nil { log.Fatal(err) } go func() { @@ -39,8 +39,11 @@ func start(opts *Options, repoUrl string) { report := getLeaks(repoName) cleanup(repoName) - reportJson, _ := json.MarshalIndent(report, "", "\t") - err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJson, 0644) + reportJSON, _ := json.MarshalIndent(report, "", "\t") + err = ioutil.WriteFile(fmt.Sprintf("%s_leaks.json", repoName), reportJSON, 0644) + if err != nil { + log.Fatalf("Can't write to file: %s", err) + } } // getLocalRepoName generates the name of the local clone folder based on the given URL diff --git a/leaks_test.go b/leaks_test.go index 767d2e88..feedffcf 100644 --- a/leaks_test.go +++ b/leaks_test.go @@ -3,9 +3,9 @@ package main import "testing" func TestGetLocalRepoName(t *testing.T) { - cases := []struct{ - name string - input string + cases := []struct { + name string + input string expected string }{ { @@ -51,4 +51,4 @@ func TestGetLocalRepoName(t *testing.T) { t.Errorf("'%s' failed. Input: '%s'; Expected: '%s'; Got: '%s'", c.input, c.name, c.expected, actual) } } -} \ No newline at end of file +} diff --git a/main.go b/main.go index ceef175c..07cf468e 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "log" "os" "regexp" ) @@ -12,7 +13,12 @@ var ( ) func init() { - appRoot, _ = os.Getwd() + var err error + appRoot, err = os.Getwd() + if err != nil { + log.Fatalf("Can't get working dir: %s", err) + } + // TODO update regex to look for things like: // TODO ability to add/filter regex // client("AKAI32fJ334...", @@ -30,7 +36,7 @@ func init() { func main() { args := os.Args[2:] - repoUrl := os.Args[1] - opts := parseOptions(args, repoUrl) - start(opts, repoUrl) + repoURL := os.Args[1] + opts := parseOptions(args) + start(opts, repoURL) } diff --git a/options.go b/options.go index 441dabde..5922871f 100644 --- a/options.go +++ b/options.go @@ -10,7 +10,7 @@ import ( // hittps://github.com/ // hittps://github.com//repo const usage = `usage: gitleaks [git link] [options] - + Options: -c Concurrency factor (potential number of git files open) -h --help Display this message @@ -39,16 +39,7 @@ func optionsNextInt(args []string, i *int) int { return argInt } -func optionsNextString(args []string, i *int) string { - if len(args) > *i+1 { - *i++ - } else { - help() - } - return args[*i] -} - -func parseOptions(args []string, repoUrl string) *Options { +func parseOptions(args []string) *Options { opts := &Options{} for i := 0; i < len(args); i++ { arg := args[i]