Create a 2.0
This commit is contained in:
parent
dd6d9465f5
commit
2f0d9c5cfc
2 changed files with 35 additions and 13 deletions
46
main.go
46
main.go
|
@ -2,28 +2,39 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"flag"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Check for URL argument
|
// Define flags
|
||||||
if (len(os.Args) < 2 || os.Args[1] == "help") {
|
urlPtr := flag.String("url", "", "URL of the page to fetch (required)")
|
||||||
fmt.Println("Swirl 1.0")
|
methodPtr := flag.String("X", "GET", "HTTP method for the request (default: GET)")
|
||||||
fmt.Println("------------------")
|
outputPtr := flag.String("o", "", "Output file path (default: print to console)")
|
||||||
fmt.Println("Usage: swirl <url>")
|
followRedirectsPtr := flag.Bool("L", false, "Follow HTTP redirects (default: false)")
|
||||||
|
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
// Check for required flag (-url)
|
||||||
|
if *urlPtr == "" {
|
||||||
|
fmt.Println("Error: -url flag is required")
|
||||||
|
flag.PrintDefaults()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get URL from argument
|
|
||||||
url := os.Args[1]
|
|
||||||
|
|
||||||
// Create an HTTP client
|
// Create an HTTP client
|
||||||
client := &http.Client{}
|
client := &http.Client{
|
||||||
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
|
if *followRedirectsPtr { // Use the boolean variable
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return fmt.Errorf("too many redirects") // Or another error
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Create a new request
|
// Create a new request
|
||||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
req, err := http.NewRequest(*methodPtr, *urlPtr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("Error creating request:", err)
|
fmt.Println("Error creating request:", err)
|
||||||
return
|
return
|
||||||
|
@ -51,6 +62,17 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print the page content
|
// Handle output
|
||||||
|
if *outputPtr != "" {
|
||||||
|
// Write content to file
|
||||||
|
err = ioutil.WriteFile(*outputPtr, body, 0644)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("Error writing to file:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Println("Content saved to:", *outputPtr)
|
||||||
|
} else {
|
||||||
|
// Print content to console
|
||||||
fmt.Println(string(body))
|
fmt.Println(string(body))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
BIN
swirl
BIN
swirl
Binary file not shown.
Loading…
Reference in a new issue