Skip to content

Commit

Permalink
fix config
Browse files Browse the repository at this point in the history
  • Loading branch information
zxp86021 committed Aug 22, 2019
1 parent b8722c3 commit 2c9db5a
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Expand Up @@ -23,6 +23,7 @@ jobs:

- run: sudo apt update && sudo apt install zlib1g-dev libsqlite3-dev
- run: sudo docker-php-ext-install zip
- run: sudo composer self-update

# Download and cache dependencies

Expand Down Expand Up @@ -61,6 +62,8 @@ jobs:
# setup .env file
- run: cp .env.example .env
- run: php artisan key:generate
- run: php artisan dotenv:set MOLI_BLOG_CLIENT_ID $MOLI_BLOG_CLIENT_ID
- run: php artisan dotenv:set MOLI_BLOG_CLIENT_SECRET $MOLI_BLOG_CLIENT_SECRET

# run tests with phpunit or codecept
- run: ./vendor/bin/phpunit
Expand Down
7 changes: 3 additions & 4 deletions .env.example
Expand Up @@ -39,12 +39,11 @@ TELEGRAM_BOT_TOKEN=SomeRandomString
NCDR_URL=SomeRandomString
NCDR_API_KEY=
NEWS_CHANNEL=NewsChannel
MOLi_CHANNEL=MOLiChannel
MOLI_CHANNEL=MOLiChannel
WEATHER_CHANNEL=WeatherCHANNEL

MOLi_BLOG_URL=
MOLi_BLOG_CLIENT_ID=
MOLi_BLOG_CLIENT_SECRET=
MOLI_BLOG_CLIENT_ID=
MOLI_BLOG_CLIENT_SECRET=

TEST_CHANNEL=

Expand Down
152 changes: 152 additions & 0 deletions app/Console/Commands/DotEnvSet.php
@@ -0,0 +1,152 @@
<?php

namespace MOLiBot\Console\Commands;

use Illuminate\Console\Command;

class DotEnvSet extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dotenv:set {key} {value?}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Set and save an environment variable in the .env file';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
try {
list($key, $value) = $this->getKeyValue();
} catch (\InvalidArgumentException $e) {
return $this->error($e->getMessage());
}

$envFilePath = app()->environmentFilePath();

$contents = file_get_contents($envFilePath);

if ($oldValue = $this->getOldValue($contents, $key)) {
$contents = str_replace("{$key}={$oldValue}", "{$key}={$value}", $contents);

$this->writeFile($envFilePath, $contents);

return $this->info("Environment variable with key '{$key}' has been changed from '{$oldValue}' to '{$value}'");
}

$contents = $contents . "\n{$key}={$value}\n";

$this->writeFile($envFilePath, $contents);

return $this->info("A new environment variable with key '{$key}' has been set to '{$value}'");
}

/**
* Overwrite the contents of a file.
*
* @param string $path
* @param string $contents
* @return boolean
*/
protected function writeFile(string $path, string $contents): bool
{
$file = fopen($path, 'w');

fwrite($file, $contents);

return fclose($file);
}

/**
* Get the old value of a given key from an environment file.
*
* @param string $envFile
* @param string $key
* @return string
*/
protected function getOldValue(string $envFile, string $key): string
{
// Match the given key at the beginning of a line
preg_match("/^{$key}=[^\r\n]*/m", $envFile, $matches);

if (count($matches)) {
return substr($matches[0], strlen($key) + 1);
}

return '';
}

/**
* Determine what the supplied key and value is from the current command.
*
* @return array
*/
protected function getKeyValue(): array
{
$key = $this->argument('key');

$value = $this->argument('value');

if (! $value) {
$parts = explode('=', $key, 2);

if (count($parts) !== 2) {
throw new \InvalidArgumentException('No value was set');
}

$key = $parts[0];

$value = $parts[1];
}

if (! $this->isValidKey($key)) {
throw new \InvalidArgumentException('Invalid argument key');
}

if (! is_bool(strpos($value, ' '))) {
$value = '"' . $value . '"';
}

return [strtoupper($key), $value];
}

/**
* Check if a given string is valid as an environment variable key.
*
* @param string $key
* @return boolean
*/
protected function isValidKey(string $key): bool
{
if (str_contains($key, '=')) {
throw new \InvalidArgumentException("Environment key should not contain '='");
}

if (!preg_match('/^[a-zA-Z_]+$/', $key)) {
throw new \InvalidArgumentException('Invalid environment key. Only use letters and underscores');
}

return true;
}
}
6 changes: 3 additions & 3 deletions config/moli.php
Expand Up @@ -4,11 +4,11 @@

'blog' => [

'url' => env('MOLi_BLOG_URL'),
'url' => 'https://blog.moli.rocks',

'client_id' => env('MOLi_BLOG_CLIENT_ID'),
'client_id' => env('MOLI_BLOG_CLIENT_ID'),

'client_secret' => env('MOLi_BLOG_CLIENT_SECRET'),
'client_secret' => env('MOLI_BLOG_CLIENT_SECRET'),

],

Expand Down
2 changes: 1 addition & 1 deletion config/telegram-channel.php
Expand Up @@ -4,7 +4,7 @@

'ncnu_news' => env('NEWS_CHANNEL'),

'MOLi' => env('MOLi_CHANNEL'),
'MOLi' => env('MOLI_CHANNEL'),

'weather' => env('WEATHER_CHANNEL'),

Expand Down

0 comments on commit 2c9db5a

Please sign in to comment.