simplified install command

This commit is contained in:
denisdulici 2019-11-21 19:36:35 +03:00
parent 8c2e12a3fa
commit 2cb3f117a5
2 changed files with 65 additions and 108 deletions

View File

@ -30,7 +30,7 @@ Akaunting uses [Laravel](http://laravel.com), the best existing PHP framework, a
* Finally, launch the [web installer](https://akaunting.com/docs/installation) or run the following command: * Finally, launch the [web installer](https://akaunting.com/docs/installation) or run the following command:
```bash ```bash
php artisan install --db-host="localhost" --db-name="my_db" --db-username="my_admin" --db-password="my_pass" --company-name="My Company" --company-email="my@company.com" --admin-email="my@company.com" --admin-password="123456" php artisan install --db-name="akaunting" --db-username="root" --db-password="pass" --admin-email="admin@company.com" --admin-password="123456"
``` ```
## Contributing ## Contributing

View File

@ -22,33 +22,19 @@ class Install extends Command
const OPT_LOCALE = 'locale'; const OPT_LOCALE = 'locale';
const OPT_NO_INTERACTION = 'no-interaction'; const OPT_NO_INTERACTION = 'no-interaction';
public $dbHost;
public $dbPort;
public $dbName;
public $dbUsername;
public $dbPassword;
public $companyName;
public $companyEmail;
public $adminEmail;
public $adminPassword;
public $locale;
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'install protected $signature = 'install
{--db-host= : Database host} {--db-host=localhost : Database host}
{--db-port=3306 : Port of the database host} {--db-port=3306 : Port of the database host}
{--db-name= : Name of the database} {--db-name= : Name of the database}
{--db-username= : Username to use to access the database} {--db-username= : Username to use to access the database}
{--db-password= : Password to use to access the database} {--db-password= : Password to use to access the database}
{--company-name= : Name of the company managed buy the app} {--company-name=My Company : Name of the company}
{--company-email= : email used to contact the company} {--company-email=my@company.com : Email of the company}
{--admin-email= : Admin user email} {--admin-email= : Admin user email}
{--admin-password= : Admin user password} {--admin-password= : Admin user password}
{--locale=en-GB : Language used in the app} {--locale=en-GB : Language used in the app}
@ -61,14 +47,6 @@ class Install extends Command
*/ */
protected $description = 'Allows to install Akaunting directly through CLI'; protected $description = 'Allows to install Akaunting directly through CLI';
/**
* Create a new command instance.
*/
public function __construct()
{
parent::__construct();
}
/** /**
* Execute the console command. * Execute the console command.
* *
@ -76,11 +54,10 @@ class Install extends Command
*/ */
public function handle() public function handle()
{ {
$missingOptions = $this->checkOptions(); if (($missing_options = $this->getMissingOptions()) && $this->option(self::OPT_NO_INTERACTION)) {
if (!empty($missingOptions) && $this->option(self::OPT_NO_INTERACTION)) {
$this->line('❌ Some options are missing and --no-interaction is present. Please run the following command for more informations :'); $this->line('❌ Some options are missing and --no-interaction is present. Please run the following command for more informations :');
$this->line('❌ php artisan help install'); $this->line('❌ php artisan help install');
$this->line('❌ Missing options are : ' . join(', ', $missingOptions)); $this->line('❌ Missing options are : ' . implode(', ', $missing_options));
return self::CMD_ERROR; return self::CMD_ERROR;
} }
@ -99,10 +76,10 @@ class Install extends Command
} }
$this->line('Creating company'); $this->line('Creating company');
Installer::createCompany($this->companyName, $this->companyEmail, $this->locale); Installer::createCompany($this->company_name, $this->company_email, $this->locale);
$this->line('Creating admin'); $this->line('Creating admin');
Installer::createUser($this->adminEmail, $this->adminPassword, $this->locale); Installer::createUser($this->admin_email, $this->admin_password, $this->locale);
$this->line('Applying the final touches'); $this->line('Applying the final touches');
Installer::finalTouches(); Installer::finalTouches();
@ -113,61 +90,41 @@ class Install extends Command
/** /**
* Check that all options are presents. otherwise returns an array of the missing options * Check that all options are presents. otherwise returns an array of the missing options
*/ */
private function checkOptions() private function getMissingOptions()
{ {
$missingOptions = array(); $missing_options = [];
$this->locale = $this->option(self::OPT_LOCALE); $contants = [
if (empty($this->locale)) { 'OPT_LOCALE',
$missingOptions[] = self::OPT_LOCALE; 'OPT_DB_PORT',
'OPT_DB_HOST',
'OPT_DB_NAME',
'OPT_DB_USERNAME',
'OPT_DB_PASSWORD',
'OPT_COMPANY_NAME',
'OPT_COMPANY_EMAIL',
'OPT_ADMIN_EMAIL',
'OPT_ADMIN_PASSWORD',
];
foreach ($contants as $const) {
$option = constant("self::$const");
$property = str_replace('-', '_', $option);
$this->$property = $this->option($option);
if (empty($this->$property)) {
// Allow empty password
if ($property == 'db_password') {
continue;
} }
$this->dbHost = $this->option(self::OPT_DB_HOST); $missing_options[] = $option;
if (empty($this->dbHost)) { }
$missingOptions[] = self::OPT_DB_HOST;
} }
$this->dbPort = $this->option(self::OPT_DB_PORT); return $missing_options;
if (empty($this->dbPort)) {
$missingOptions[] = self::OPT_DB_PORT;
}
$this->dbName = $this->option(self::OPT_DB_NAME);
if (empty($this->dbName)) {
$missingOptions[] = self::OPT_DB_NAME;
}
$this->dbUsername = $this->option(self::OPT_DB_USERNAME);
if (empty($this->dbUsername)) {
$missingOptions[] = self::OPT_DB_USERNAME;
}
$this->dbPassword = $this->option(self::OPT_DB_PASSWORD);
if (empty($this->dbPassword)) {
$missingOptions[] = self::OPT_DB_PASSWORD;
}
$this->companyName = $this->option(self::OPT_COMPANY_NAME);
if (empty($this->companyName)) {
$missingOptions[] = self::OPT_COMPANY_NAME;
}
$this->companyEmail = $this->option(self::OPT_COMPANY_EMAIL);
if (empty($this->companyEmail)) {
$missingOptions[] = self::OPT_COMPANY_EMAIL;
}
$this->adminEmail = $this->option(self::OPT_ADMIN_EMAIL);
if (empty($this->adminEmail)) {
$missingOptions[] = self::OPT_ADMIN_EMAIL;
}
$this->adminPassword = $this->option(self::OPT_ADMIN_PASSWORD);
if (empty($this->adminPassword)) {
$missingOptions[] = self::OPT_ADMIN_PASSWORD;
}
return $missingOptions;
} }
/** /**
@ -175,54 +132,54 @@ class Install extends Command
*/ */
private function prompt() private function prompt()
{ {
if (empty($this->dbHost)) { if (empty($this->db_host)) {
$this->dbHost = $this->ask('What is the database host?', 'localhost'); $this->db_host = $this->ask('What is the database host?', 'localhost');
} }
if (empty($this->dbPort)) { if (empty($this->db_port)) {
$this->dbPort = $this->ask('What is the database port?', '3606'); $this->db_port = $this->ask('What is the database port?', '3306');
} }
if (empty($this->dbName)) { if (empty($this->db_name)) {
$this->dbName = $this->ask('What is the database name?'); $this->db_name = $this->ask('What is the database name?');
} }
if (empty($this->dbUsername)) { if (empty($this->db_username)) {
$this->dbUsername = $this->ask('What is the database username?'); $this->db_username = $this->ask('What is the database username?');
} }
if (empty($this->dbPassword)) { if (empty($this->db_password)) {
$this->dbPassword = $this->secret('What is the database password?'); $this->db_password = $this->secret('What is the database password?', '');
} }
if (empty($this->companyName)) { if (empty($this->company_name)) {
$this->companyName = $this->ask('What is the company name?'); $this->company_name = $this->ask('What is the company name?', 'My Company');
} }
if (empty($this->companyEmail)) { if (empty($this->company_email)) {
$this->companyEmail = $this->ask('What is the company contact email?'); $this->company_email = $this->ask('What is the company contact email?', 'my@company.com');
} }
if (empty($this->adminEmail)) { if (empty($this->admin_email)) {
$this->adminEmail = $this->ask('What is the admin email?', $this->companyEmail); $this->admin_email = $this->ask('What is the admin email?', $this->company_email);
} }
if (empty($this->adminPassword)) { if (empty($this->admin_password)) {
$this->adminPassword = $this->secret('What is the admin password?'); $this->admin_password = $this->secret('What is the admin password?');
} }
} }
private function createDatabaseTables() private function createDatabaseTables()
{ {
$this->dbHost = $this->option(self::OPT_DB_HOST); $this->db_host = $this->option(self::OPT_DB_HOST);
$this->dbPort = $this->option(self::OPT_DB_PORT); $this->db_port = $this->option(self::OPT_DB_PORT);
$this->dbName = $this->option(self::OPT_DB_NAME); $this->db_name = $this->option(self::OPT_DB_NAME);
$this->dbUsername = $this->option(self::OPT_DB_USERNAME); $this->db_username = $this->option(self::OPT_DB_USERNAME);
$this->dbPassword = $this->option(self::OPT_DB_PASSWORD); $this->db_password = $this->option(self::OPT_DB_PASSWORD);
$this->line('Connecting to database ' . $this->dbName . '@' . $this->dbHost . ':' . $this->dbPort); $this->line('Connecting to database ' . $this->db_name . '@' . $this->db_host . ':' . $this->db_port);
if (!Installer::createDbTables($this->dbHost, $this->dbPort, $this->dbName, $this->dbUsername, $this->dbPassword)) { if (!Installer::createDbTables($this->db_host, $this->db_port, $this->db_name, $this->db_username, $this->db_password)) {
$this->error('Error: Could not connect to the database! Please, make sure the details are correct.'); $this->error('Error: Could not connect to the database! Please, make sure the details are correct.');
return false; return false;