diff --git a/.DS_Store b/.DS_Store index 7a7974b..82d21b7 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/build.sh b/build.sh index f37c99f..8e43a41 100755 --- a/build.sh +++ b/build.sh @@ -1,3 +1,29 @@ #!/bin/bash -GOOS=linux GOARCH=amd64 go build +# Default architecture +DEFAULT_ARCH="linux/amd64" + +# Supported architectures (adjust as needed) +ARCHITECTURES=("linux/amd64" "linux/arm64" "linux/arm/v7") + +# Build function +build_binary() { + os=$1 + arch=$2 + output_name="go-glitch" + + if [[ "$os/$arch" != "$DEFAULT_ARCH" ]]; then + output_name="${output_name}_${os}_${arch}" + fi + + echo "Building for ${os}/${arch} -> ${output_name}" + GOOS=${os} GOARCH=${arch} go build -o ${output_name} +} + +# Main Build Process +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 diff --git a/go-glitch b/go-glitch index eb5be21..8e10dbf 100755 Binary files a/go-glitch and b/go-glitch differ diff --git a/go-glitch.go b/go-glitch.go index e9f1aca..0a798a2 100644 --- a/go-glitch.go +++ b/go-glitch.go @@ -1,78 +1,94 @@ package main import ( - "bufio" - "fmt" - "os" - "strings" - "time" + "bufio" + "fmt" + "io" + "os" + "strings" + "time" - "github.com/getsentry/sentry-go" + "github.com/getsentry/sentry-go" ) func main() { - // Check that exactly one argument is passed (file path) - if len(os.Args) != 2 { - fmt.Fprintf(os.Stderr, "Usage: %s \n", os.Args[0]) - os.Exit(1) - } + var reader io.Reader + var filePath string - filePath := os.Args[1] + // Determine the source of input + if len(os.Args) == 2 { + // Read from the file specified in the argument + filePath = os.Args[1] + file, err := os.Open(filePath) + if err != nil { + fmt.Fprintf(os.Stderr, "Error opening file: %s\n", err) + os.Exit(1) + } + defer file.Close() + reader = file + } else if len(os.Args) == 1 && !isInputFromPipe() { + fmt.Fprintf(os.Stderr, "Usage: %s OR pipe input\n", os.Args[0]) + os.Exit(1) + } else { + // Read from stdin if data is being piped + reader = os.Stdin + } - // Open the file - file, err := os.Open(filePath) - if err != nil { - fmt.Fprintf(os.Stderr, "Error opening file: %s\n", err) - os.Exit(1) - } - defer file.Close() + // Read content from the chosen input + scanner := bufio.NewScanner(reader) + var content strings.Builder + for scanner.Scan() { + content.WriteString(scanner.Text()) + content.WriteString("\n") // Preserve line breaks + } - // Read file contents into a string - scanner := bufio.NewScanner(file) - var content strings.Builder - for scanner.Scan() { - content.WriteString(scanner.Text()) - content.WriteString("\n") // Preserve line breaks - } + if err := scanner.Err(); err != nil { + fmt.Fprintf(os.Stderr, "Error reading input: %s\n", err) + os.Exit(1) + } - if err := scanner.Err(); err != nil { - fmt.Fprintf(os.Stderr, "Error reading file: %s\n", err) - os.Exit(1) - } - - logMessage := content.String() - - dsn := os.Getenv("SENTRY_DSN") - if dsn == "" { - fmt.Fprintf(os.Stderr, "Error: SENTRY_DSN environment variable is not set.\n") - os.Exit(1) - } - - // Initialize Sentry with the DSN - err = sentry.Init(sentry.ClientOptions{ - Dsn: dsn, - }) - if err != nil { - fmt.Fprintf(os.Stderr, "Error initializing Sentry: %s\n", err) - os.Exit(1) - } - - // Defer a function to flush Sentry's buffer - defer func() { - success := sentry.Flush(5 * time.Second) - if !success { - fmt.Fprintf(os.Stderr, "Failed to flush Sentry buffer within the expected time.\n") - } else { - fmt.Println("Sentry buffer flushed successfully.") - } - }() - - // Capture the message and output the event ID - eventID := sentry.CaptureMessage(logMessage) - if eventID != nil { - fmt.Printf("Sent message to Sentry with event ID: %s\n", *eventID) - } else { - fmt.Fprintf(os.Stderr, "Failed to send message to Sentry.\n") - } + logMessage := content.String() + sendToSentry(logMessage) } +// Helper function to check if there is data being piped to stdin +func isInputFromPipe() bool { + fileInfo, err := os.Stdin.Stat() + if err != nil { + return false + } + return fileInfo.Mode()&os.ModeCharDevice == 0 +} + +// Function to handle sending data to Sentry +func sendToSentry(logMessage string) { + dsn := os.Getenv("SENTRY_DSN") + if dsn == "" { + fmt.Fprintf(os.Stderr, "Error: SENTRY_DSN environment variable is not set.\n") + os.Exit(1) + } + + err := sentry.Init(sentry.ClientOptions{ + Dsn: dsn, + }) + if err != nil { + fmt.Fprintf(os.Stderr, "Error initializing Sentry: %s\n", err) + os.Exit(1) + } + + defer func() { + success := sentry.Flush(5 * time.Second) + if !success { + fmt.Fprintf(os.Stderr, "Failed to flush Sentry buffer within the expected time.\n") + } else { + fmt.Println("Sentry buffer flushed successfully.") + } + }() + + eventID := sentry.CaptureMessage(logMessage) + if eventID != nil { + fmt.Printf("Sent message to Sentry with event ID: %s\n", *eventID) + } else { + fmt.Fprintf(os.Stderr, "Failed to send message to Sentry.\n") + } +} diff --git a/go-glitch_linux_arm b/go-glitch_linux_arm new file mode 100755 index 0000000..06b02c7 Binary files /dev/null and b/go-glitch_linux_arm differ diff --git a/go-glitch_linux_arm64 b/go-glitch_linux_arm64 new file mode 100755 index 0000000..511b1fd Binary files /dev/null and b/go-glitch_linux_arm64 differ