This commit is contained in:
Colin 2024-05-22 23:33:13 -04:00
commit 688439b504
22 changed files with 1499 additions and 0 deletions

70
build.sh Executable file
View File

@ -0,0 +1,70 @@
#!/bin/bash
# Default architecture
DEFAULT_ARCH="linux/amd64"
# Supported architectures (adjust as needed)
ARCHITECTURES=("linux/amd64" "linux/arm64" "linux/arm/v7" "darwin/amd64" "darwin/arm64")
# Ensure all necessary directories exist and go modules are ready
prepare_build() {
# Create necessary directories if they don't exist
mkdir -p dist
mkdir -p build_logs
# Initialize go modules if go.mod does not exist
if [ ! -f go.mod ]; then
echo "Initializing Go modules"
go mod init yourmodule # Replace 'yourmodule' with your actual module name or path
fi
# Fetch and ensure all dependencies are up to date
echo "Checking dependencies..."
go mod tidy
}
# Build function
build_binary() {
os=$1
arch=$2
output_name="sitemapper"
if [[ "$os/$arch" != "$DEFAULT_ARCH" ]]; then
output_name="${output_name}_${os}_${arch}"
fi
output_name="dist/${output_name}"
# Dynamic Linking
echo "Building dynamically linked for ${os}/${arch} -> ${output_name}"
GOOS=${os} GOARCH=${arch} go build -o ${output_name} main.go 2>build_logs/${os}_${arch}_build.log
if [ $? -eq 0 ]; then
echo "Successfully built ${output_name}"
else
echo "Failed to build ${output_name}. Check build_logs/${os}_${arch}_build.log for errors."
fi
# Static Linking
if [[ "$os" == "linux" ]]; then # Typically, static linking is most relevant for Linux environments
static_output_name="${output_name}_static"
echo "Building statically linked for ${os}/${arch} -> ${static_output_name}"
CGO_ENABLED=0 GOOS=${os} GOARCH=${arch} go build -a -ldflags '-extldflags "-static"' -o ${static_output_name} main.go 2>build_logs/${os}_${arch}_static_build.log
if [ $? -eq 0 ]; then
echo "Successfully built ${static_output_name}"
else
echo "Failed to build ${static_output_name}. Check build_logs/${os}_${arch}_static_build.log for errors."
fi
fi
}
# Main Build Process
prepare_build
for arch in "${ARCHITECTURES[@]}"; do
IFS='/' read -r -a parts <<< "$arch" # Split architecture string
os=${parts[0]}
arch=${parts[1]}
build_binary $os $arch
done
echo "Build process completed."

View File

View File

View File

View File

View File

View File

View File

View File

BIN
dist/sitemapper vendored Executable file

Binary file not shown.

BIN
dist/sitemapper_darwin_amd64 vendored Executable file

Binary file not shown.

BIN
dist/sitemapper_darwin_arm64 vendored Executable file

Binary file not shown.

BIN
dist/sitemapper_linux_arm vendored Executable file

Binary file not shown.

BIN
dist/sitemapper_linux_arm64 vendored Executable file

Binary file not shown.

BIN
dist/sitemapper_linux_arm64_static vendored Executable file

Binary file not shown.

BIN
dist/sitemapper_linux_arm_static vendored Executable file

Binary file not shown.

BIN
dist/sitemapper_static vendored Executable file

Binary file not shown.

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module yourmodule
go 1.21.1

152
main.go Normal file
View File

@ -0,0 +1,152 @@
package main
import (
"crypto/tls"
"encoding/csv"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"strings"
)
// Sitemap represents the structure of the XML sitemap
type Sitemap struct {
URLs []URL `xml:"url"`
}
// URL represents a single URL entry in the sitemap
type URL struct {
Loc string `xml:"loc"`
}
func main() {
// Define command line flags
csvOutput := flag.Bool("csv", false, "Output URLs as CSV to sitemap.csv")
sitemapURL := flag.String("url", "", "URL of the sitemap")
flag.Parse()
// List of known paths for sitemap.xml
knownPaths := []string{
"/sitemap.xml",
"/sitemap_index.xml",
"/sitemap/sitemap.xml",
"/sitemap/sitemap-index.xml",
}
// If no URL is provided, check common paths
if *sitemapURL == "" {
fmt.Println("No URL provided, checking common paths for sitemap.xml")
baseURL := "https://example.com" // Replace with your base URL if needed
for _, p := range knownPaths {
u, err := url.Parse(baseURL)
if err != nil {
fmt.Printf("Invalid base URL: %v\n", err)
return
}
u.Path = path.Join(u.Path, p)
if checkURL(u.String()) {
*sitemapURL = u.String()
break
}
}
if *sitemapURL == "" {
fmt.Println("Sitemap not found in common paths")
return
}
}
// Create an HTTP client with TLS certificate verification disabled
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
// Fetch the sitemap XML
resp, err := client.Get(*sitemapURL)
if err != nil {
fmt.Printf("Failed to fetch sitemap: %v\n", err)
return
}
defer resp.Body.Close()
// Read the XML response
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response body: %v\n", err)
return
}
// Attempt to parse the XML data
var sitemap Sitemap
err = xml.Unmarshal(data, &sitemap)
if err != nil {
fmt.Printf("Failed to parse XML: %v\n", err)
fmt.Println("Here is the problematic XML snippet:")
lines := strings.Split(string(data), "\n")
for i, line := range lines {
if i >= 43 && i <= 45 { // Print lines around the reported error line (44)
fmt.Printf("%d: %s\n", i+1, line)
}
}
return
}
// Extract URLs
urls := make([]string, len(sitemap.URLs))
for i, url := range sitemap.URLs {
urls[i] = url.Loc
}
if *csvOutput {
// Output URLs to a CSV file
err := writeURLsToCSV("sitemap.csv", urls)
if err != nil {
fmt.Printf("Failed to write CSV: %v\n", err)
return
}
fmt.Println("URLs written to sitemap.csv")
} else {
// Print URLs as a raw comma-separated string
fmt.Println(strings.Join(urls, ","))
}
}
// checkURL checks if a given URL is accessible
func checkURL(sitemapURL string) bool {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
resp, err := client.Head(sitemapURL)
if err != nil {
return false
}
return resp.StatusCode == http.StatusOK
}
// writeURLsToCSV writes a slice of URLs to a CSV file
func writeURLsToCSV(filename string, urls []string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
for _, url := range urls {
if err := writer.Write([]string{url}); err != nil {
return err
}
}
return nil
}

145
main.working Normal file
View File

@ -0,0 +1,145 @@
package main
import (
"crypto/tls"
"encoding/csv"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"strings"
)
// Sitemap represents the structure of the XML sitemap
type Sitemap struct {
URLs []URL `xml:"url"`
}
// URL represents a single URL entry in the sitemap
type URL struct {
Loc string `xml:"loc"`
}
func main() {
// Define command line flags
csvOutput := flag.Bool("csv", false, "Output URLs as CSV to sitemap.csv")
sitemapURL := flag.String("url", "", "URL of the sitemap")
flag.Parse()
// List of known paths for sitemap.xml
knownPaths := []string{
"/sitemap.xml",
"/sitemap_index.xml",
"/sitemap/sitemap.xml",
"/sitemap/sitemap-index.xml",
}
// If no URL is provided, check common paths
if *sitemapURL == "" {
fmt.Println("No URL provided, checking common paths for sitemap.xml")
baseURL := "https://example.com" // Replace with your base URL if needed
for _, p := range knownPaths {
u, err := url.Parse(baseURL)
if err != nil {
fmt.Printf("Invalid base URL: %v\n", err)
return
}
u.Path = path.Join(u.Path, p)
if checkURL(u.String()) {
*sitemapURL = u.String()
break
}
}
if *sitemapURL == "" {
fmt.Println("Sitemap not found in common paths")
return
}
}
// Create an HTTP client with TLS certificate verification disabled
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
// Fetch the sitemap XML
resp, err := client.Get(*sitemapURL)
if err != nil {
fmt.Printf("Failed to fetch sitemap: %v\n", err)
return
}
defer resp.Body.Close()
// Read the XML response
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Failed to read response body: %v\n", err)
return
}
// Parse the XML data
var sitemap Sitemap
err = xml.Unmarshal(data, &sitemap)
if err != nil {
fmt.Printf("Failed to parse XML: %v\n", err)
return
}
// Extract URLs
urls := make([]string, len(sitemap.URLs))
for i, url := range sitemap.URLs {
urls[i] = url.Loc
}
if *csvOutput {
// Output URLs to a CSV file
err := writeURLsToCSV("sitemap.csv", urls)
if err != nil {
fmt.Printf("Failed to write CSV: %v\n", err)
return
}
fmt.Println("URLs written to sitemap.csv")
} else {
// Print URLs as a raw comma-separated string
fmt.Println(strings.Join(urls, ","))
}
}
// checkURL checks if a given URL is accessible
func checkURL(sitemapURL string) bool {
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
resp, err := client.Head(sitemapURL)
if err != nil {
return false
}
return resp.StatusCode == http.StatusOK
}
// writeURLsToCSV writes a slice of URLs to a CSV file
func writeURLsToCSV(filename string, urls []string) error {
file, err := os.Create(filename)
if err != nil {
return err
}
defer file.Close()
writer := csv.NewWriter(file)
defer writer.Flush()
for _, url := range urls {
if err := writer.Write([]string{url}); err != nil {
return err
}
}
return nil
}

36
run.sh Executable file
View File

@ -0,0 +1,36 @@
#!/bin/bash
# Function to run the built binary for the local architecture
run_binary() {
os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case $arch in
x86_64) arch="amd64" ;;
arm64) arch="arm64" ;; # Handle M1 Mac correctly
armv7l) arch="arm/v7" ;;
*) echo "Unsupported architecture: $arch"; exit 1 ;;
esac
output_name="sitemapper"
if [[ "$os/$arch" != "linux/amd64" ]]; then
output_name="${output_name}_${os}_${arch}"
fi
output_name="dist/${output_name}"
if [ -f "${output_name}" ]; then
echo "Running ${output_name} $@"
./${output_name} "$@"
else
echo "Binary for ${os}/${arch} not found. Please check the build logs."
fi
}
# Check if arguments are provided
if [ $# -eq 0 ]; then
echo "Usage: $0 [arguments for the binary]"
exit 1
fi
run_binary "$@"

1093
sitemap.csv Normal file

File diff suppressed because it is too large Load Diff