working normally now
This commit is contained in:
parent
4526d05aef
commit
1002905f56
51
go-glitch.go
51
go-glitch.go
|
@ -1,53 +1,78 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/getsentry/sentry-go"
|
"github.com/getsentry/sentry-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Check for the right number of command line arguments
|
// Check that exactly one argument is passed (file path)
|
||||||
if len(os.Args) != 2 {
|
if len(os.Args) != 2 {
|
||||||
fmt.Println("Usage: ./go-glitch \"Your log message here\"")
|
fmt.Fprintf(os.Stderr, "Usage: %s <path to log file>\n", os.Args[0])
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
logMessage := os.Args[1]
|
filePath := os.Args[1]
|
||||||
|
|
||||||
|
// 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 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 file: %s\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
logMessage := content.String()
|
||||||
|
|
||||||
dsn := os.Getenv("SENTRY_DSN")
|
dsn := os.Getenv("SENTRY_DSN")
|
||||||
if dsn == "" {
|
if dsn == "" {
|
||||||
fmt.Println("Error: SENTRY_DSN environment variable is not set.")
|
fmt.Fprintf(os.Stderr, "Error: SENTRY_DSN environment variable is not set.\n")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize Sentry with the DSN
|
// Initialize Sentry with the DSN
|
||||||
err := sentry.Init(sentry.ClientOptions{
|
err = sentry.Init(sentry.ClientOptions{
|
||||||
Dsn: dsn,
|
Dsn: dsn,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("Error initializing Sentry: %s\n", err)
|
fmt.Fprintf(os.Stderr, "Error initializing Sentry: %s\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Defer a function to flush Sentry's buffer
|
// Defer a function to flush Sentry's buffer
|
||||||
defer func() {
|
defer func() {
|
||||||
success := sentry.Flush(5 * time.Second)
|
success := sentry.Flush(5 * time.Second)
|
||||||
if !success {
|
if !success {
|
||||||
fmt.Println("Failed to flush Sentry buffer within the expected time.")
|
fmt.Fprintf(os.Stderr, "Failed to flush Sentry buffer within the expected time.\n")
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Sentry buffer flushed successfully.")
|
fmt.Println("Sentry buffer flushed successfully.")
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Capture the message and output the event ID
|
// Capture the message and output the event ID
|
||||||
eventID := sentry.CaptureMessage(logMessage)
|
eventID := sentry.CaptureMessage(logMessage)
|
||||||
if eventID != nil {
|
if eventID != nil {
|
||||||
fmt.Printf("Sent message to Sentry with event ID: %s\n", *eventID)
|
fmt.Printf("Sent message to Sentry with event ID: %s\n", *eventID)
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("Failed to send message to Sentry.")
|
fmt.Fprintf(os.Stderr, "Failed to send message to Sentry.\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue