Skip to content

Commit

Permalink
Improvements for c on Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
smebberson committed Jun 16, 2024
1 parent 1d429c4 commit 99c36ef
Show file tree
Hide file tree
Showing 28 changed files with 191 additions and 24 deletions.
36 changes: 36 additions & 0 deletions bin/c-dev-ip.js
Original file line number Diff line number Diff line change
@@ -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();
10 changes: 10 additions & 0 deletions bin/c-dev.js
Original file line number Diff line number Diff line change
@@ -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);
34 changes: 34 additions & 0 deletions bin/c-ds-dev-get.js
Original file line number Diff line number Diff line change
@@ -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 <dev-name>`."
),
false,
true
);
}

return reportError(err, false, true);
});
35 changes: 35 additions & 0 deletions bin/c-ds-dev-set.js
Original file line number Diff line number Diff line change
@@ -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('<dev-name>')
.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();
13 changes: 13 additions & 0 deletions bin/c-ds-dev.js
Original file line number Diff line number Diff line change
@@ -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 <dev-name>', 'Set your dev computer name.')
.parse(process.argv);

missingCommand(program);
2 changes: 1 addition & 1 deletion bin/c-ds-handle-get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
3 changes: 2 additions & 1 deletion bin/c-ds-handle-set.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion bin/c-ds-handle.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
2 changes: 1 addition & 1 deletion bin/c-ds-mk-get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
2 changes: 1 addition & 1 deletion bin/c-ds-mk-set.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
2 changes: 1 addition & 1 deletion bin/c-ds-mk.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
2 changes: 1 addition & 1 deletion bin/c-ds-pc-get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
2 changes: 1 addition & 1 deletion bin/c-ds-pc-set.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
2 changes: 1 addition & 1 deletion bin/c-ds-pc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
8 changes: 5 additions & 3 deletions bin/c-ds.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand All @@ -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);
2 changes: 1 addition & 1 deletion bin/c-gc-cmd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node
'use strict';

const program = require('commander');
Expand Down
20 changes: 19 additions & 1 deletion bin/c-hosts-add.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion bin/c-kc-apply.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node
'use strict';

const program = require('commander');
Expand Down
2 changes: 1 addition & 1 deletion bin/c-kc-cmd.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node
'use strict';

const program = require('commander');
Expand Down
2 changes: 1 addition & 1 deletion bin/c-kc-logs.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node
'use strict';

const program = require('commander');
Expand Down
2 changes: 1 addition & 1 deletion bin/c-kc-manifests.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node
'use strict';

const program = require('commander');
Expand Down
2 changes: 1 addition & 1 deletion bin/c-kc-pod.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node
'use strict';

const program = require('commander');
Expand Down
2 changes: 1 addition & 1 deletion bin/c-kc-start.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node
'use strict';

const program = require('commander');
Expand Down
2 changes: 1 addition & 1 deletion bin/c-kc-stop.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node
'use strict';

const program = require('commander');
Expand Down
2 changes: 1 addition & 1 deletion bin/c-ts-ip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
2 changes: 1 addition & 1 deletion bin/c-ts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env -S node --trace-warnings
#!/usr/bin/env -S node

'use strict';

Expand Down
1 change: 1 addition & 0 deletions bin/c.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { missingCommand } = require('./lib/c');
program
.command('d <command>', 'Shortcuts to control Docker.')
.command('dc <command>', 'Shortcuts to control Docker Compose.')
.command('dev <command>', 'Shortcuts to manage your dev computer.')
.command('gc <command>', 'Shortcuts to help with gcloud.')
.command('ds <command>', 'Shortcuts to manage DevSpace.')
.command('hosts <command>', 'Shortcuts to help with hosts management.')
Expand Down
19 changes: 18 additions & 1 deletion bin/lib/c-ts.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 99c36ef

Please sign in to comment.