This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #140 from dbpolito/reset-db_fwd-yaml
Add fwd reset-db | Add fwd.yaml
- Loading branch information
Showing
14 changed files
with
1,130 additions
and
297 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Builder; | ||
|
||
class Fwd extends Builder | ||
{ | ||
public function getProgramName() : string | ||
{ | ||
return 'fwd'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace App\Commands; | ||
|
||
use App\Builder\Escaped; | ||
use App\Builder\Mysql; | ||
|
||
class ResetDb extends Command | ||
{ | ||
/** | ||
* The signature of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'reset-db {envFile?}'; | ||
|
||
/** | ||
* The description of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Reset DB.'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() : int | ||
{ | ||
if ($envFile = $this->argument('envFile')) { | ||
$this->environment->overloadEnv( | ||
$this->environment->getContextEnv($envFile) | ||
); | ||
} | ||
|
||
$commands = [ | ||
[$this, 'databaseDropDatabase'], | ||
[$this, 'databaseCreateDatabase'], | ||
[$this, 'databaseGrantDatabase'], | ||
]; | ||
|
||
return $this->runCommands($commands); | ||
} | ||
|
||
protected function databaseDropDatabase() : int | ||
{ | ||
return $this->runTask('MySQL Drop Database', function () { | ||
return $this->commandExecutor->runQuietly(Mysql::make( | ||
'-e', | ||
Escaped::make(sprintf('drop database if exists %s', env('DB_DATABASE'))) | ||
)); | ||
}); | ||
} | ||
|
||
protected function databaseCreateDatabase() : int | ||
{ | ||
return $this->runTask('MySQL Create Database', function () { | ||
return $this->commandExecutor->runQuietly(Mysql::make( | ||
'-e', | ||
Escaped::make(sprintf('create database %s', env('DB_DATABASE'))) | ||
)); | ||
}); | ||
} | ||
|
||
protected function databaseGrantDatabase() : int | ||
{ | ||
return $this->runTask('MySQL Grant Privileges', function () { | ||
return $this->commandExecutor->runQuietly(Mysql::make( | ||
'-e', | ||
Escaped::make(vsprintf('grant all on %s.* to %s@"%%"', [ | ||
env('DB_DATABASE'), | ||
env('DB_USERNAME'), | ||
])) | ||
)); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace App\Commands; | ||
|
||
use App\Builder\Builder; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
class Script extends Command | ||
{ | ||
/** | ||
* The signature of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'script {name}'; | ||
|
||
/** | ||
* The description of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Run fwd script.'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$fwdYaml = Yaml::parseFile( | ||
$this->environment->getContextFile('fwd.yaml'), | ||
Yaml::PARSE_OBJECT_FOR_MAP | ||
); | ||
|
||
if (! $script = object_get($fwdYaml, 'scripts.' . $this->argument('name'))) { | ||
$this->error('Script not found.'); | ||
|
||
return 1; | ||
} | ||
|
||
try { | ||
collect($script) | ||
->each(function ($command) { | ||
$exitCode = $this->runTask($command, function () use ($command) { | ||
return $this->commandExecutor->runQuietly( | ||
Builder::make($command) | ||
); | ||
}); | ||
|
||
if ($exitCode) { | ||
throw new \Exception("Command '{$command}' failed."); | ||
} | ||
}); | ||
} catch (\Exception $exception) { | ||
$this->error($exception->getMessage()); | ||
|
||
return 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.