Skip to content

Commit

Permalink
Added setup script, updated README and help command
Browse files Browse the repository at this point in the history
  • Loading branch information
baalimago committed Aug 18, 2024
1 parent 8df68aa commit 03ffdea
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ This is a very simple static webserver which hot-reloads your web-browser on cha
## Usage
`wd-40 s|serve <relative directory>` 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
Expand Down
7 changes: 7 additions & 0 deletions cmd/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
87 changes: 87 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -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 <desired-position>'."
exit 1
fi

echo "wd-40 installed successfully in /usr/local/bin, try it out with 'wd-40 h'"

0 comments on commit 03ffdea

Please sign in to comment.