Skip to content

Commit 7cf723c

Browse files
committed
Tidier error reporting
1 parent b2ffe1a commit 7cf723c

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

cmd/gitjacker/main.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"fmt"
56
"io/ioutil"
67
"net/url"
@@ -36,7 +37,7 @@ var rootCmd = &cobra.Command{
3637
Long: `Gitjacker steals git repositories from websites which mistakenly host the contents of the .git directory.
3738
More information at https://github.com/liamg/gitjacker`,
3839
Args: cobra.ExactArgs(1),
39-
RunE: func(cmd *cobra.Command, args []string) error {
40+
Run: func(cmd *cobra.Command, args []string) {
4041

4142
_ = tml.Printf(`<red>
4243
██████ ██ ████████ ██ █████ ██████ ██ ██ ███████ ██████
@@ -53,23 +54,23 @@ https://github.com/liamg/gitjacker %9s
5354

5455
u, err := url.Parse(rawURL)
5556
if err != nil {
56-
return fmt.Errorf("invalid url: %w", err)
57+
fail("Invalid url: %s", err)
5758
}
5859

5960
if !u.IsAbs() {
60-
return fmt.Errorf("invalid url: must be absolute")
61+
fail("Invalid url: must be absolute e.g. https://victim.website/")
6162
}
6263

6364
if outputDir == "" {
6465
outputDir, err = ioutil.TempDir(os.TempDir(), "gitjacker")
6566
if err != nil {
66-
return err
67+
fail("Failed to create temporary directory: %s", err)
6768
}
6869
}
6970

7071
versionData, err := exec.Command("git", "--version").Output()
7172
if err != nil {
72-
return fmt.Errorf("failed to start git: %w - please check it is installed", err)
73+
fail("Cannot check git version: %s - please check it is installed", err)
7374
}
7475
versionParts := strings.Split(string(versionData), " ")
7576
version := strings.TrimSpace(versionParts[len(versionParts)-1])
@@ -78,7 +79,7 @@ https://github.com/liamg/gitjacker %9s
7879
logrus.SetLevel(logrus.DebugLevel)
7980
}
8081

81-
tml.Printf(`
82+
_ = tml.Printf(`
8283
Target: <yellow>%s</yellow>
8384
Local Git: %s
8485
Output Dir: %s
@@ -93,7 +94,10 @@ Output Dir: %s
9394
if !verbose {
9495
fmt.Printf("\x1b[2K\r")
9596
}
96-
return err
97+
if errors.Is(err, gitjacker.ErrNotVulnerable) {
98+
fail("The provided URL does not appear vulnerable.\n\nError: %s", err)
99+
}
100+
fail("Gitjacking failed: %s", err)
97101
}
98102

99103
if !verbose {
@@ -139,7 +143,10 @@ You can find the retrieved repository data in <blue>%s</blue>
139143
branchStr,
140144
summary.OutputDirectory,
141145
)
142-
143-
return nil
144146
},
145147
}
148+
149+
func fail(format string, args ...interface{}) {
150+
_, _ = fmt.Fprintln(os.Stderr, tml.Sprintf("<red>%s", fmt.Sprintf(format, args...)))
151+
os.Exit(1)
152+
}

internal/pkg/gitjacker/retriever.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,12 @@ func (r *retriever) Run() (*Summary, error) {
375375
// grab packed files
376376
if err := r.handlePackFiles(); err == ErrNoPackInfo {
377377
r.summary.PackInformationAvailable = false
378-
logrus.Debug("Pack information file is not available - some objects may be missing.")
378+
logrus.Debugf("Pack information file is not available - some objects may be missing.")
379379
} else if err == nil {
380380
r.summary.PackInformationAvailable = true
381381
} else { // if there's a different error, ignore it, we can continue without unpacking
382382
r.summary.PackInformationAvailable = true
383-
logrus.Debug("Error in unpack operation: %s", err)
383+
logrus.Debugf("Error in unpack operation: %s", err)
384384
}
385385
if len(r.summary.FoundObjects) == 0 {
386386
r.summary.Status = StatusFailure

internal/pkg/gitjacker/retriever_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestRetrieval(t *testing.T) {
112112
}
113113
}()
114114

115-
if err := New(target, outputDir).Run(); err != nil {
115+
if _, err := New(target, outputDir).Run(); err != nil {
116116
t.Fatal(err)
117117
}
118118

0 commit comments

Comments
 (0)