-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.php
More file actions
171 lines (141 loc) · 4.76 KB
/
command.php
File metadata and controls
171 lines (141 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
if ( ! class_exists( 'WP_CLI' ) ) {
return;
}
use WP_CLI\Utils;
function terminus_exec($cmd) {
$exit = 0; $output = '';
exec($cmd . " 2>&1", $output, $exit);
return array('output' => $output, 'code' => $exit);
}
function terminus_logged_out() {
$ret = terminus_exec("terminus auth:whoami")['output'];
return isset($ret[1])&& preg_match("/not logged in/i", $ret[1]) > 0;
}
/**
* Overwrites local database with copy from pantheon
*
* ## OPTIONS
*
* [--site=<site>]
* : The name of the site to copy the database from. Required.
* [--env=<env>]
* : The environment to clone from. Defaults to 'dev'
* [--url=<url>]
* : The url to replace in the database after import. Will be automatically derived from WP settings if omitted
* [--no-backup]
* : Don't create a new pantheon backup before downloading. Use this if the database hasn't changed
* ---
*
* ## EXAMPLES
*
* wp terminus db --site=my-pantheon-site
*
* @when before_wp_load
*/
function terminus_db_command($args, $opts) {
$site = isset($opts['site']) ? $opts['site'] : null;
$env = isset($opts['env']) ? $opts['env'] : "dev";
$backup = isset($opts['backup']) ? $opts['backup'] : true;
$url = preg_replace("(^https?://)", "", (isset($opts['url']) ? $opts['url'] : WP_CLI::runcommand('option get siteurl', array('return' => 'all'))->stdout));
$now = time();
if (terminus_logged_out()) {
WP_CLI::error("Please log in to pantheon using `terminus auth:login`");
return;
}
if (!$site) {
WP_CLI::error("Please supply the site option `--site=NAME`");
return;
}
$filename = "$site-db-$now.sql.gz";
$progress = \WP_CLI\Utils\make_progress_bar( 'Overwriting local database with Pantheon', $backup ? 8 : 7 );
$progress->tick();
if ($backup) {
$ret = terminus_exec("terminus backup:create $site.$env --element=db");
$progress->tick();
if ($ret['code'] != 0) {
WP_CLI::error($ret['output'][0]);
return;
}
}
$ret = terminus_exec("terminus backup:get $site.$env --element=database --to=/tmp/$filename");
$progress->tick();
if ($ret['code'] != 0) {
WP_CLI::error($ret['output'][0]);
return;
}
if (file_exists("/tmp/$filename")) {
WP_CLI::launch_self('db reset --yes');
$progress->tick();
WP_CLI::launch("gunzip /tmp/$filename");
WP_CLI::launch_self("db import /tmp/" . basename($filename, '.gz'));
$progress->tick();
WP_CLI::launch("rm /tmp/" . basename($filename, '.gz'));
$progress->tick();
WP_CLI::runcommand("search-replace '$env-$site.pantheonsite.io' '$url'");
$progress->tick();
$progress->finish();
WP_CLI::success("Finished overwriting database. (•_•) ( •_•)>⌐■-■ (⌐■_■)");
} else {
WP_CLI::error("File $filename does not exist. Exiting");
}
};
WP_CLI::add_command( 'terminus db', 'terminus_db_command' );
/**
* Copies files from pantheon
*
* ## OPTIONS
*
* [--site=<site>]
* : The name of the site to copy the database from. Required.
* [--env=<env>]
* : The environment to clone from. Defaults to 'dev'
* [--url=<url>]
* : The url to replace in the database after import. Will be automatically derived from WP settings if omitted
* [--no-backup]
* : Don't create a new pantheon backup before downloading. Use this if the database hasn't changed
* ---
*
* ## EXAMPLES
*
* wp terminus files --site=my-pantheon-site
*
* @when before_wp_load
*/
function terminus_files_command($args, $opts) {
$site = isset($opts['site']) ? $opts['site'] : null;
$env = isset($opts['env']) ? $opts['env'] : "dev";
$backup = isset($opts['backup']) ? $opts['backup'] : true;
$now = time();
if (terminus_logged_out()) {
WP_CLI::error("Please log in to pantheon using `terminus auth:login`");
return;
}
if (!$site) {
WP_CLI::error("Please supply the site option `--site=NAME`");
return;
}
$filename = "$site-files-$now.tar.gz";
$progress = \WP_CLI\Utils\make_progress_bar( 'Copying files from Pantheon', $backup ? 5 : 4 );
$progress->tick();
if ($backup) {
terminus_exec("terminus backup:create $site.$env --element=files");
$progress->tick();
}
terminus_exec("terminus backup:get $site.$env --element=files --to=/tmp/$filename");
$progress->tick();
if (file_exists("/tmp/$filename")) {
WP_CLI::launch(`gzip -dc "/tmp/$filename" | tar xf - -C /tmp`);
$progress->tick();
foreach (glob("/tmp/files_$env/*") as $f) {
if (is_dir($f)) {
WP_CLI::launch("yes | cp -r $f wp-content/uploads/");
}
}
WP_CLI::launch("rm -rf /tmp/files_$env/");
WP_CLI::launch("rm /tmp/$filename");
$progress->finish();
WP_CLI::success("Finished copying files. (•_•) ( •_•)>⌐■-■ (⌐■_■)");
}
};
WP_CLI::add_command( 'terminus files', 'terminus_files_command' );