haste-it/main.go

64 lines
1.4 KiB
Go

package main
import (
"encoding/json"
"bytes"
"fmt"
"io"
"net/http"
"os"
)
func main() {
// Fetch Hastebin URL from environment variable
hastebinURL := os.Getenv("HASTEBIN_URL")
if hastebinURL == "" {
fmt.Println("Error: HASTEBIN_URL environment variable is not set")
return
}
// Determine the input source
var input io.Reader
if len(os.Args) > 1 {
file, err := os.Open(os.Args[1])
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
return
}
defer file.Close()
input = file
} else {
input = os.Stdin
}
// Read the entire input
buffer := new(bytes.Buffer)
_, err := buffer.ReadFrom(input)
if err != nil {
fmt.Printf("Error reading input: %v\n", err)
return
}
// Send data to Hastebin
resp, err := http.Post(hastebinURL+"/documents", "text/plain", buffer)
if err != nil {
fmt.Printf("Error posting data to Hastebin: %v\n", err)
return
}
defer resp.Body.Close()
// Decode response to get the key
type HastebinResponse struct {
Key string `json:"key"`
}
var res HastebinResponse
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
fmt.Printf("Error decoding Hastebin response: %v\n", err)
return
}
// Output the full URL
fmt.Println(hastebinURL + "/" + res.Key)
}