refactor CLI logic and file input support

This commit is contained in:
Hedy Li 2021-07-09 14:50:24 +08:00
parent 345af74bd1
commit 8079af3beb
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
1 changed files with 34 additions and 11 deletions

View File

@ -90,28 +90,51 @@ func SpartanParsedURL(u url.URL, input string) bool {
return true 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() { func main() {
args := os.Args[1:] args := os.Args[1:]
if len(args) == 0 { if len(args) == 0 {
fmt.Println("do --help for help") Usage()
return
} }
url := args[0]
input := ""
for i, arg := range args { for i, arg := range args {
if arg == "--help" { if arg == "--help" {
fmt.Println("Usage: <program> [--help] [URL]") Usage()
return return
} }
if arg == "-i" { if arg == "-i" || arg == "-f" {
if len(args) < i+2 { if len(args) < i+2 || (i==0 && len(args) < i+3) {
fmt.Println("argument needed for -i") fmt.Println("input arument or URL missing")
return return
} }
if i == 0 { url = args[i-1]
fmt.Println("try this format instead: <program> URL -i INPUT") if arg == "-i" {
return input = args[i+1]
break
} }
SpartanURL(args[i-1], args[i+1]) // arg must be -f
return 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)
} }