Skip to content

Commit

Permalink
chore: upload binary to github releases (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Dec 17, 2022
1 parent 0d475e5 commit 85ecdf9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 18 deletions.
4 changes: 3 additions & 1 deletion .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ checksum:
name_template: 'checksums.txt'

archives:
- files:
- format: 'binary'
name_template: "{{ .ProjectName }}_{{ .Version }}_{{ .Os }}_{{ .Arch }}"
files:
- completions/*

snapshot:
Expand Down
7 changes: 4 additions & 3 deletions npm/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ const name = 'safebox';
const version = packageJson.version;
const platform = PLATFORM_MAPPING[process.platform];
const arch = ARCH_MAPPING[process.arch];
const binaryName = platform === 'win32' ? `${name}.ext` : name;
const tarUrl = `https://github.com/adikari/safebox/releases/download/v${version}/safebox_${version}_${platform}_${arch}.tar.gz`;
const ext = platform === 'win32' ? '.exe' : '';
const binaryName = `${name}.${ext}`;
const binaryUrl = `https://github.com/adikari/safebox/releases/download/v${version}/safebox_${version}_${platform}_${arch}${ext}`;
const bin = path.join(__dirname, "bin");

const constants = {
Expand All @@ -32,7 +33,7 @@ const constants = {
arch,
binaryName,
bin,
tarUrl,
binaryUrl,
};

module.exports = constants;
24 changes: 15 additions & 9 deletions npm/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const request = require('request'),
os = require('os'),
fs = require('fs'),
path = require('path'),
cp = require('child_process'),
constants = require('./constants');

const { name, platform, arch, binaryName, bin, tarUrl } = constants;
const { name, platform, arch, binaryName, bin, binaryUrl } = constants;

if (!arch) {
error(`${name} is not supported for this architecture: ${arch}`);
Expand All @@ -28,17 +27,25 @@ const MAX_RETRIES = 3;

const install = () => {
const tmpdir = os.tmpdir();
const req = request({ uri: tarUrl });
const binary = path.join(tmpdir, binaryName);

const tarFile = `${tmpdir}/${name}-${Date.now()}.tar.gz`;
const download = fs.createWriteStream(tarFile);
const copyBinary = () => fs.copyFileSync(binary, path.join(bin, binaryName));

if (fs.existsSync(binary)) {
console.log('safebox binary already downloaded');
copyBinary();
return;
}

const req = request({ uri: binaryUrl });
if (retries > 0) {
console.log(`retrying to install safebox - retry ${retries} out of ${MAX_RETRIES}`)
}

console.log(`downloading safebox binary`);

const download = fs.createWriteStream(binary);

req.on('response', res => {
if (res.statusCode !== 200) {
error(`Error downloading safebox binary. HTTP Status Code: ${res.statusCode}`);
Expand All @@ -51,12 +58,11 @@ const install = () => {
console.log('download complete. installing safebox.')

try {
if (!fs.existsSync(tarFile)) {
throw new Error(`${tarFile} does not exist`)
if (!fs.existsSync(binary)) {
throw new Error(`${binary} does not exist`)
}

cp.execSync(`tar -xf ${tarFile} -C ${tmpdir}`);
fs.copyFileSync(path.join(tmpdir, binaryName), path.join(bin, binaryName));
copyBinary();
} catch (error) {
console.error('failed to extract binary.', error.message)

Expand Down
7 changes: 2 additions & 5 deletions scripts/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ download_binary_and_run_installer() {
need_cmd mkdir
need_cmd rm
need_cmd rmdir
need_cmd tar
need_cmd which
need_cmd dirname
need_cmd awk
Expand All @@ -43,10 +42,9 @@ download_binary_and_run_installer() {
esac

local _current_dir=$(pwd)
local _tardir="safebox_${PACKAGE_VERSION:1}"_"${_arch}"
local _url="$BINARY_DOWNLOAD_PREFIX/$PACKAGE_VERSION/${_tardir}.tar.gz"
local _binary="safebox_${PACKAGE_VERSION:1}"_"${_arch}"
local _url="$BINARY_DOWNLOAD_PREFIX/$PACKAGE_VERSION/${_binary}$_ext"
local _dir="$(mktemp -d 2>/dev/null || ensure mktemp -d -t test)"
local _file="$_dir/input.tar.gz"
local _safebox="$_dir/safebox$_ext"
local _bin_dir="/usr/local/bin"

Expand All @@ -60,7 +58,6 @@ download_binary_and_run_installer() {
exit 1
fi

ensure tar xf "$_file"
sudo mv "$_safebox" "$_bin_dir"
local _retval=$?

Expand Down

0 comments on commit 85ecdf9

Please sign in to comment.