Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

meg + gf = megf enhancement #53

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.mkd
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ Options:
-s, --savestatus <status> Save only responses with specific status code
-v, --verbose Verbose mode
-X, --method <method> HTTP method (default: GET)
-g, --savegf <gf entry> Save only if gf finds something

Defaults:
pathsFile: ./paths
Expand Down
8 changes: 8 additions & 0 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type config struct {
headers headerArgs
followLocation bool
method string
savegf string
saveStatus saveStatusArgs
timeout int
verbose bool
Expand Down Expand Up @@ -91,6 +92,11 @@ func processArgs() config {
flag.StringVar(&method, "method", "GET", "")
flag.StringVar(&method, "X", "GET", "")

// savegf param
savegf := ""
flag.StringVar(&savegf, "savegf", "", "")
flag.StringVar(&savegf, "g", "", "")

// savestatus params
var saveStatus saveStatusArgs
flag.Var(&saveStatus, "savestatus", "")
Expand Down Expand Up @@ -148,6 +154,7 @@ func processArgs() config {
headers: headers,
followLocation: followLocation,
method: method,
savegf: savegf,
saveStatus: saveStatus,
timeout: timeout,
requester: requesterFn,
Expand Down Expand Up @@ -177,6 +184,7 @@ func init() {
h += " -t, --timeout <millis> Set the HTTP timeout (default: 10000)\n"
h += " -v, --verbose Verbose mode\n"
h += " -X, --method <method> HTTP method (default: GET)\n\n"
h += " -g, --savegf <gf entry> Save only if gf finds something\n\n"

h += "Defaults:\n"
h += " pathsFile: ./paths\n"
Expand Down
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package main

import (
"bufio"
"bytes"
"fmt"
"net/url"
"os"
"os/exec"
"path/filepath"
"sync"
"time"
Expand Down Expand Up @@ -84,6 +86,7 @@ func main() {
owg.Add(1)
go func() {
for res := range responses {

if len(c.saveStatus) > 0 && !c.saveStatus.Includes(res.statusCode) {
continue
}
Expand All @@ -94,6 +97,25 @@ func main() {
}

path, err := res.save(c.output, c.noHeaders)
if len(c.savegf) > 0 {
var cmd *exec.Cmd
cmd = exec.Command("gf", c.savegf, path)
var out bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = os.Stderr
cmd.Run()
strOut := out.String()
if len(strOut) == 0 {
err := os.Remove(path)

if err != nil {
fmt.Println(err)
return
}
continue
}
}

if err != nil {
fmt.Fprintf(os.Stderr, "failed to save file: %s\n", err)
}
Expand Down