Skip to content

Commit

Permalink
Add install script
Browse files Browse the repository at this point in the history
  • Loading branch information
babarot committed Feb 8, 2025
1 parent 4393702 commit 28feb90
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
<meta property="og:url" content="https://github.com/babarot/gomi">
<meta property="og:title" content="gomi - A Safer Alternative to the UNIX rm Command">
<meta property="og:description" content="Never worry about accidentally deleting important files again. gomi moves them to trash instead of permanent deletion, giving you peace of mind in the command line.">
<meta property="og:image" content="https://storage.googleapis.com/zenn-user-upload/426c71a0bbca-20250208.png">
<meta property="og:image" content="https://storage.googleapis.com/zenn-user-upload/b3b2ba4ffc5b-20250208.png">

<!-- Twitter -->
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:url" content="https://github.com/babarot/gomi">
<meta property="twitter:title" content="gomi - A Safer Alternative to the UNIX rm Command">
<meta property="twitter:description" content="Never worry about accidentally deleting important files again. gomi moves them to trash instead of permanent deletion, giving you peace of mind in the command line.">
<meta property="twitter:image" content="https://storage.googleapis.com/zenn-user-upload/426c71a0bbca-20250208.png">
<meta property="twitter:image" content="https://storage.googleapis.com/zenn-user-upload/b3b2ba4ffc5b-20250208.png">

<!-- External Resources -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Orbitron:wght@400;500;700&display=swap">
Expand Down
41 changes: 41 additions & 0 deletions docs/install/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<title>Installation</title>
<style>
pre {
background-color: #f4f4f4;
padding: 15px;
border-radius: 5px;
overflow-x: auto;
line-height: 1.5;
}
</style>
</head>
<body>
<div id="content"></div>

<script>
async function loadRawContent() {
try {
const response = await fetch('https://raw.githubusercontent.com/babarot/gomi/main/hack/install');
const text = await response.text();

const escapedText = text
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#039;');

document.getElementById('content').innerHTML = `<pre>${escapedText}</pre>`;
} catch (error) {
console.error('Error loading content:', error);
document.getElementById('content').innerHTML = 'Failed to load content.';
}
}

loadRawContent();
</script>
</body>
</html>
18 changes: 18 additions & 0 deletions hack/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Installation script

Run script from local:

```console
$ cat hack/install | bash
```

Run script via curl (when not cloning repo):

```console
$ curl -sL https://raw.githubusercontent.com/babarot/gomi/HEAD/hack/install | GOMI_VERSION=v1.2.2 bash
```

env | description | default
---|---|---
`GOMI_VERSION` | gomi version, available versions are on [releases](https://github.com/babarot/gomi/releases) | `latest`
`GOMI_BIN_DIR` | Path to install | `~/bin`
125 changes: 125 additions & 0 deletions hack/install
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/usr/bin/env bash

gomi_bin_dir=${GOMI_BIN_DIR:-~/bin}
gomi_version=${GOMI_VERSION:-latest}
gomi_tmp_dir=${TMPDIR:-/tmp}/gomi-${gomi_version}

main() {
# Try to download binary executable
local arch
local notfound=false
local tarball

if [[ -x ${gomi_bin_dir}/gomi ]]; then
echo "already installed: ${gomi_bin_dir}/gomi"
return 0
fi

arch="$(uname -sm)"
case "${arch}" in
"Darwin arm64") tarball="gomi_Darwin_arm64.tar.gz" ;;
"Darwin x86_64") tarball="gomi_Darwin_x86_64.tar.gz" ;;
"Linux aarch64") tarball="gomi_Linux_arm64.tar.gz" ;;
"Linux "*64) tarball="gomi_Linux_x86_64.tar.gz" ;;
*) notfound=true ;;
esac

if ! { download ${tarball} && install -v -m 0755 "${gomi_tmp_dir}/gomi" "${gomi_bin_dir}/gomi"; } || ${notfound}; then
echo "gomi available on your system is not found. So trying to make gomi from Go!"
if command -v go >/dev/null; then
try_go
else
echo "go executable not found. Installation failed." >&2
return 1
fi
fi

command -v gomi &>/dev/null && gomi --version

echo 'For more information, see: https://github.com/babarot/gomi'
}

try_curl() {
local file=${1}
command -v curl > /dev/null &&
if [[ ${file} =~ tar.gz$ ]]; then
curl --progress-bar -fL "${file}" | tar -xzf - -C "${gomi_tmp_dir}"
else
local tmp=${gomi_tmp_dir}/gomi.zip
curl --progress-bar -fLo "${tmp}" "${file}" && unzip -o "${tmp}" && rm -f "${tmp}"
fi
}

try_wget() {
local file=${1}
command -v wget > /dev/null &&
if [[ ${file} =~ tar.gz$ ]]; then
wget -O - "${file}" | tar -xzf - -C "${gomi_tmp_dir}"
else
local tmp=${gomi_tmp_dir}/gomi.zip
wget -O "${tmp}" "${file}" && unzip -o "${tmp}" && rm -f "${tmp}"
fi
}

download() {
local tarball="${1}"
local url

if [[ -z ${tarball} ]]; then
# when not found what to download
return 1
fi

mkdir -p "${gomi_bin_dir}" || {
echo "Failed to create directory" >&2
return 1
}

mkdir -p "${gomi_tmp_dir}" || {
echo "Failed to create directory" >&2
return 1
}

if [[ ${gomi_version} == latest ]]; then
url="https://github.com/babarot/gomi/releases/latest/download/${tarball}"
else
url="https://github.com/babarot/gomi/releases/download/${gomi_version}/${tarball}"
fi

echo "Downloading gomi ..."
if ! (try_curl "${url}" || try_wget "${url}"); then
echo "Failed to download with curl and wget" >&2
return 1
fi

if [[ ! -f ${gomi_tmp_dir}/gomi ]]; then
echo "Failed to download ${tarball}" >&2
return 1
fi
}

try_go() {
local do_cp=false
local path="github.com/babarot/gomi"
local cmd="${path}/cmd"

echo -n "Building binary (go get -u ${path}) ... "
if [[ -z ${GOPATH} ]]; then
do_cp=true
export GOPATH="${TMPDIR:-/tmp}/gomi-gopath"
mkdir -p "${GOPATH}"
fi

local ts
ts=$(date "+%Y-%m-%d")

if go install -ldflags "-s -w -X ${cmd}.Version=${gomi_version} -X ${cmd}.BuildTag=built-by-go -X ${cmd}.BuildSHA=${ts}" ${path}; then
echo "OK"
${do_cp} && cp -v "${GOPATH}/bin/gomi" "${gomi_bin_dir}/gomi"
else
echo "Failed to build binary. Installation failed." >&2
return 1
fi
}

main "${@}"

0 comments on commit 28feb90

Please sign in to comment.