Skip to content

Commit

Permalink
Add checks for tools required and add better logging around their fai…
Browse files Browse the repository at this point in the history
…lures (#5)
  • Loading branch information
tateexon authored Jul 31, 2024
1 parent 4b6974e commit 8739db1
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"strings"

Expand Down Expand Up @@ -38,6 +39,8 @@ func main() {
includeTestDeps := flag.Bool("t", true, "Should we include test dependencies. Default is true")
flag.Parse()

checkTools()

config := setConfig(branch, projectPath, excludes, levels, includeTestDeps)

goList, gitDiff, gitModDiff := makeExecCalls(config)
Expand Down Expand Up @@ -66,6 +69,22 @@ func setConfig(branch, projectPath, excludes *string, levels *int, includeTestDe
}
}

// checkTools check if the required tools are installed in the path
func checkTools() {
_, goErr := exec.LookPath("go")
_, gitErr := exec.LookPath("git")
toolsMissing := []string{}
if goErr != nil {
toolsMissing = append(toolsMissing, "go")
}
if gitErr != nil {
toolsMissing = append(toolsMissing, "git")
}
if len(toolsMissing) > 0 {
log.Fatalf("tools are missing from your path and may not be installed that are required to run this tool: %+v", toolsMissing)
}
}

func run(config *Config, goList, gitDiff, gitModDiff *cmd.Output) {
packages, err := golang.ParsePackages(goList.Stdout)
if err != nil {
Expand Down Expand Up @@ -125,15 +144,15 @@ func findAllAffectedPackages(config *Config, changedPackages, changedModPackages
func makeExecCalls(config *Config) (*cmd.Output, *cmd.Output, *cmd.Output) {
goList, err := golang.GoList()
if err != nil {
log.Fatalf("Error getting go list: %v", err)
log.Fatalf("Error getting go list: %v\nStdErr: %s", err, goList.Stderr.String())
}
gitDiff, err := git.Diff(config.Branch)
if err != nil {
log.Fatalf("Error getting the git diff: %v", err)
log.Fatalf("Error getting the git diff: %v\nStdErr: %s", err, gitDiff.Stderr.String())
}
gitModDiff, err := git.ModDiff(config.Branch, config.ProjectPath)
if err != nil {
log.Fatalf("Error getting the git mod diff")
log.Fatalf("Error getting the git mod diff: %v\nStdErr: %s", err, gitModDiff.Stderr.String())
}

return goList, gitDiff, gitModDiff
Expand Down

0 comments on commit 8739db1

Please sign in to comment.