Initial commit (Sanitized)

This commit is contained in:
sol
2026-02-23 17:14:33 +00:00
commit a0acc38fa6
4 changed files with 245 additions and 0 deletions

110
src/live-status.js Executable file
View File

@@ -0,0 +1,110 @@
#!/usr/bin/env node
const https = require('http'); // Using http for mattermost:8065 (no ssl inside docker network)
const fs = require('fs');
// --- HELPER: PARSE ARGS ---
const args = process.argv.slice(2);
let command = null;
let options = {};
let otherArgs = [];
for (let i = 0; i < args.length; i++) {
if (args[i] === '--channel') {
options.channel = args[i+1];
i++; // Skip next arg (the channel ID)
} else if (!command && (args[i] === 'create' || args[i] === 'update')) {
command = args[i];
} else {
otherArgs.push(args[i]);
}
}
// --- CONFIGURATION (DYNAMIC) ---
const CONFIG = {
host: 'mattermost',
port: 8065,
token: 'DEFAULT_TOKEN_PLACEHOLDER', // Set via install.sh wizard
// Priority: 1. CLI Flag, 2. Env Var, 3. Hardcoded Fallback (Project-0)
channel_id: options.channel || process.env.MM_CHANNEL_ID || process.env.CHANNEL_ID || 'obzja4hb8pd85xk45xn4p31jye'
};
// --- HELPER: HTTP REQUEST ---
function request(method, path, data) {
return new Promise((resolve, reject) => {
const options = {
hostname: CONFIG.host,
port: CONFIG.port,
path: '/api/v4' + path,
method: method,
headers: {
'Authorization': `Bearer ${CONFIG.token}`,
'Content-Type': 'application/json'
}
};
const req = https.request(options, (res) => {
let body = '';
res.on('data', (chunk) => body += chunk);
res.on('end', () => {
if (res.statusCode >= 200 && res.statusCode < 300) {
try {
resolve(JSON.parse(body));
} catch (e) {
resolve(body);
}
} else {
reject(new Error(`Request Failed (${res.statusCode}): ${body}`));
}
});
});
req.on('error', (e) => reject(e));
if (data) req.write(JSON.stringify(data));
req.end();
});
}
// --- COMMANDS ---
async function createPost(text) {
try {
const result = await request('POST', '/posts', {
channel_id: CONFIG.channel_id,
message: text
});
console.log(result.id);
} catch (e) {
console.error(`Error creating post in channel ${CONFIG.channel_id}:`, e.message);
process.exit(1);
}
}
async function updatePost(postId, text) {
try {
const current = await request('GET', `/posts/${postId}`);
await request('PUT', `/posts/${postId}`, {
id: postId,
message: text,
props: current.props
});
console.log("updated");
} catch (e) {
console.error("Error updating post:", e.message);
process.exit(1);
}
}
// --- CLI ROUTER ---
if (command === 'create') {
const text = otherArgs.join(' ');
createPost(text);
} else if (command === 'update') {
const id = otherArgs[0];
const text = otherArgs.slice(1).join(' ');
updatePost(id, text);
} else {
console.log("Usage: live-status [--channel ID] create <text>");
console.log(" live-status update <id> <text>");
process.exit(1);
}