Skip to content

Commit

Permalink
feat: add single install script
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-karan committed Jul 1, 2024
1 parent 24ec17e commit a831f77
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 40 deletions.
94 changes: 54 additions & 40 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@ env:
- GO111MODULE=on
- CGO_ENABLED=0

before:
hooks:
- go mod tidy

builds:
- binary: doggo
id: cli
ldflags:
- -s -w -X "main.buildVersion={{ .Tag }} ({{ .ShortCommit }} {{ .Date }})"
dir: ./cmd/
goos:
- windows
- darwin
- linux
- darwin
- windows
- freebsd
- openbsd
- netbsd
goarch:
- amd64
- arm64
- "386"
- arm
goarm:
- 6
- 7
ldflags:
- -s -w -X "main.buildVersion={{ .Tag }} ({{ .ShortCommit }} {{ .Date }})"
dir: ./cmd/doggo/
- "7"
ignore:
- goos: windows
goarch: arm64
- goos: windows
goarm: "7"

- binary: doggo-api.bin
id: api
- binary: doggo-web.bin
id: web
goos:
- windows
- darwin
- linux
goarch:
- amd64
Expand All @@ -33,14 +44,43 @@ builds:
- 7
ldflags:
- -s -w -X "main.buildVersion={{ .Tag }} ({{ .ShortCommit }} {{ .Date }})"
dir: ./cmd/api/
dir: ./web/

archives:
- format: tar.gz
- id: cli
allow_different_binary_count: true
format_overrides:
- goos: windows
format: zip
name_template: >-
{{ .ProjectName }}_
{{- .Version }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
wrap_in_directory: true
files:
- README.md
- LICENSE
- completions/

changelog:
sort: asc
use: github
filters:
exclude:
- "^docs:"
- "^test:"
groups:
- title: "New Features"
regexp: "^.*feat[(\\w)]*:+.*$"
order: 0
- title: "Bug fixes"
regexp: "^.*fix[(\\w)]*:+.*$"
order: 10
- title: Others
order: 999


dockers:
- image_templates:
Expand All @@ -66,29 +106,3 @@ dockers:
build_flag_templates:
- "--build-arg"
- "ARCH=arm64v8"

# - image_templates:
# - "ghcr.io/mr-karan/doggo-api:{{ .Tag }}"
# - "ghcr.io/mr-karan/doggo-api:latest"
# id: doggo-api
# ids:
# - api
# dockerfile: Dockerfile-api
# build_flag_templates:
# - "--build-arg"
# - "ARCH=amd64"
# extra_files:
# - config-api-sample.toml
# - image_templates:
# - "ghcr.io/mr-karan/doggo-api:{{ .Tag }}-arm64v8"
# - "ghcr.io/mr-karan/doggo-api:latest-arm64v8"
# id: doggo-api-arm
# ids:
# - api
# goarch: arm64
# dockerfile: Dockerfile-api
# build_flag_templates:
# - "--build-arg"
# - "ARCH=arm64v8"
# extra_files:
# - config-api-sample.toml
165 changes: 165 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#!/usr/bin/env sh

set -eu
printf '\n'

BOLD="$(tput bold 2>/dev/null || printf '')"
GREY="$(tput setaf 0 2>/dev/null || printf '')"
GREEN="$(tput setaf 2 2>/dev/null || printf '')"
YELLOW="$(tput setaf 3 2>/dev/null || printf '')"
BLUE="$(tput setaf 4 2>/dev/null || printf '')"
RED="$(tput setaf 1 2>/dev/null || printf '')"
NO_COLOR="$(tput sgr0 2>/dev/null || printf '')"

info() {
printf '%s\n' "${BOLD}${GREY}>${NO_COLOR} $*"
}

warn() {
printf '%s\n' "${YELLOW}! $*${NO_COLOR}"
}

error() {
printf '%s\n' "${RED}x $*${NO_COLOR}" >&2
}

completed() {
printf '%s\n' "${GREEN}${NO_COLOR} $*"
}

has() {
command -v "$1" 1>/dev/null 2>&1
}

SUPPORTED_TARGETS="linux_amd64 linux_arm64 windows_amd64 darwin_amd64 darwin_arm64"

get_latest_release() {
curl --silent "https://api.github.com/repos/mr-karan/doggo/releases/latest" |
grep '"tag_name":' |
sed -E 's/.*"([^"]+)".*/\1/'
}

detect_platform() {
platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "${platform}" in
linux) platform="linux" ;;
darwin) platform="darwin" ;;
msys*|mingw*) platform="windows" ;;
*)
error "Unsupported platform: ${platform}"
exit 1
;;
esac
printf '%s' "${platform}"
}

detect_arch() {
arch="$(uname -m)"
case "${arch}" in
x86_64) arch="amd64" ;;
aarch64|arm64) arch="arm64" ;;
*)
error "Unsupported architecture: ${arch}"
exit 1
;;
esac
printf '%s' "${arch}"
}

download_and_install() {
version="$1"
platform="$2"
arch="$3"

# Remove 'v' prefix from version for filename
version_no_v="${version#v}"
filename="doggo_${version_no_v}_${platform}_${arch}.tar.gz"
url="https://github.com/mr-karan/doggo/releases/download/${version}/${filename}"

info "Downloading doggo ${version} for ${platform}_${arch}..."
info "Download URL: ${url}"

if has curl; then
if ! curl -sSL "${url}" -o "${filename}"; then
error "Failed to download ${filename}"
error "Curl output:"
curl -SL "${url}"
exit 1
fi
elif has wget; then
if ! wget -q "${url}" -O "${filename}"; then
error "Failed to download ${filename}"
error "Wget output:"
wget "${url}"
exit 1
fi
else
error "Neither curl nor wget found. Please install one of them and try again."
exit 1
fi

info "Verifying downloaded file..."
if ! file "${filename}" | grep -q "gzip compressed data"; then
error "Downloaded file is not in gzip format. Installation failed."
error "File type:"
file "${filename}"
rm -f "${filename}"
exit 1
fi

info "Extracting ${filename}..."
if ! tar -xzvf "${filename}"; then
error "Failed to extract ${filename}"
rm -f "${filename}"
exit 1
fi

info "Installing doggo..."
if [ ! -f "doggo" ]; then
error "doggo binary not found in the extracted files"
error "Extracted files:"
ls -la
rm -f "${filename}"
exit 1
fi

chmod +x doggo
if ! sudo mv doggo /usr/local/bin/; then
error "Failed to move doggo to /usr/local/bin/"
rm -f "${filename}" "doggo"
exit 1
fi

info "Cleaning up..."
rm -f "${filename}"

completed "doggo ${version} has been installed to /usr/local/bin/doggo"
}

main() {
if ! has curl && ! has wget; then
error "Either curl or wget is required to download doggo. Please install one of them and try again."
exit 1
fi

platform="$(detect_platform)"
arch="$(detect_arch)"
version="$(get_latest_release)"

info "Latest doggo version: ${version}"
info "Detected platform: ${platform}"
info "Detected architecture: ${arch}"

target="${platform}_${arch}"

if ! echo "${SUPPORTED_TARGETS}" | grep -q "${target}"; then
error "Unsupported target: ${target}"
exit 1
fi

download_and_install "${version}" "${platform}" "${arch}"

info "You can now use doggo by running 'doggo' in your terminal."
}

main

0 comments on commit a831f77

Please sign in to comment.