@arifbiswas/git-pre-push 1.0.12 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Terminal output for `gitsafty vault <sub>` (shared by the CLI and the menu).
3
+ */
4
+
5
+ const { colors } = require('./reporter');
6
+ const { printHint } = require('./doctor');
7
+
8
+ /**
9
+ * @param {string} sub setup | status | test | remove
10
+ * @param {{ host?: string, passphrase?: boolean }} [options]
11
+ * @returns {Promise<number>} exit code
12
+ */
13
+ async function runVaultCommand(sub = 'status', { host, passphrase = false } = {}) {
14
+ const vault = require('./vault');
15
+ sub = String(sub || 'status').toLowerCase();
16
+
17
+ if (sub === 'setup' || sub === 'on' || sub === 'enable') {
18
+ const res = await vault.setupVault({ host: host || 'github.com', mode: passphrase ? 'passphrase' : 'auto' });
19
+ if (!res.success) {
20
+ console.error(`\n${colors.red}${colors.bold}✖ ${res.message}${colors.reset}`);
21
+ printHint(new Error(res.message));
22
+ return 1;
23
+ }
24
+ console.log(`\n${colors.green}${colors.bold}✔ ${res.message}${colors.reset}`);
25
+ for (const step of res.steps) console.log(` ${colors.dim}•${colors.reset} ${step}`);
26
+ for (const key of res.sshWarnings) {
27
+ console.log(`\n${colors.yellow}⚠ SSH key without passphrase: ${key}${colors.reset}`);
28
+ console.log(` ${colors.dim}Anyone who copies it can push as you. Protect it: ${colors.reset}ssh-keygen -p -f ${key}`);
29
+ }
30
+ console.log('');
31
+ return 0;
32
+ }
33
+
34
+ if (sub === 'test') {
35
+ const res = vault.testUnlock();
36
+ console.log(`\n${res.success ? colors.green + '✔' : colors.red + '✖'} ${res.message}${colors.reset}\n`);
37
+ if (!res.success) printHint(new Error(res.message));
38
+ return res.success ? 0 : 1;
39
+ }
40
+
41
+ if (sub === 'remove' || sub === 'off' || sub === 'disable') {
42
+ const res = vault.removeVault({ host });
43
+ console.log(`\n${colors.yellow}${colors.bold}✔ ${res.message}${colors.reset}\n`);
44
+ return 0;
45
+ }
46
+
47
+ const st = vault.vaultStatus({ host });
48
+ const ok = (b) => (b ? `${colors.green}✔` : `${colors.red}✖`) + colors.reset;
49
+ console.log(`\n${colors.bold}GitSafty Vault (${st.host})${colors.reset}`);
50
+ console.log(` ${ok(st.vaultExists)} Encrypted token vault ${st.vaultExists ? `(${st.username}, file mode ${st.vaultMode})` : '(not set up: run gitsafty vault setup)'}`);
51
+ if (st.vaultExists) {
52
+ const hw = st.protection !== 'passphrase';
53
+ console.log(` ${ok(hw)} Locked to this device's security chip ${hw ? `(${st.protection})` : '(passphrase only: run vault setup again on a Mac with Secure Enclave or Windows with Hello)'}`);
54
+ }
55
+ if (st.tokenExpiresAt) console.log(` ${colors.dim}Token expires: ${st.tokenExpiresAt}${colors.reset}`);
56
+ console.log(` ${ok(st.helperActive)} GitSafty is git's credential helper for ${st.host}`);
57
+ console.log(` ${ok(st.otherHelpers.length === 0)} No other helper can store or hand out a token${st.otherHelpers.length ? `: ${st.otherHelpers.join(', ')}` : ''}`);
58
+ console.log(` ${ok(st.sshWarnings.length === 0)} SSH private keys are passphrase-protected${st.sshWarnings.length ? `: ${st.sshWarnings.join(', ')}` : ''}`);
59
+ console.log('');
60
+ return 0;
61
+ }
62
+
63
+ module.exports = { runVaultCommand };