commit 1f7216340111c81d48c142a29827d6667b12a9b2 Author: Colin Date: Fri Apr 26 16:20:28 2024 -0400 haste-it diff --git a/README.md b/README.md new file mode 100644 index 0000000..c110cc9 --- /dev/null +++ b/README.md @@ -0,0 +1,59 @@ +To help you install the `haste-it` binaries efficiently on various architectures, I'll provide you with a README guide that includes bash one-liners for installation. Additionally, I'll include a simple bash script for automatic installation and setting up the environment variable for the Hastebin server URL. + +### README.md Content + +```markdown +# Haste-It Installation Guide + +This guide provides instructions for installing the `haste-it` binary on various systems based on the architecture. + +## Installation + +Before installing, ensure that the binary for your architecture has been built and is located in the `dist` directory. + +Run the following command to install the `haste-it` binary: + +```bash +bash install.sh +``` + +This script will automatically detect your system's architecture, choose the correct binary from the `dist` directory, and install it. + +## Configuration + +After installation, set the environment variable for your Hastebin server by adding the following to your `.bashrc`, `.zshrc`, or equivalent shell configuration file: + +```bash +export HASTEBIN_URL="https://your-hastebin-server.com" +``` + +Replace `https://your-hastebin-server.com` with the URL of your Hastebin server. + +## Usage + +After installation and configuration, you can use `haste-it` by simply typing: + +```bash +haste-it [file] +``` + +If no file is specified, `haste-it` will read from standard input. +``` + +### Installation Script (`install.sh`) + +Here's a simple bash script to automate the installation process. This script will detect the architecture, find the appropriate binary in the `dist` directory, and install it to `/usr/local/bin`. + +## Instructions for Use + + Ensure that install.sh has execute permissions: + +```bash +chmod +x install.sh +``` +Run the script with sufficient privileges (as it installs to /usr/local/bin): + +```bash +sudo ./install.sh +``` +Set the environment variable as described in the README. diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..18e5dd0 --- /dev/null +++ b/build.sh @@ -0,0 +1,57 @@ +#!/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="haste-it" + + if [[ "$os/$arch" != "$DEFAULT_ARCH" ]]; then + output_name="${output_name}_${os}_${arch}" + fi + + output_name="dist/${output_name}" + + echo "Building 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 +} + +# 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." + diff --git a/build.sh.save b/build.sh.save new file mode 100644 index 0000000..f357679 --- /dev/null +++ b/build.sh.save @@ -0,0 +1,57 @@ +#!/bin/bash + +# Default architecture +DEFAULT_ARCH="linux/amd64" + +# Supported architectures (adjust as needed) +ARCHITECTURES=("linux/amd64" "linux/arm64" "linux/arm/v7") + +# 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="haste-it" + + if [[ "$os/$arch" != "$DEFAULT_ARCH" ]]; then + output_name="${output_name}_${os}_${arch}" + fi + + output_name="build_output/${output_name}" + + echo "Building 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}" + 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." + diff --git a/build_logs/darwin_amd64_build.log b/build_logs/darwin_amd64_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/darwin_arm64_build.log b/build_logs/darwin_arm64_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/linux_amd64_build.log b/build_logs/linux_amd64_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/linux_arm64_build.log b/build_logs/linux_arm64_build.log new file mode 100644 index 0000000..e69de29 diff --git a/build_logs/linux_arm_build.log b/build_logs/linux_arm_build.log new file mode 100644 index 0000000..e69de29 diff --git a/dist/haste-it b/dist/haste-it new file mode 100755 index 0000000..9d33058 Binary files /dev/null and b/dist/haste-it differ diff --git a/dist/haste-it_darwin_amd64 b/dist/haste-it_darwin_amd64 new file mode 100755 index 0000000..7d54763 Binary files /dev/null and b/dist/haste-it_darwin_amd64 differ diff --git a/dist/haste-it_darwin_arm64 b/dist/haste-it_darwin_arm64 new file mode 100755 index 0000000..0b50b7c Binary files /dev/null and b/dist/haste-it_darwin_arm64 differ diff --git a/dist/haste-it_linux_arm b/dist/haste-it_linux_arm new file mode 100755 index 0000000..cdec6b5 Binary files /dev/null and b/dist/haste-it_linux_arm differ diff --git a/dist/haste-it_linux_arm64 b/dist/haste-it_linux_arm64 new file mode 100755 index 0000000..2228ba2 Binary files /dev/null and b/dist/haste-it_linux_arm64 differ diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e9a94d3 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module yourmodule + +go 1.21.1 diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..1a9194b --- /dev/null +++ b/install.sh @@ -0,0 +1,59 @@ + +### Installation Script (`install.sh`) + +Here's a simple bash script to automate the installation process. This script will detect the architecture, find the appropriate binary in the `dist` directory, and install it to `/usr/local/bin`. + +```bash +#!/bin/bash + +# Define the installation directory and binary name +INSTALL_DIR="/usr/local/bin" +BINARY_NAME="haste-it" + +# Define supported architectures and corresponding binaries +declare -A binaries +binaries["linux/amd64"]="haste-it_linux_amd64" +binaries["linux/arm64"]="haste-it_linux_arm64" +binaries["linux/arm/v7"]="haste-it_linux_arm" +binaries["darwin/amd64"]="haste-it_darwin_amd64" +binaries["darwin/arm64"]="haste-it_darwin_arm64" + +# Detect the OS and architecture +OS="$(uname -s | tr '[:upper:]' '[:lower:]')" +ARCH="$(uname -m)" + +case $ARCH in + x86_64) + ARCH="amd64" + ;; + arm64) + ARCH="arm64" + ;; + aarch64) + ARCH="arm64" + ;; + arm*) + ARCH="arm/v7" + ;; +esac + +# Construct the key for finding the binary +KEY="${OS}/${ARCH}" + +# Check if the binary exists for this architecture +if [[ -z "${binaries[$KEY]}" ]]; then + echo "No pre-built binary for your system architecture ($KEY)." + exit 1 +fi + +# Install the binary +BINARY_PATH="dist/${binaries[$KEY]}" +if [[ -f "$BINARY_PATH" ]]; then + sudo cp "$BINARY_PATH" "$INSTALL_DIR/$BINARY_NAME" + sudo chmod +x "$INSTALL_DIR/$BINARY_NAME" + echo "Installed $BINARY_NAME to $INSTALL_DIR" +else + echo "Binary file not found: $BINARY_PATH" + exit 1 +fi + diff --git a/main.go b/main.go new file mode 100644 index 0000000..46aad84 --- /dev/null +++ b/main.go @@ -0,0 +1,63 @@ +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) +} +