refactor CLI logic and file input support
This commit is contained in:
parent
345af74bd1
commit
8079af3beb
45
sparte.go
45
sparte.go
|
@ -90,28 +90,51 @@ func SpartanParsedURL(u url.URL, input string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func Usage() {
|
||||
fmt.Fprintln(os.Stderr, `
|
||||
Usage: sparte [--help] [URL] [-i INPUT] [-f FILE]
|
||||
|
||||
--help usage
|
||||
-i send INPUT to URL
|
||||
-f send contents of FILE as input to UR
|
||||
|
||||
`)
|
||||
return
|
||||
}
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if len(args) == 0 {
|
||||
fmt.Println("do --help for help")
|
||||
Usage()
|
||||
return
|
||||
}
|
||||
url := args[0]
|
||||
input := ""
|
||||
for i, arg := range args {
|
||||
if arg == "--help" {
|
||||
fmt.Println("Usage: <program> [--help] [URL]")
|
||||
Usage()
|
||||
return
|
||||
}
|
||||
if arg == "-i" {
|
||||
if len(args) < i+2 {
|
||||
fmt.Println("argument needed for -i")
|
||||
if arg == "-i" || arg == "-f" {
|
||||
if len(args) < i+2 || (i==0 && len(args) < i+3) {
|
||||
fmt.Println("input arument or URL missing")
|
||||
return
|
||||
}
|
||||
if i == 0 {
|
||||
fmt.Println("try this format instead: <program> URL -i INPUT")
|
||||
return
|
||||
url = args[i-1]
|
||||
if arg == "-i" {
|
||||
input = args[i+1]
|
||||
break
|
||||
}
|
||||
SpartanURL(args[i-1], args[i+1])
|
||||
return
|
||||
// arg must be -f
|
||||
inputBytes, err := ioutil.ReadFile(args[i+1])
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "Error reading file")
|
||||
os.Exit(1)
|
||||
}
|
||||
input = string(inputBytes)
|
||||
break
|
||||
// ignoring other args if any
|
||||
}
|
||||
}
|
||||
SpartanURL(args[0], "")
|
||||
SpartanURL(url, input)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue