Skip to content

Commit

Permalink
index.js to manage CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
abe-winter committed Nov 17, 2023
0 parents commit 3d8408d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# build-action

This is a Github CI action which wraps Viam's cloud build system. Use this to build and publish your Viam module for multiple platforms.

## What is Viam cloud build?

Cloud build is cross-compilation infrastructure that we provide to module authors so you can publish your module for multiple platforms. (x86 and ARM Linux, ideally MacOS as well).

Your customers are deploying to different kinds of hardware, and prototyping on a laptop. Cloud build helps you to reach them everywhere without doing the heavy lifting of operating your own buildsystem.

## Basic usage

## Auth instructions
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
viam-cli
59 changes: 59 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const fs = require('fs');
const os = require('os');
const https = require('https');
const stream = require('node:stream/promises');
const util = require('node:util');
const execFileAsync = util.promisify(require('node:child_process').execFile);

const platforms = ['linux', 'darwin'];
const machines = {
// todo: why are arm64 and aarch64 both in their docs https://nodejs.org/api/os.html#osmachine
arm64: 'arm64',
aarch64: 'arm64',
x86_64: 'amd64',
};
const cliPath = './viam-cli';
const argsConfig = {
options: {
"skip-download": { type: 'boolean' },
},
};

/** download a file and optionally set mode bits */
async function download(url, dest, mode = fs.constants.S_IRWXU) {
const output = fs.createWriteStream(dest);
const incoming = await new Promise(function(resolve, _reject) {
https.get(url, (incoming) => resolve(incoming));
})
await stream.pipeline(incoming, output);
if (mode != null) {
fs.chmodSync(dest, mode);
}
}

/** infer the architecture portion of the URL from os.* methods. */
function archSlug() {
const platform = os.platform(), machine = os.machine();
if (platforms.indexOf(platform) == -1) {
throw Error(`unknown platform ${platform}`);
}
if (machines[machine] == null) {
throw Error(`unknown machine ${machine}`);
}
return `${platform}-${machines[machine]}`;
}

/** async main */
(async function() {
const args = util.parseArgs(argsConfig);
const slug = archSlug();
console.log('inferred architecture', slug);
if (!args.values['skip-download']) {
await download(`https://storage.googleapis.com/packages.viam.com/apps/viam-cli/viam-cli-stable-${slug}`, cliPath);
console.log('downloaded CLI');
}
console.log((await execFileAsync(cliPath, ['version'])).stdout.trim());
console.warn("TODO: start build");
console.warn("TODO: wait for build");
console.warn("TODO: show logs / status");
})();

0 comments on commit 3d8408d

Please sign in to comment.