From 17e70f14a28dfeec803d0e21fcc204d06f5bec9b Mon Sep 17 00:00:00 2001 From: tracer Date: Fri, 3 May 2024 19:15:19 +0200 Subject: [PATCH] refactored command groups, migrations:make only available in dev/test --- composer.json | 6 +- src/Controller/CLIController.php | 481 ++++++++++++++++--------------- 2 files changed, 251 insertions(+), 236 deletions(-) diff --git a/composer.json b/composer.json index ffc96d6..e1083d8 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { - "name": "24unix/bindapi", - "description": "manage Bind9 DNS server via REST API", + "name": "tracer/bindapi", + "description": "manage Bind9 client zones for KeyHelp", "version": "1.0.9", - "build_number": "375", + "build_number": "376", "authors": [ { "name": "Micha Espey", diff --git a/src/Controller/CLIController.php b/src/Controller/CLIController.php index e9c3b15..572531e 100644 --- a/src/Controller/CLIController.php +++ b/src/Controller/CLIController.php @@ -63,243 +63,258 @@ class CLIController { $this->baseDir = dirname(path: __DIR__, levels: 2) . '/'; - $this->commandGroupContainer = (new CommandGroupContainer()) - ->addCommandGroup(commandGroup: (new CommandGroup(name: 'apikeys', description: 'API keys to access this bindAPI')) - ->addCommand(command: new Command( - name: 'list', - callback: function () { - $this->apikeysList(); - } - ) - ) - ->addCommand(command: new Command( - name: 'create', - callback: function () { - $this->apikeysCreate(); - }, - mandatoryParameters: ['name']) - ) - ->addCommand(command: new Command( - name: 'update', - callback: function () { - $this->apikeysUpdate(); - }, - mandatoryParameters: ['ID',], - optionalParameters: ['name='])) - ->addCommand(command: new Command( - name: 'delete', - callback: function () { - $this->apikeysDelete(); - }, - mandatoryParameters: ['ID']))) - ->addCommandGroup(commandGroup: (new CommandGroup(name: 'cron', description: 'Run zone file maintenance')) - ->addCommand(command: new Command( - name: 'run', - callback: function () { - $this->cronRun(); - }))) - ->addCommandGroup(commandGroup: (new CommandGroup(name: 'check', description: 'health checks the system can perform')) - ->addCommand(command: new Command( - name: 'cache', - callback: function () { - $this->checkCache(); - }, - description: 'Clear the opcache. Note: It doesn\'t call composer clear-cache.')) - ->addCommand(command: new Command( - name: 'permissions', - callback: function () { - $this->checkPermissions(); - }, - description: 'check file permissions')) - ->addCommand(command: new Command( - name: 'panel', - callback: function () { - $this->checkPanel(); - }, - description: 'Validate setting for this panel')) - ->addCommand(command: new Command( - name: 'panels', - callback: function () { - $this->checkPanels(); - }, - optionalParameters: ['ID', 'fix=xes'])) - ->addCommand(command: new Command( - name: 'nameserver', - callback: function () { - $this->checkNameserver(); - }, - description: 'Validate setting for this panel')) - ->addCommand(command: new Command( - name: 'nameservers', - callback: function () { - $this->checkNameservers(); - }, - description: 'Check version on nameservers')) - ->addCommand(command: new Command( - name: 'domains', - callback: function () { - $this->checkDomains(); - })) - ->addCommand(command: new Command( - name: 'mail', - callback: function () { - $this->checkMail(); - })) - ->addCommand(command: new Command( - name: 'showincludes', - callback: function () { - $this->checkShowIncludes(); - }, - description: 'Shows needed setting on panels')) - ->addCommand(command: new Command( - name: 'generatekey', - callback: function () { - $this->checkGenerateKey(); - }, - description: 'Generates a a new key for encryption')) - ->addCommand(command: new Command( - name: 'setup', - callback: function () { - $this->checkSetup(); - }, - mandatoryParameters: ['username'], - description: 'Adapt filesystem permissions (requires elaborated permissions)')) - ->addCommand(command: new Command( - name: 'version', - callback: function () { - $this->checkVersion(); - }, - optionalParameters: ['update'], - description: 'Read or set the bindApi version in the database'))) - ->addCommandGroup(commandGroup: (new CommandGroup(name: 'panels', description: 'all KeyHelp systems configured')) - ->addCommand(command: new Command( - name: 'list', - callback: function () { - $this->panelsList(); - })) - ->addCommand(command: new Command( - name: 'create', - callback: function () { - $this->panelsCreate(); - }, - mandatoryParameters: ['name'], - optionalParameters: ['A=', 'AAAA=', 'apikey=', 'self='])) - ->addCommand(command: new Command( - name: 'update', - callback: function () { - $this->panelsUpdate(); - }, - mandatoryParameters: ['ID'], - optionalParameters: ['name=', 'A=', 'AAAA=', 'apikey=', 'self='])) - ->addCommand(command: new Command( - name: 'delete', - callback: function () { - $this->panelsDelete(); - }, - mandatoryParameters: ['ID'])) - ->addCommand(command: new Command( - name: 'apiping', - callback: function () { - $this->apiPing(type: 'panel'); - }, - optionalParameters: ['ID']))) - ->addCommandGroup(commandGroup: (new CommandGroup(name: 'nameservers', description: 'available nameservers')) - ->addCommand(command: new Command( - name: 'list', - callback: function () { - $this->nameserversList(); - })) - ->addCommand(command: new Command( - name: 'create', - callback: function () { - $this->nameserversCreate(); - }, - mandatoryParameters: ['name'], - optionalParameters: ['A=', 'AAAA=', 'apikey='])) - ->addCommand(command: new Command( - name: 'update', - callback: function () { - $this->nameserversUpdate(); - }, - mandatoryParameters: ['ID'], - optionalParameters: ['name=', 'A=', 'AAAA=', 'apikey='])) - ->addCommand(command: new Command( - name: 'delete', - callback: function () { - $this->nameserversDelete(); - }, - mandatoryParameters: ['ID'])) - ->addCommand(command: new Command( - name: 'apiping', - callback: function () { - $this->apiPing(type: 'nameserver'); - }, - optionalParameters: ['ID']))) - ->addCommandGroup(commandGroup: (new CommandGroup(name: 'domains', description: 'configured domains')) - ->addCommand(command: new Command( - name: 'list', - callback: function () { - $this->domainsList(); - })) - ->addCommand(command: new Command( - name: 'update', - callback: function () { - $this->domainsUpdate(); - }, + $apikeyGroup = (new CommandGroup(name: 'apikeys', description: 'API keys to access this bindAPI')) + ->addCommand(command: new Command( + name: 'list', + callback: function () { + $this->apikeysList(); + }) + ) + ->addCommand(command: new Command( + name: 'create', + callback: function () { + $this->apikeysCreate(); + }, + mandatoryParameters: ['name']) + ) + ->addCommand(command: new Command( + name: 'update', + callback: function () { + $this->apikeysUpdate(); + }, + mandatoryParameters: ['ID',], + optionalParameters: ['name='])) + ->addCommand(command: new Command( + name: 'delete', + callback: function () { + $this->apikeysDelete(); + }, + mandatoryParameters: ['ID'] + )); + $cronGroup = (new CommandGroup(name: 'cron', description: 'Run zone file maintenance')) + ->addCommand(command: new Command( + name: 'run', + callback: function () { + $this->cronRun(); + })); + $panelsGroup = (new CommandGroup(name: 'panels', description: 'all KeyHelp systems configured')) + ->addCommand(command: new Command( + name: 'list', + callback: function () { + $this->panelsList(); + })) + ->addCommand(command: new Command( + name: 'create', + callback: function () { + $this->panelsCreate(); + }, + mandatoryParameters: ['name'], + optionalParameters: ['A=', 'AAAA=', 'apikey=', 'self='])) + ->addCommand(command: new Command( + name: 'update', + callback: function () { + $this->panelsUpdate(); + }, + mandatoryParameters: ['ID'], + optionalParameters: ['name=', 'A=', 'AAAA=', 'apikey=', 'self='])) + ->addCommand(command: new Command( + name: 'delete', + callback: function () { + $this->panelsDelete(); + }, + mandatoryParameters: ['ID'])) + ->addCommand(command: new Command( + name: 'apiping', + callback: function () { + $this->apiPing(type: 'panel'); + }, + optionalParameters: ['ID'])); + $domainsGroup = (new CommandGroup(name: 'domains', description: 'configured domains')) + ->addCommand(command: new Command( + name: 'list', + callback: function () { + $this->domainsList(); + })) + ->addCommand(command: new Command( + name: 'update', + callback: function () { + $this->domainsUpdate(); + }, // mandatoryParameters: ['name'], // optionalParameters: ['A=', 'AAAA=', 'apikey=']))) - description: 'Update zone files'))) - ->addCommandGroup(commandGroup: (new CommandGroup(name: 'dyndns', description: 'handle DynDNS entries')) - ->addCommand(command: new Command( - name: 'list', - callback: function () { - $this->dynDnsList(); - })) - ->addCommand(command: new Command( - name: 'create', - callback: function () { - $this->dynDnsCreate(); - }, - mandatoryParameters: ['hostname.example.com', 'password'], - optionalParameters: ['A=', 'AAAA='], - description: 'FQDN within a domain where this server is master')) - ->addCommand(command: new Command( - name: 'update', - callback: function () { - $this->dynDnyUpdate(); - }, - mandatoryParameters: ['hostname.example.com',], - optionalParameters: ['password=', 'A=', 'AAAA='])) - ->addCommand(command: new Command( - name: 'delete', - callback: function () { - $this->dynDnsDelete(); - }, - mandatoryParameters: ['ID']))) - ->addCommandGroup(commandGroup: (new CommandGroup(name: 'migrations', description: 'maintain database migrations')) - ->addCommand(command: new Command( - name: 'status', - callback: function () { - $this->migrationsStatus(); - }, - description: 'List information about migrations' - )) - ->addCommand(command: new Command( - name: 'migrate', - callback: function () { - $this->migrationsMigrate(); - }, - description: 'Apply a new migration file' - )) - ->addCommand(command: new Command( - name: 'make', - callback: function () { - $this->migrationsMake(); - }, - description: '!Use only on dev environments, not in production! Build a new migration file' - )) + description: 'Update zone files')); + $nameserversGroup = (new CommandGroup(name: 'nameservers', description: 'available nameservers')) + ->addCommand(command: new Command( + name: 'list', + callback: function () { + $this->nameserversList(); + })) + ->addCommand(command: new Command( + name: 'create', + callback: function () { + $this->nameserversCreate(); + }, + mandatoryParameters: ['name'], + optionalParameters: ['A=', 'AAAA=', 'apikey='])) + ->addCommand(command: new Command( + name: 'update', + callback: function () { + $this->nameserversUpdate(); + }, + mandatoryParameters: ['ID'], + optionalParameters: ['name=', 'A=', 'AAAA=', 'apikey='])) + ->addCommand(command: new Command( + name: 'delete', + callback: function () { + $this->nameserversDelete(); + }, + mandatoryParameters: ['ID'])) + ->addCommand(command: new Command( + name: 'apiping', + callback: function () { + $this->apiPing(type: 'nameserver'); + }, + optionalParameters: ['ID'])); + $dyndnsGroup = (new CommandGroup(name: 'dyndns', description: 'handle DynDNS entries')) + ->addCommand(command: new Command( + name: 'list', + callback: function () { + $this->dynDnsList(); + })) + ->addCommand(command: new Command( + name: 'create', + callback: function () { + $this->dynDnsCreate(); + }, + mandatoryParameters: ['hostname.example.com', 'password'], + optionalParameters: ['A=', 'AAAA='], + description: 'FQDN within a domain where this server is master')) + ->addCommand(command: new Command( + name: 'update', + callback: function () { + $this->dynDnyUpdate(); + }, + mandatoryParameters: ['hostname.example.com',], + optionalParameters: ['password=', 'A=', 'AAAA='])) + ->addCommand(command: new Command( + name: 'delete', + callback: function () { + $this->dynDnsDelete(); + }, + mandatoryParameters: ['ID'])); + $checkGroup = (new CommandGroup(name: 'check', description: 'health checks the system can perform')) + ->addCommand(command: new Command( + name: 'cache', + callback: function () { + $this->checkCache(); + }, + description: 'Clear the opcache. Note: It doesn\'t call composer clear-cache.')) + ->addCommand(command: new Command( + name: 'permissions', + callback: function () { + $this->checkPermissions(); + }, + description: 'check file permissions')) + ->addCommand(command: new Command( + name: 'panel', + callback: function () { + $this->checkPanel(); + }, + description: 'Validate setting for this panel')) + ->addCommand(command: new Command( + name: 'panels', + callback: function () { + $this->checkPanels(); + }, + optionalParameters: ['ID', 'fix=xes'])) + ->addCommand(command: new Command( + name: 'nameserver', + callback: function () { + $this->checkNameserver(); + }, + description: 'Validate setting for this panel')) + ->addCommand(command: new Command( + name: 'nameservers', + callback: function () { + $this->checkNameservers(); + }, + description: 'Check version on nameservers')) + ->addCommand(command: new Command( + name: 'domains', + callback: function () { + $this->checkDomains(); + })) + ->addCommand(command: new Command( + name: 'mail', + callback: function () { + $this->checkMail(); + })) + ->addCommand(command: new Command( + name: 'showincludes', + callback: function () { + $this->checkShowIncludes(); + }, + description: 'Shows needed setting on panels')) + ->addCommand(command: new Command( + name: 'generatekey', + callback: function () { + $this->checkGenerateKey(); + }, + description: 'Generates a a new key for encryption')) + ->addCommand(command: new Command( + name: 'setup', + callback: function () { + $this->checkSetup(); + }, + mandatoryParameters: ['username'], + description: 'Adapt filesystem permissions (requires elaborated permissions)')) + ->addCommand(command: new Command( + name: 'version', + callback: function () { + $this->checkVersion(); + }, + optionalParameters: ['update'], + description: 'Read or set the bindApi version in the database')); + $migrationsGroup = (new CommandGroup(name: 'migrations', description: 'maintain database migrations')) + ->addCommand(command: new Command( + name: 'status', + callback: function () { + $this->migrationsStatus(); + }, + description: 'List information about migrations' + )) + ->addCommand(command: new Command( + name: 'migrate', + callback: function () { + $this->migrationsMigrate(); + }, + description: 'Apply a new migration file' + )); + $env = $this->configController->getConfig(configKey: 'env'); + $makeEnvs = ['dev', 'test']; + + if (in_array(needle: $env, haystack: $makeEnvs)) { + $makeMigrationsCommand = new Command( + name: 'make', + callback: function () { + $this->migrationsMake(); + }, + description: 'Build a new migration file' ); + $migrationsGroup->addCommand(command: $makeMigrationsCommand); + } + + $this->commandGroupContainer = (new CommandGroupContainer()) + ->addCommandGroup(commandGroup: $apikeyGroup) + ->addCommandGroup($cronGroup) + ->addCommandGroup($panelsGroup) + ->addCommandGroup($domainsGroup) + ->addCommandGroup($nameserversGroup) + ->addCommandGroup($dyndnsGroup) + ->addCommandGroup($checkGroup) + ->addCommandGroup($migrationsGroup); + // ->addCommandGroup(commandGroup: (new CommandGroup(name: 'webmail', description: 'manage webmail setup')) // ->addCommand(command: new Command(