|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Acacha\ForgePublish\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Acacha\ForgePublish\Commands\Traits\ChecksEnv; |
|
6
|
|
|
use Acacha\ForgePublish\Commands\Traits\ChecksSSHConnection; |
|
7
|
|
|
use Acacha\ForgePublish\Commands\Traits\RunsSSHCommands; |
|
8
|
|
|
use GuzzleHttp\Client; |
|
9
|
|
|
use Illuminate\Console\Command; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class PublishEnv. |
|
13
|
|
|
* |
|
14
|
|
|
* @package Acacha\ForgePublish\Commands |
|
15
|
|
|
*/ |
|
16
|
|
|
class PublishEnv extends Command |
|
17
|
|
|
{ |
|
18
|
|
|
use ChecksEnv, RunsSSHCommands; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Server name |
|
22
|
|
|
* |
|
23
|
|
|
* @var String |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $server; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Domain |
|
29
|
|
|
* |
|
30
|
|
|
* @var String |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $domain; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* The name and signature of the console command. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $signature = 'publish:env {action?} {key?} {value?} {--server=} {--domain=}'; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* The console command description. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $description = 'Manage environment on production'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Guzzle http client. |
|
50
|
|
|
* |
|
51
|
|
|
* @var Client |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $http; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create a new command instance. |
|
57
|
|
|
* |
|
58
|
|
|
*/ |
|
59
|
|
|
public function __construct(Client $http) |
|
60
|
|
|
{ |
|
61
|
|
|
parent::__construct(); |
|
62
|
|
|
$this->http = $http; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Execute the console command. |
|
67
|
|
|
* |
|
68
|
|
|
*/ |
|
69
|
|
|
public function handle() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->abortCommandExecution(); |
|
72
|
|
|
if (! $this->argument('action') || $this->argument('action') == 'list') { |
|
73
|
|
|
$this->listEnvVariables(); |
|
74
|
|
|
return; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($this->argument('action') == 'edit') { |
|
78
|
|
|
$this->editEnvVariables(); |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if ($this->argument('action') == 'check') { |
|
83
|
|
|
$this->checkEnvVariable(); |
|
84
|
|
|
return; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* List remote env variables |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function listEnvVariables() |
|
92
|
|
|
{ |
|
93
|
|
|
$this->runSSH("cd $this->domain;cat .env"); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Edit remote env variables |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function editEnvVariables() |
|
100
|
|
|
{ |
|
101
|
|
|
$this->runSSH("cd $this->domain;editor .env"); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Edit remote env variables |
|
106
|
|
|
*/ |
|
107
|
|
|
protected function checkEnvVariable() |
|
108
|
|
|
{ |
|
109
|
|
|
$key = $this->checkKey(); |
|
110
|
|
|
$output = $this->execSSH("cd $this->domain;cat .env"); |
|
111
|
|
|
if (str_contains($output, $key)) { |
|
112
|
|
|
$this->info("Key $key found in remote environment file"); |
|
113
|
|
|
} else { |
|
114
|
|
|
$this->error("Key $key NOT found in remote environment file"); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Check key. |
|
120
|
|
|
* |
|
121
|
|
|
* @return array|string |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function checkKey() |
|
124
|
|
|
{ |
|
125
|
|
|
if (! $key = $this->argument('key')) { |
|
126
|
|
|
$this->error('No key argument has been provided!'); |
|
127
|
|
|
die(); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
return $key; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Abort command execution? |
|
134
|
|
|
*/ |
|
135
|
|
|
protected function abortCommandExecution() |
|
136
|
|
|
{ |
|
137
|
|
|
$this->server = $this->checkEnv('server', 'ACACHA_FORGE_SERVER'); |
|
138
|
|
|
$this->domain = $this->checkEnv('domain', 'ACACHA_FORGE_DOMAIN'); |
|
139
|
|
|
|
|
140
|
|
|
$this->abortIfNoSSHConnection(); |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exitexpression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.