Skip to content

Commit 032524d

Browse files
authored
fixes phpunit.xml, adds project ID trait for happiness (GoogleCloudPlatform#151)
1 parent 1d5afe1 commit 032524d

File tree

9 files changed

+53
-57
lines changed

9 files changed

+53
-57
lines changed

bigquery/api/phpunit.xml renamed to bigquery/api/phpunit.xml.dist

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@
2525
logging>
2626
<filter>
2727
<whitelist>
28-
<file>main.phpfile>
29-
<file>util.phpfile>
28+
<file>bigquery.phpfile>
3029
whitelist>
3130
filter>
3231
phpunit>

bigquery/api/src/BrowseTableCommand.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717

1818
namespace Google\Cloud\Samples\BigQuery;
1919

20-
use Google\Cloud\ClientTrait;
2120
use Symfony\Component\Console\Command\Command;
2221
use Symfony\Component\Console\Input\InputArgument;
2322
use Symfony\Component\Console\Input\InputInterface;
2423
use Symfony\Component\Console\Input\InputOption;
2524
use Symfony\Component\Console\Question\ConfirmationQuestion;
2625
use Symfony\Component\Console\Output\OutputInterface;
2726
use InvalidArgumentException;
28-
use Exception;
2927

3028
/**
3129
* Command line utility to list BigQuery tables.
@@ -34,7 +32,7 @@
3432
*/
3533
class BrowseTableCommand extends Command
3634
{
37-
use ClientTrait;
35+
use ProjectIdTrait;
3836

3937
protected function configure()
4038
{
@@ -73,10 +71,7 @@ protected function configure()
7371
protected function execute(InputInterface $input, OutputInterface $output)
7472
{
7573
if (!$projectId = $input->getOption('project')) {
76-
if (!$projectId = $this->detectProjectId()) {
77-
throw new Exception('Could not derive a project ID from gcloud. ' .
78-
'You must supply a project ID using --project');
79-
}
74+
$projectId = $this->detectProjectId();
8075
}
8176
$maxResults = $input->getOption('max-results');
8277
$fullTableName = $input->getArgument('dataset.table');

bigquery/api/src/DatasetsCommand.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,10 @@
1717

1818
namespace Google\Cloud\Samples\BigQuery;
1919

20-
use Google\Cloud\ClientTrait;
2120
use Symfony\Component\Console\Command\Command;
2221
use Symfony\Component\Console\Input\InputInterface;
2322
use Symfony\Component\Console\Input\InputOption;
2423
use Symfony\Component\Console\Output\OutputInterface;
25-
use Exception;
2624

2725
/**
2826
* Command line utility to list BigQuery datasets.
@@ -31,7 +29,7 @@
3129
*/
3230
class DatasetsCommand extends Command
3331
{
34-
use ClientTrait;
32+
use ProjectIdTrait;
3533

3634
protected function configure()
3735
{
@@ -58,10 +56,7 @@ protected function configure()
5856
protected function execute(InputInterface $input, OutputInterface $output)
5957
{
6058
if (!$projectId = $input->getOption('project')) {
61-
if (!$projectId = $this->detectProjectId()) {
62-
throw new Exception('Could not derive a project ID from gcloud. ' .
63-
'You must supply a project ID using --project');
64-
}
59+
$projectId = $this->getProjectIdFromGcloud();
6560
}
6661
list_datasets($projectId);
6762
}

bigquery/api/src/ExportCommand.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717

1818
namespace Google\Cloud\Samples\BigQuery;
1919

20-
use Google\Cloud\ClientTrait;
2120
use Symfony\Component\Console\Command\Command;
2221
use Symfony\Component\Console\Input\InputInterface;
2322
use Symfony\Component\Console\Input\InputArgument;
2423
use Symfony\Component\Console\Input\InputOption;
2524
use Symfony\Component\Console\Output\OutputInterface;
2625
use Google\Cloud\ServiceBuilder;
2726
use InvalidArgumentException;
28-
use Exception;
2927

3028
/**
3129
* Command line utility to import data into BigQuery.
@@ -34,7 +32,7 @@
3432
*/
3533
class ExportCommand extends Command
3634
{
37-
use ClientTrait;
35+
use ProjectIdTrait;
3836

3937
protected function configure()
4038
{
@@ -85,10 +83,7 @@ protected function configure()
8583
protected function execute(InputInterface $input, OutputInterface $output)
8684
{
8785
if (!$projectId = $input->getOption('project')) {
88-
if ($projectId = $this->detectProjectId()) {
89-
throw new Exception('Could not derive a project ID from gcloud. ' .
90-
'You must supply a project ID using --project');
91-
}
86+
$projectId = $this->getProjectIdFromGcloud();
9287
}
9388
$fullTableName = $input->getArgument('dataset.table');
9489
if (1 !== substr_count($fullTableName, '.')) {

bigquery/api/src/ImportCommand.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
namespace Google\Cloud\Samples\BigQuery;
1919

20-
use Google\Cloud\ClientTrait;
2120
use Symfony\Component\Console\Command\Command;
2221
use Symfony\Component\Console\Input\InputInterface;
2322
use Symfony\Component\Console\Input\InputArgument;
2423
use Symfony\Component\Console\Input\InputOption;
2524
use Symfony\Component\Console\Output\OutputInterface;
26-
use Symfony\Component\Console\Question\ConfirmationQuestion;
2725
use Symfony\Component\Console\Question\Question;
2826
use Google\Cloud\ServiceBuilder;
2927
use InvalidArgumentException;
@@ -36,7 +34,7 @@
3634
*/
3735
class ImportCommand extends Command
3836
{
39-
use ClientTrait;
37+
use ProjectIdTrait;
4038

4139
protected function configure()
4240
{
@@ -89,18 +87,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
8987
{
9088
$question = $this->getHelper('question');
9189
if (!$projectId = $input->getOption('project')) {
92-
if ($projectId = $this->detectProjectId()) {
93-
if ($input->isInteractive()) {
94-
$message = sprintf('Import data for project %s? [y/n]: ', $projectId);
95-
if (!$question->ask($input, $output, new ConfirmationQuestion($message))) {
96-
return $output->writeln('Task cancelled by user.');
97-
}
98-
}
99-
} else {
100-
throw new Exception('Could not derive a project ID from gcloud. ' .
101-
'You must supply a project ID using --project');
102-
}
90+
$projectId = $this->getProjectIdFromGcloud();
10391
}
92+
$message = sprintf('Using project %s', $projectId);
93+
$output->writeln($message);
10494
$fullTableName = $input->getArgument('dataset.table');
10595
if (1 !== substr_count($fullTableName, '.')) {
10696
throw new InvalidArgumentException('Table must in the format "dataset.table"');

bigquery/api/src/ProjectIdTrait.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
/**
3+
* Copyright 2015 Google Inc. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
namespace Google\Cloud\Samples\BigQuery;
19+
20+
/**
21+
* Trait for determining the current project ID using "gcloud"
22+
*/
23+
trait ProjectIdTrait
24+
{
25+
private function getProjectIdFromGcloud()
26+
{
27+
exec("gcloud config list --format 'value(core.project)' 2>/dev/null", $output, $return_var);
28+
29+
if (0 === $return_var) {
30+
return array_pop($output);
31+
}
32+
33+
throw new Exception('Could not derive a project ID from gcloud. ' .
34+
'You must supply a project ID using --project');
35+
}
36+
}

bigquery/api/src/QueryCommand.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace Google\Cloud\Samples\BigQuery;
1919

20-
use Google\Cloud\ClientTrait;
2120
use Symfony\Component\Console\Command\Command;
2221
use Symfony\Component\Console\Input\InputInterface;
2322
use Symfony\Component\Console\Input\InputArgument;
@@ -34,7 +33,7 @@
3433
*/
3534
class QueryCommand extends Command
3635
{
37-
use ClientTrait;
36+
use ProjectIdTrait;
3837

3938
protected function configure()
4039
{
@@ -73,10 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7372
{
7473
$question = $this->getHelper('question');
7574
if (!$projectId = $input->getOption('project')) {
76-
if (!$projectId = $this->detectProjectId()) {
77-
throw new Exception('Could not derive a project ID from gcloud. ' .
78-
'You must supply a project ID using --project');
79-
}
75+
$projectId = $this->getProjectIdFromGcloud();
8076
}
8177
$message = sprintf('Running query for project %s', $projectId);
8278
$output->writeln($message);

bigquery/api/src/SchemaCommand.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
namespace Google\Cloud\Samples\BigQuery;
1919

20-
use Google\Cloud\ClientTrait;
2120
use Symfony\Component\Console\Command\Command;
2221
use Symfony\Component\Console\Input\InputInterface;
2322
use Symfony\Component\Console\Input\InputArgument;
@@ -30,7 +29,6 @@
3029
use Google\Cloud\Exception\BadRequestException;
3130
use InvalidArgumentException;
3231
use LogicException;
33-
use Exception;
3432

3533
/**
3634
* Command line utility to create a BigQuery schema.
@@ -39,7 +37,7 @@
3937
*/
4038
class SchemaCommand extends Command
4139
{
42-
use ClientTrait;
40+
use ProjectIdTrait;
4341

4442
protected function configure()
4543
{
@@ -98,10 +96,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9896
{
9997
$question = $this->getHelper('question');
10098
if (!$projectId = $input->getOption('project')) {
101-
if (!$projectId = $this->detectProjectId()) {
102-
throw new Exception('Could not derive a project ID from gcloud. ' .
103-
'You must supply a project ID using --project');
104-
}
99+
$projectId = $this->getProjectIdFromGcloud();
105100
}
106101
$message = sprintf('Using project %s', $projectId);
107102
$output->writeln($message);

bigquery/api/src/TablesCommand.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@
1717

1818
namespace Google\Cloud\Samples\BigQuery;
1919

20-
use Google\Cloud\ClientTrait;
2120
use Symfony\Component\Console\Command\Command;
2221
use Symfony\Component\Console\Input\InputArgument;
2322
use Symfony\Component\Console\Input\InputInterface;
2423
use Symfony\Component\Console\Input\InputOption;
2524
use Symfony\Component\Console\Output\OutputInterface;
26-
use Exception;
2725

2826
/**
2927
* Command line utility to list BigQuery tables.
@@ -32,7 +30,7 @@
3230
*/
3331
class TablesCommand extends Command
3432
{
35-
use ClientTrait;
33+
use ProjectIdTrait;
3634

3735
protected function configure()
3836
{
@@ -64,10 +62,7 @@ protected function configure()
6462
protected function execute(InputInterface $input, OutputInterface $output)
6563
{
6664
if (!$projectId = $input->getOption('project')) {
67-
if (!$projectId = $this->detectProjectId()) {
68-
throw new Exception('Could not derive a project ID from gcloud. ' .
69-
'You must supply a project ID using --project');
70-
}
65+
$projectId = $this->getProjectIdFromGcloud();
7166
}
7267
$datasetId = $input->getArgument('dataset');
7368

0 commit comments

Comments
 (0)