Spaces:
Runtime error
Runtime error
File size: 2,411 Bytes
fb15d01 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | const fs = require('fs');
const path = require('path');
const https = require('https');
// Usage: node scripts/sync_hf_secrets.js <namespace/space-name> <hf-token>
const args = process.argv.slice(2);
if (args.length < 2) {
console.error('Usage: node scripts/sync_hf_secrets.js <namespace/space-name> <hf-token>');
process.exit(1);
}
const repoId = args[0];
const token = args[1];
const envPath = path.join(__dirname, '..', '.env');
if (!fs.existsSync(envPath)) {
console.error('.env file not found!');
process.exit(1);
}
const envContent = fs.readFileSync(envPath, 'utf8');
const lines = envContent.split('\n');
const secrets = {};
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || trimmed.startsWith('#')) continue;
const idx = trimmed.indexOf('=');
if (idx === -1) continue;
const key = trimmed.substring(0, idx).trim();
const value = trimmed.substring(idx + 1).trim().replace(/^['"]|['"]$/g, '');
if (key && value) {
secrets[key] = value;
}
}
console.log(`Found ${Object.keys(secrets).length} secrets in .env. Starting sync to ${repoId}...`);
async function setSecret(key, value) {
return new Promise((resolve, reject) => {
const data = JSON.stringify({ key, value });
const options = {
hostname: 'huggingface.co',
path: `/api/spaces/${repoId}/secrets`,
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(data)
}
};
const req = https.request(options, (res) => {
if (res.statusCode === 200 || res.statusCode === 201 || res.statusCode === 400) {
// HF sometimes returns 400 if it already exists or invalid payload, but mostly it works.
resolve();
} else {
res.on('data', d => reject(new Error(`Failed to set ${key}: ${res.statusCode} ${d}`)));
}
});
req.on('error', (e) => reject(e));
req.write(data);
req.end();
});
}
async function sync() {
let success = 0;
for (const [key, value] of Object.entries(secrets)) {
try {
await setSecret(key, value);
console.log(`✅ Set secret: ${key}`);
success++;
} catch (e) {
console.error(`❌ Failed to set ${key}: ${e.message}`);
}
}
console.log(`\nSync complete! Successfully synced ${success}/${Object.keys(secrets).length} secrets.`);
}
sync();
|