Skip to content

Commit 5ecee74

Browse files
committed
Add terminus-code-plugin
0 parents  commit 5ecee74

9 files changed

Lines changed: 366 additions & 0 deletions

File tree

.circleci/config.yml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defaults: &defaults
2+
docker:
3+
- image: quay.io/pantheon-public/terminus-plugin-test:1.x
4+
working_directory: ~/work/terminus_plugin
5+
environment:
6+
BASH_ENV: ~/.bashrc
7+
TZ: "/usr/share/zoneinfo/America/Chicago"
8+
TERM: dumb
9+
10+
version: 2
11+
jobs:
12+
test:
13+
<<: *defaults
14+
steps:
15+
- checkout
16+
- run:
17+
name: Set up environment
18+
command: ./.circleci/set-up-globals.sh
19+
- run:
20+
name: Dependencies
21+
command: composer install
22+
- run:
23+
name: Lint
24+
command: composer lint
25+
- run:
26+
name: Unit
27+
command: composer unit
28+
- run:
29+
name: Functional
30+
command: composer functional
31+
- run:
32+
name: Style
33+
command: composer cs
34+
35+
workflows:
36+
version: 2
37+
build_test:
38+
jobs:
39+
- test

.circleci/set-up-globals.sh

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
#=====================================================================================================================
4+
# EXPORT needed environment variables
5+
#
6+
# Circle CI 2.0 does not yet expand environment variables so they have to be manually EXPORTed
7+
# Once environment variables can be expanded this section can be removed
8+
# See: https://discuss.circleci.com/t/unclear-how-to-work-with-user-variables-circleci-provided-env-variables/12810/11
9+
# See: https://discuss.circleci.com/t/environment-variable-expansion-in-working-directory/11322
10+
# See: https://discuss.circleci.com/t/circle-2-0-global-environment-variables/8681
11+
#=====================================================================================================================
12+
mkdir -p $(dirname $BASH_ENV)
13+
touch $BASH_ENV
14+
(
15+
echo 'export PATH=$PATH:$HOME/bin'
16+
echo 'export TERMINUS_HIDE_UPDATE_MESSAGE=1'
17+
) >> $BASH_ENV
18+
source $BASH_ENV
19+
20+
set -ex
21+
22+
TERMINUS_PLUGINS_DIR=.. terminus list -n remote
23+
24+
set +ex
25+
echo "Test site is $TERMINUS_SITE"
26+
echo "Logging in with a machine token:"
27+
terminus auth:login -n --machine-token="$TERMINUS_TOKEN"
28+
terminus whoami
29+
touch $HOME/.ssh/config
30+
echo "StrictHostKeyChecking no" >> "$HOME/.ssh/config"
31+
git config --global user.email "$GIT_EMAIL"
32+
git config --global user.name "Circle CI"
33+
# Ignore file permissions.
34+
git config --global core.fileMode false

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2016 Terminus Plugin Project
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Terminus Code Plugin
2+
3+
Version 1.x
4+
5+
[![CircleCI](https://circleci.com/gh/terminus-plugin-project/terminus-code-plugin.svg?style=shield)](https://circleci.com/gh/terminus-plugin-project/terminus-code-plugin)
6+
[![Terminus v1.x Compatible](https://img.shields.io/badge/terminus-v1.x-green.svg)](https://github.com/terminus-plugin-project/terminus-code-plugin/tree/1.x)
7+
[![Terminus v0.x Compatible](https://img.shields.io/badge/terminus-v0.x-green.svg)](https://github.com/terminus-plugin-project/terminus-code-plugin/tree/0.x)
8+
9+
Terminus plugin to clone the code from any available [Pantheon](https://www.pantheon.io) site environment to your local system.
10+
11+
## Usage:
12+
```
13+
$ terminus site:env:code | env:code | code <site>.<env>
14+
```
15+
16+
## Example:
17+
Clone the code from the my-site development environment.
18+
```
19+
terminus code my-site.dev
20+
```
21+
22+
Learn more about [Terminus](https://pantheon.io/docs/terminus/) and [Terminus Plugins](https://pantheon.io/docs/terminus/plugins/).
23+
24+
## Installation:
25+
For installation help, see [Manage Plugins](https://pantheon.io/docs/terminus/plugins/).
26+
27+
```
28+
mkdir -p ~/.terminus/plugins
29+
composer create-project -d ~/.terminus/plugins terminus-plugin-project/terminus-code-plugin:~1
30+
```
31+
32+
## Configuration:
33+
34+
This plugin requires no configuration to use.
35+
36+
## Testing:
37+
```
38+
export TERMINUS_SITE=my-site
39+
cd ~/.terminus/plugins/terminus-code-plugin
40+
composer install
41+
composer test
42+
```
43+
44+
## Help:
45+
Run `terminus help code` for help.

composer.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "terminus-plugin-project/terminus-code-plugin",
3+
"description": "Code - A Terminus plugin to clone the code from any available Pantheon site environment to your local system.",
4+
"authors": [
5+
{
6+
"name": "Ed Reel",
7+
"email": "edreel@gmail.com",
8+
"homepage": "https://github.com/uberhacker",
9+
"role": "Developer"
10+
}
11+
],
12+
"type": "terminus-plugin",
13+
"keywords": [
14+
"pantheon",
15+
"terminus",
16+
"code",
17+
"plugin"
18+
],
19+
"support": {
20+
"issues": "https://github.com/terminus-plugin-project/terminus-code-plugin/issues"
21+
},
22+
"license": "MIT",
23+
"require": {
24+
"php": "^5.5 || ^7.0"
25+
},
26+
"require-dev": {
27+
"squizlabs/php_codesniffer": "^2.7"
28+
},
29+
"scripts": {
30+
"install-bats": "if [ ! -f bin/bats ] ; then git clone https://github.com/sstephenson/bats.git; mkdir -p bin; bats/install.sh .; fi",
31+
"bats": "TERMINUS_PLUGINS_DIR=.. bin/bats tests",
32+
"cs": "phpcs --standard=PSR2 -n src",
33+
"cbf": "phpcbf --standard=PSR2 -n src",
34+
"confirm-install": "bin/bats tests/functional/confirm-install.bats",
35+
"test-output": "bin/bats tests/functional/test-output.bats",
36+
"cleanup": "if [ -d $TERMINUS_SITE ]; then rm -rf $TERMINUS_SITE; fi",
37+
"test": [
38+
"@install-bats",
39+
"@bats",
40+
"@cs",
41+
"@confirm-install",
42+
"@test-output",
43+
"@cleanup"
44+
]
45+
},
46+
"autoload": {
47+
"psr-4": { "TerminusPluginProject\\TerminusCode\\": "src" }
48+
},
49+
"extra": {
50+
"terminus": {
51+
"compatible-version": "^1"
52+
}
53+
}
54+
}

src/Commands/CodeCommand.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/**
4+
* This command will clone the code from any available Pantheon site environment to your local system.
5+
*
6+
* See README.md for usage information.
7+
*/
8+
9+
namespace TerminusPluginProject\TerminusCode\Commands;
10+
11+
use Pantheon\Terminus\Commands\Site\SiteCommand;
12+
use Pantheon\Terminus\Exceptions\TerminusNotFoundException;
13+
14+
class CodeCommand extends SiteCommand
15+
{
16+
/**
17+
* Clones the code from any available site environment.
18+
*
19+
* @authorize
20+
*
21+
* @command site:env:code
22+
* @aliases env:code code
23+
*
24+
* @usage terminus site:env:code | env:code | code <site>.<env>
25+
* Clone the code of <site>.<env> locally. Example: terminus code my-site.dev.
26+
*/
27+
public function code($site_env = false)
28+
{
29+
30+
if (!$site_env) {
31+
throw new TerminusNotFoundException('Usage: terminus site:env:code | env:code | code <site>.<env>');
32+
}
33+
34+
$this->sites()->fetch(
35+
[
36+
'org_id' => null,
37+
'team_only' => false,
38+
]
39+
);
40+
41+
$sites = $this->sites->serialize();
42+
43+
if (empty($sites)) {
44+
throw new TerminusNotFoundException('You have no sites.');
45+
}
46+
47+
// Validate if site.env exists.
48+
$site_env_exists = false;
49+
$site_env_frozen = false;
50+
foreach ($sites as $site) {
51+
if ($environments = $this->getSite($site['name'])->getEnvironments()->serialize()) {
52+
foreach ($environments as $environment) {
53+
if ($environment['initialized'] == 'true') {
54+
$site_environment = $site['name'] . '.' . $environment['id'];
55+
if ($site_env == $site_environment) {
56+
$site_env_exists = true;
57+
$site_env_frozen = ($site['frozen'] == 'true');
58+
break;
59+
}
60+
}
61+
}
62+
}
63+
if ($site_env_exists) {
64+
break;
65+
}
66+
}
67+
68+
if (!$site_env_exists) {
69+
$message = 'Site environment {site_env} does not exist.';
70+
throw new TerminusNotFoundException($message, ['site_env' => $site_env]);
71+
}
72+
73+
if ($site_env_frozen) {
74+
$message = 'Site environment {site_env} is frozen.';
75+
throw new TerminusNotFoundException($message, ['site_env' => $site_env]);
76+
}
77+
78+
$command = '$(terminus connection:info ' . $site_env . ' --field=git_command)';
79+
$this->execute($command);
80+
}
81+
82+
/**
83+
* Executes the command.
84+
*/
85+
protected function execute($cmd)
86+
{
87+
$process = proc_open(
88+
$cmd,
89+
[
90+
0 => STDIN,
91+
1 => STDOUT,
92+
2 => STDERR,
93+
],
94+
$pipes
95+
);
96+
proc_close($process);
97+
}
98+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bats
2+
3+
#
4+
# confirm-install.bats
5+
#
6+
# Ensure that Terminus and the Composer plugin have been installed correctly
7+
#
8+
9+
@test "confirm terminus version" {
10+
terminus --version
11+
}
12+
13+
@test "get help on plugin command" {
14+
run terminus help code
15+
[[ "$output" == *"Clones the code from any available site environment."* ]]
16+
[ "$status" -eq 0 ]
17+
}

tests/functional/test-output.bats

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bats
2+
3+
#
4+
# test-output.bats
5+
#
6+
# Test plugin command output
7+
#
8+
9+
@test "output of plugin command" {
10+
run terminus code $TERMINUS_SITE.dev
11+
[[ "$output" == *"Cloning into '$TERMINUS_SITE'..."* ]]
12+
[ "$status" -eq 0 ]
13+
}

tests/unit/CodeTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace TerminusPluginProject\TerminusCode\Commands;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class CodeTest extends TestCase
8+
{
9+
$this->TERMINUS_SITE = null;
10+
11+
/**
12+
* Constructor method to set values.
13+
*/
14+
private function __constructor()
15+
{
16+
$this->TERMINUS_SITE = getenv('TERMINUS_SITE');
17+
}
18+
19+
/**
20+
* Data provider for testCode.
21+
*
22+
* Return an array of arrays, each of which contains the parameter
23+
* values to be used in one invocation of the testCodeCommand function.
24+
*/
25+
public function codeTestValues()
26+
{
27+
return [
28+
[$this->TERMINUS_SITE, "$(terminus site:info $this->TERMINUS_SITE --field=name)"],
29+
['dev', "$(terminus env:info $this->TERMINUS_SITE.dev --field=id)"],
30+
];
31+
}
32+
33+
/**
34+
* Test our CodeCommand class. Each time this function is called, it will
35+
* be passed data from the data provider function identified by the
36+
* dataProvider annotation.
37+
*
38+
* @dataProvider codeTestValues
39+
*/
40+
public function testCodeCommand($expected, $command)
41+
{
42+
$code = new CodeCommand($this->TERMINUS_SITE);
43+
$this->assertEquals($expected, $code->execute($command));
44+
}
45+
}

0 commit comments

Comments
 (0)