gh-legacy
Version:
A powerful GitHub CLI tool for automatic repository ownership transfer to trusted beneficiaries after periods of inactivity
99 lines (86 loc) ⢠3.83 kB
JavaScript
// commands/list.js
const fs = require('fs');
const path = require('path');
const CONFIG_PATH = path.join(__dirname, '../db.json');
function formatTimeRemaining(milliseconds) {
const seconds = Math.floor(milliseconds / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
const months = Math.floor(days / 30);
const years = Math.floor(months / 12);
if (years > 0) return `${years} year${years > 1 ? 's' : ''}`;
if (months > 0) return `${months} month${months > 1 ? 's' : ''}`;
if (days > 0) return `${days} day${days > 1 ? 's' : ''}`;
if (hours > 0) return `${hours} hour${hours > 1 ? 's' : ''}`;
if (minutes > 0) return `${minutes} minute${minutes > 1 ? 's' : ''}`;
return `${seconds} second${seconds > 1 ? 's' : ''}`;
}
exports.listBeneficiaries = () => {
if (!fs.existsSync(CONFIG_PATH)) {
console.error('ā No configuration found. Run "gh-legacy init" first.');
return;
}
const config = JSON.parse(fs.readFileSync(CONFIG_PATH));
const now = Date.now();
const lastHeartbeat = new Date(config.lastHeartbeat).getTime();
const timeSinceHeartbeat = now - lastHeartbeat;
console.log('\nš GH-Legacy Configuration');
console.log('='.repeat(50));
console.log(`ā° Last Heartbeat: ${new Date(config.lastHeartbeat).toLocaleString()}`);
console.log(`ā±ļø Time Since Last Heartbeat: ${formatTimeRemaining(timeSinceHeartbeat)}`);
console.log(`š„ Total Beneficiaries: ${config.beneficiaries.length}`);
console.log('');
if (config.beneficiaries.length === 0) {
console.log('š No beneficiaries configured yet.');
console.log('š” Use "gh-legacy add-beneficiary" to add beneficiaries.');
return;
}
config.beneficiaries.forEach((b, index) => {
console.log(`\n${index + 1}. š Beneficiary Details`);
console.log('ā'.repeat(40));
console.log(`šļø Repository: ${b.repo}`);
console.log(`š¤ GitHub Username: ${b.githubUsername}`);
console.log(`š§ Email: ${b.email}`);
console.log(`ā³ Access After: ${b.accessAfter}`);
console.log(`š
Added: ${new Date(b.addedAt).toLocaleDateString()}`);
if (b.granted) {
console.log(`ā
Status: ACCESS GRANTED`);
console.log(`šÆ Granted: ${new Date(b.grantedAt).toLocaleString()}`);
} else {
// Calculate if access should be granted
const accessDelay = parseDuration(b.accessAfter);
const shouldGrant = timeSinceHeartbeat >= accessDelay;
if (shouldGrant) {
console.log(`šØ Status: READY FOR ACCESS (conditions met!)`);
console.log(`š” Run "gh-legacy trigger-access" to grant access`);
} else {
const remaining = accessDelay - timeSinceHeartbeat;
console.log(`ā³ Status: WAITING (${formatTimeRemaining(remaining)} remaining)`);
}
}
});
console.log('\nš” Commands:');
console.log(' gh-legacy heartbeat - Send a heartbeat');
console.log(' gh-legacy trigger-access - Check and grant access');
console.log(' gh-legacy add-beneficiary - Add new beneficiary');
};
function parseDuration(durationStr) {
const [value, unit] = durationStr.toLowerCase().split(' ');
const num = parseInt(value);
switch (unit) {
case 'minutes':
case 'minute': return num * 60 * 1000;
case 'hours':
case 'hour': return num * 60 * 60 * 1000;
case 'days':
case 'day': return num * 24 * 60 * 60 * 1000;
case 'weeks':
case 'week': return num * 7 * 24 * 60 * 60 * 1000;
case 'months':
case 'month': return num * 30 * 24 * 60 * 60 * 1000;
case 'years':
case 'year': return num * 365 * 24 * 60 * 60 * 1000;
default: return 0;
}
}