From 99c36ef3618bcb6100f016bbe23731480ed6e55b Mon Sep 17 00:00:00 2001 From: Scott Mebberson <74628+smebberson@users.noreply.github.com> Date: Sun, 16 Jun 2024 13:01:09 +0930 Subject: [PATCH] Improvements for c on Linux. --- bin/c-dev-ip.js | 36 ++++++++++++++++++++++++++++++++++++ bin/c-dev.js | 10 ++++++++++ bin/c-ds-dev-get.js | 34 ++++++++++++++++++++++++++++++++++ bin/c-ds-dev-set.js | 35 +++++++++++++++++++++++++++++++++++ bin/c-ds-dev.js | 13 +++++++++++++ bin/c-ds-handle-get.js | 2 +- bin/c-ds-handle-set.js | 3 ++- bin/c-ds-handle.js | 2 +- bin/c-ds-mk-get.js | 2 +- bin/c-ds-mk-set.js | 2 +- bin/c-ds-mk.js | 2 +- bin/c-ds-pc-get.js | 2 +- bin/c-ds-pc-set.js | 2 +- bin/c-ds-pc.js | 2 +- bin/c-ds.js | 8 +++++--- bin/c-gc-cmd.js | 2 +- bin/c-hosts-add.js | 20 +++++++++++++++++++- bin/c-kc-apply.js | 2 +- bin/c-kc-cmd.js | 2 +- bin/c-kc-logs.js | 2 +- bin/c-kc-manifests.js | 2 +- bin/c-kc-pod.js | 2 +- bin/c-kc-start.js | 2 +- bin/c-kc-stop.js | 2 +- bin/c-ts-ip.js | 2 +- bin/c-ts.js | 2 +- bin/c.js | 1 + bin/lib/c-ts.js | 19 ++++++++++++++++++- 28 files changed, 191 insertions(+), 24 deletions(-) create mode 100644 bin/c-dev-ip.js create mode 100644 bin/c-dev.js create mode 100644 bin/c-ds-dev-get.js create mode 100644 bin/c-ds-dev-set.js create mode 100644 bin/c-ds-dev.js diff --git a/bin/c-dev-ip.js b/bin/c-dev-ip.js new file mode 100644 index 0000000..68cea97 --- /dev/null +++ b/bin/c-dev-ip.js @@ -0,0 +1,36 @@ +'use strict'; + +const program = require('commander'); +const { exec } = require('shelljs'); +const { + devspacePath, + loadState, + newline, + reportError, + stateFilePath, +} = require('./lib/c'); + +// The basic program, which uses sub-commands. +program + .option('-n', 'Do not print the trailing newline character.') + .option( + '-p [profile]', + 'Specify a minikube profile, otherwise the default minikube profile will be used.' + ) + .parse(process.argv); + +const run = async () => { + const devName = await loadState('devName', stateFilePath(devspacePath())); + + exec(`c ts ip ${devName}`, { silent: true }, (err, stdout, stderr) => { + if (err) { + return reportError(new Error(stderr), false, true); + } + + process.stdout.write( + `${stdout.replace(/\n/, '', 'g')}${newline(program.N)}` + ); + }); +}; + +run(); diff --git a/bin/c-dev.js b/bin/c-dev.js new file mode 100644 index 0000000..0210274 --- /dev/null +++ b/bin/c-dev.js @@ -0,0 +1,10 @@ +#!/usr/bin/env -S node + +'use strict'; + +const program = require('commander'); +const { missingCommand } = require('./lib/c'); + +program.command('ip', 'Manage your dev computer.').parse(process.argv); + +missingCommand(program); diff --git a/bin/c-ds-dev-get.js b/bin/c-ds-dev-get.js new file mode 100644 index 0000000..8321fb5 --- /dev/null +++ b/bin/c-ds-dev-get.js @@ -0,0 +1,34 @@ +#!/usr/bin/env -S node + +'use strict'; + +const program = require('commander'); +const { + loadState, + devspacePath, + newline, + reportError, + stateFilePath, +} = require('./lib/c'); + +program + .option('-n', 'Do not print the trailing newline character.') + .parse(process.argv); + +return loadState('devName', stateFilePath(devspacePath())) + .then((data) => { + process.stdout.write(`${data}${newline(program.N)}`); + }) + .catch((err) => { + if (err.code === 'ENOENT') { + return reportError( + new Error( + "Your dev computer name hasn't been configured yet. Use `c ds dev set `." + ), + false, + true + ); + } + + return reportError(err, false, true); + }); diff --git a/bin/c-ds-dev-set.js b/bin/c-ds-dev-set.js new file mode 100644 index 0000000..3684b72 --- /dev/null +++ b/bin/c-ds-dev-set.js @@ -0,0 +1,35 @@ +#!/usr/bin/env -S node + +'use strict'; + +const program = require('commander'); +const { + devspacePath, + reportError, + stateFilePath, + storeState, +} = require('./lib/c'); + +program + .arguments('') + .description("Set your dev computer's name.") + .parse(process.argv); + +const [name] = program.args; + +if (!name) { + return reportError( + new Error("You must provide your dev computer's name."), + program + ); +} + +const run = async () => { + try { + await storeState('devName', name, stateFilePath(devspacePath())); + } catch (err) { + reportError(err, program, true); + } +}; + +run(); diff --git a/bin/c-ds-dev.js b/bin/c-ds-dev.js new file mode 100644 index 0000000..614563d --- /dev/null +++ b/bin/c-ds-dev.js @@ -0,0 +1,13 @@ +#!/usr/bin/env -S node + +'use strict'; + +const program = require('commander'); +const { missingCommand } = require('./lib/c'); + +program + .command('get', 'Get your dev computer name.') + .command('set ', 'Set your dev computer name.') + .parse(process.argv); + +missingCommand(program); diff --git a/bin/c-ds-handle-get.js b/bin/c-ds-handle-get.js index 56f68c4..c304947 100644 --- a/bin/c-ds-handle-get.js +++ b/bin/c-ds-handle-get.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c-ds-handle-set.js b/bin/c-ds-handle-set.js index 121bf19..72758b8 100644 --- a/bin/c-ds-handle-set.js +++ b/bin/c-ds-handle-set.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; @@ -29,6 +29,7 @@ const run = async () => { try { await storeState('handle', handle, file); + await storeState('devName', `${handle}-dev`, file); await storeState('mkName', `${handle}-minikube`, file); await storeState('pcName', `${handle}-pc`, file); } catch (err) { diff --git a/bin/c-ds-handle.js b/bin/c-ds-handle.js index 744a707..103aa24 100644 --- a/bin/c-ds-handle.js +++ b/bin/c-ds-handle.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c-ds-mk-get.js b/bin/c-ds-mk-get.js index affcb52..65a242b 100644 --- a/bin/c-ds-mk-get.js +++ b/bin/c-ds-mk-get.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c-ds-mk-set.js b/bin/c-ds-mk-set.js index cdc262e..3b65798 100644 --- a/bin/c-ds-mk-set.js +++ b/bin/c-ds-mk-set.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c-ds-mk.js b/bin/c-ds-mk.js index cf09567..52891cd 100644 --- a/bin/c-ds-mk.js +++ b/bin/c-ds-mk.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c-ds-pc-get.js b/bin/c-ds-pc-get.js index 4b20798..6969574 100644 --- a/bin/c-ds-pc-get.js +++ b/bin/c-ds-pc-get.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c-ds-pc-set.js b/bin/c-ds-pc-set.js index 41dafb4..60df3ed 100644 --- a/bin/c-ds-pc-set.js +++ b/bin/c-ds-pc-set.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c-ds-pc.js b/bin/c-ds-pc.js index c83e3a2..230e47f 100644 --- a/bin/c-ds-pc.js +++ b/bin/c-ds-pc.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c-ds.js b/bin/c-ds.js index 0a55453..6f532d2 100644 --- a/bin/c-ds.js +++ b/bin/c-ds.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; @@ -7,8 +7,10 @@ const { missingCommand } = require('./lib/c'); program .command('handle', 'Manage your Idearium handle.') - .command('mk', 'Manage your Minikube name.') - .command('pc', 'Manager your computer name.') + .command('mk', 'Manage your minikube name.') + .command('dev', 'Manage your dev computer name.') + .command('pc', 'Manage your computer name.') + .command('target', 'Manage your deployment target.') .parse(process.argv); missingCommand(program); diff --git a/bin/c-gc-cmd.js b/bin/c-gc-cmd.js index e6e283a..74f7a86 100644 --- a/bin/c-gc-cmd.js +++ b/bin/c-gc-cmd.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; const program = require('commander'); diff --git a/bin/c-hosts-add.js b/bin/c-hosts-add.js index 8ee8b2d..941a16b 100644 --- a/bin/c-hosts-add.js +++ b/bin/c-hosts-add.js @@ -1,7 +1,7 @@ 'use strict'; const program = require('commander'); -const { spawnSync } = require('child_process'); +const { execSync, spawnSync } = require('child_process'); const { hostilePath, reportError } = require('./lib/c'); let ip; @@ -27,7 +27,25 @@ if (!domains) { ); } +const findNodePath = () => { + let nodePath; + try { + nodePath = execSync('which node').toString().trim(); + } catch (error) { + console.error('Node.js is not installed or not found in PATH.'); + process.exit(1); + } + + if (!nodePath) { + console.error('Node.js is not installed or not found in PATH.'); + process.exit(1); + } + + return nodePath; +}; + const { status, stderr } = spawnSync('sudo', [ + findNodePath(), hostilePath(), 'set', ip, diff --git a/bin/c-kc-apply.js b/bin/c-kc-apply.js index 3e5ffb6..99d4bf2 100644 --- a/bin/c-kc-apply.js +++ b/bin/c-kc-apply.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; const program = require('commander'); diff --git a/bin/c-kc-cmd.js b/bin/c-kc-cmd.js index 6b59341..92df2ff 100644 --- a/bin/c-kc-cmd.js +++ b/bin/c-kc-cmd.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; const program = require('commander'); diff --git a/bin/c-kc-logs.js b/bin/c-kc-logs.js index c7a9ac8..98222ab 100644 --- a/bin/c-kc-logs.js +++ b/bin/c-kc-logs.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; const program = require('commander'); diff --git a/bin/c-kc-manifests.js b/bin/c-kc-manifests.js index 81f607f..2924387 100644 --- a/bin/c-kc-manifests.js +++ b/bin/c-kc-manifests.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; const program = require('commander'); diff --git a/bin/c-kc-pod.js b/bin/c-kc-pod.js index a8abee1..4279a42 100644 --- a/bin/c-kc-pod.js +++ b/bin/c-kc-pod.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; const program = require('commander'); diff --git a/bin/c-kc-start.js b/bin/c-kc-start.js index 6ff985e..4c84bfc 100644 --- a/bin/c-kc-start.js +++ b/bin/c-kc-start.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; const program = require('commander'); diff --git a/bin/c-kc-stop.js b/bin/c-kc-stop.js index 349a6b3..6e81c3b 100644 --- a/bin/c-kc-stop.js +++ b/bin/c-kc-stop.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; const program = require('commander'); diff --git a/bin/c-ts-ip.js b/bin/c-ts-ip.js index 18e0f68..cfc6e29 100644 --- a/bin/c-ts-ip.js +++ b/bin/c-ts-ip.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c-ts.js b/bin/c-ts.js index d2af9eb..221a953 100644 --- a/bin/c-ts.js +++ b/bin/c-ts.js @@ -1,4 +1,4 @@ -#!/usr/bin/env -S node --trace-warnings +#!/usr/bin/env -S node 'use strict'; diff --git a/bin/c.js b/bin/c.js index e12ac64..7c2dfa7 100755 --- a/bin/c.js +++ b/bin/c.js @@ -9,6 +9,7 @@ const { missingCommand } = require('./lib/c'); program .command('d ', 'Shortcuts to control Docker.') .command('dc ', 'Shortcuts to control Docker Compose.') + .command('dev ', 'Shortcuts to manage your dev computer.') .command('gc ', 'Shortcuts to help with gcloud.') .command('ds ', 'Shortcuts to manage DevSpace.') .command('hosts ', 'Shortcuts to help with hosts management.') diff --git a/bin/lib/c-ts.js b/bin/lib/c-ts.js index d2bfaba..7b1bafd 100644 --- a/bin/lib/c-ts.js +++ b/bin/lib/c-ts.js @@ -1,6 +1,23 @@ 'use strict'; -const app = '/Applications/Tailscale.app/Contents/MacOS/Tailscale'; +const os = require('os'); + +const platform = os.platform(); +let app; + +if (platform === 'darwin') { + // MacOS + app = '/Applications/Tailscale.app/Contents/MacOS/Tailscale'; +} + +if (platform === 'linux') { + // Linux + app = '/usr/bin/tailscale'; +} + +if (!['darwin', 'linux'].includes(platform)) { + throw new Error('Unsupported platform'); +} module.exports = { app,