#!/usr/bin/env php * * This file is part of a Moko Consulting project. * * SPDX-License-Identifier: GPL-3.0-or-later * * FILE INFORMATION * DEFGROUP: moko-platform.CLI * INGROUP: moko-platform * REPO: https://git.mokoconsulting.tech/MokoConsulting/moko-platform * PATH: /cli/scaffold_client.php * VERSION: 09.22.00 * BRIEF: Scaffold a new client-waas repo from Template-Client-WaaS with pre-configured settings */ declare(strict_types=1); require_once __DIR__ . '/../lib/Enterprise/CliFramework.php'; use MokoEnterprise\CliFramework; class ScaffoldClientCli extends CliFramework { protected function configure(): void { $this->setDescription('Scaffold a new client-waas repo from Template-Client-WaaS'); $this->addArgument('--name', 'Client name', ''); $this->addArgument('--org', 'Gitea organization', ''); $this->addArgument('--gitea-url', 'Gitea URL', 'https://git.mokoconsulting.tech'); $this->addArgument('--token', 'Gitea API token', ''); } protected function run(): int { $name = $this->getArgument('--name'); $org = $this->getArgument('--org'); $giteaUrl = rtrim($this->getArgument('--gitea-url'), '/'); $token = $this->getArgument('--token'); if ($name === '' || $org === '' || $token === '') { $this->log('ERROR', '--name, --org, and --token are required.'); return 1; } $repoName = 'client-waas-' . $name; $this->log('INFO', "Scaffolding client repo: {$org}/{$repoName}"); $this->log('INFO', "Gitea URL: {$giteaUrl}"); if ($this->dryRun) { $this->log('INFO', '[DRY RUN] Would create repo from template MokoConsulting/Template-Client-WaaS'); $this->log('INFO', "[DRY RUN] Repo: {$org}/{$repoName}"); $this->printPostSetupInstructions($repoName, $giteaUrl, $org); return 0; } $this->log('INFO', 'Step 1: Creating repo from template...'); $createPayload = json_encode([ 'owner' => $org, 'name' => $repoName, 'description' => "{$name} WaaS site", 'private' => true, 'git_content' => true, 'topics' => true, 'labels' => true, ]); $response = $this->apiRequest( 'POST', "/api/v1/repos/MokoConsulting/Template-Client-WaaS/generate", $giteaUrl, $token, $createPayload ); if ($response['code'] < 200 || $response['code'] >= 300) { $this->log('ERROR', "Failed to create repo (HTTP {$response['code']})."); return 1; } $this->log('INFO', "Repo created: {$org}/{$repoName}"); $this->log('INFO', 'Step 2: Updating repo description...'); $this->apiRequest('PATCH', "/api/v1/repos/{$org}/{$repoName}", $giteaUrl, $token, json_encode(['description' => "{$name} WaaS site"])); $this->log('INFO', 'Step 3: Creating dev branch from main...'); $response = $this->apiRequest( 'POST', "/api/v1/repos/{$org}/{$repoName}/branches", $giteaUrl, $token, json_encode([ 'new_branch_name' => 'dev', 'old_branch_name' => 'main', ]) ); if ($response['code'] >= 200 && $response['code'] < 300) { $this->log('INFO', 'Branch "dev" created from "main".'); } else { $this->log('WARN', "Could not create dev branch (HTTP {$response['code']})."); } $this->printPostSetupInstructions($repoName, $giteaUrl, $org); $this->log('INFO', 'Scaffold complete.'); return 0; } private function printPostSetupInstructions(string $repoName, string $giteaUrl, string $org): void { fwrite(STDERR, "\n=== POST-SETUP INSTRUCTIONS ===\n\n" . "Navigate to: {$giteaUrl}/{$org}/{$repoName}/settings\n\n" . "Set REPO VARIABLES:\n" . " DEV_SYNC_HOST, DEV_SYNC_PORT, DEV_SYNC_USER, DEV_SYNC_PATH\n" . " LIVE_SSH_HOST, LIVE_SSH_PORT, LIVE_SSH_USER, LIVE_SYNC_PATH\n\n" . "Set REPO SECRETS:\n" . " DEV_SYNC_KEY, LIVE_SSH_KEY\n\n" . "================================\n"); } private function apiRequest(string $method, string $endpoint, string $giteaUrl, string $token, ?string $body = null): array { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $giteaUrl . $endpoint); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json', 'Accept: application/json', "Authorization: token {$token}"]); if ($body !== null) { curl_setopt($ch, CURLOPT_POSTFIELDS, $body); } $responseBody = curl_exec($ch); $httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); if (curl_errno($ch)) { $error = curl_error($ch); curl_close($ch); return ['code' => 0, 'body' => "cURL error: {$error}"]; } curl_close($ch); return ['code' => $httpCode, 'body' => $responseBody]; } } $app = new ScaffoldClientCli(); exit($app->execute());