diff --git a/README.md b/README.md index b7ebc40..ada9a2c 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,16 @@ This is a very simple static webserver which hot-reloads your web-browser on cha ## Usage `wd-40 s|serve ` or `wd-40 s|serve` +## Getting started +```bash +go install github.com/baalimago/wd-40@latest +``` + +You may also use the setup script: +```bash +curl -fsSL https://raw.githubusercontent.com/baalimago/wd-40/main/setup.sh | sh +``` + ## Architecture 1. First the content of the website is copied to a temporary directory 1. At every file, the MIME type is inspected, if it's text/html, a `delta-streamer.js` script is injected diff --git a/cmd/setup.go b/cmd/setup.go index 1bbdd1d..921f01f 100644 --- a/cmd/setup.go +++ b/cmd/setup.go @@ -21,6 +21,9 @@ func Parse(args []string) (Command, error) { } cmdCandidate := "" for _, arg := range args[1:] { + if isHelp(arg) { + return nil, ErrHelpful + } isFlag := strings.HasPrefix(arg, "-") if isFlag { continue @@ -41,6 +44,10 @@ func Parse(args []string) (Command, error) { return nil, ArgNotFoundError(cmdCandidate) } +func isHelp(s string) bool { + return s == "-h" || s == "-help" || s == "h" || s == "help" +} + func formatCommandDescriptions() string { var buf bytes.Buffer w := tabwriter.NewWriter(&buf, 0, 0, 2, ' ', 0) diff --git a/main.go b/main.go index 688e216..b6d0822 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,11 @@ func printHelp(command cmd.Command, err error, printUsage cmd.UsagePrinter) int } else if errors.Is(err, cmd.ErrNoArgs) { printUsage() } else if errors.Is(err, cmd.ErrHelpful) { - fmt.Println(command.Help()) + if command != nil { + fmt.Println(command.Help()) + } else { + printUsage() + } return 0 } else { ancli.PrintfErr("unknown error: %v", err.Error()) diff --git a/setup.sh b/setup.sh new file mode 100755 index 0000000..f24dd8b --- /dev/null +++ b/setup.sh @@ -0,0 +1,87 @@ +#!/bin/sh + +# Function to get the latest release download URL for the specified OS and architecture +get_latest_release_url() { + repo="baalimago/wd-40" + os="$1" + arch="$2" + + # Fetch the latest release data from GitHub API + release_data=$(curl -s "https://api.github.com/repos/$repo/releases/latest") + + # Extract the asset URL for the specified OS and architecture + download_url=$(echo "$release_data" | grep "browser_download_url" | grep "$os" | grep "$arch" | cut -d '"' -f 4) + + echo "$download_url" +} + +# Detect the OS +case "$(uname)" in + Linux*) + os="linux" + ;; + Darwin*) + os="darwin" + ;; + *) + echo "Unsupported OS: $(uname)" + exit 1 + ;; +esac + +# Detect the architecture +arch=$(uname -m) +case "$arch" in + x86_64) + arch="amd64" + ;; + armv7*) + arch="arm" + ;; + aarch64|arm64) + arch="arm64" + ;; + i?86) + arch="386" + ;; + *) + echo "Unsupported architecture: $arch" + exit 1 + ;; +esac + +printf "detected os: '%s', arch: '%s'\n" "$os" "$arch" + +# Get the download URL for the latest release +printf "finding asset url..." +download_url=$(get_latest_release_url "$os" "$arch") +printf "OK!\n" + +# Download the binary +tmp_file=$(mktemp) + +printf "downloading binary..." +if ! curl -s -L -o "$tmp_file" "$download_url"; then + echo + echo "Failed to download the binary." + exit 1 +fi +printf "OK!\n" + +printf "setting file executable file permissions..." +# Make the binary executable + +if ! chmod +x "$tmp_file"; then + echo + echo "Failed to make the binary executable. Try running the script with sudo." + exit 1 +fi +printf "OK!\n" + +# Move the binary to /usr/local/bin and handle permission errors +if ! mv "$tmp_file" /usr/local/bin/wd-40; then + echo "Failed to move the binary to /usr/local/bin/wd-40, see error above. Try running the script with sudo, or run 'mv $tmp_file '." + exit 1 +fi + +echo "wd-40 installed successfully in /usr/local/bin, try it out with 'wd-40 h'"