Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.

Commit

Permalink
Implement export location option (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
edno committed Mar 10, 2019
1 parent 2daf7ca commit 13acd4e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 11 deletions.
32 changes: 32 additions & 0 deletions app/Console/Commands/AbstractExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\Filesystem;
use League\Flysystem\Adapter\Local;
use App\Account;

abstract class AbstractExportCommand extends Command
Expand Down Expand Up @@ -132,4 +134,34 @@ final protected function fecthAccounts($filter=null)

return $query->orderBy('netlogin', 'desc')->get();
}

final protected function exportToLocation($filename)
{
$source = $this->storage->path($filename);

if($this->argument('output')) {
$target = $this->argument('output') . '/' . $filename;

$fs = new Filesystem(new Local('/'));

if ($fs->has($target) ) {
if ($this->option('ci')) {
$fs->delete($target);
} else {
if ($this->confirm("Overwrite existing '$target' ?")) {
$fs->delete($target);
} else {
throw new \Exception();
}
}
}

$fs->rename($source, $target);

} else {
$target = $source;
}

return $target;
}
}
6 changes: 4 additions & 2 deletions app/Console/Commands/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class Export extends Command
* @var string
*/
protected $signature = 'export
{--ci : No progress bar (eg for CI)}';
{--ci : No interaction (no progess, no confirmation)}
{output? : Target location for export (default Storage)}';

/**
* The console command description.
Expand Down Expand Up @@ -46,7 +47,8 @@ public function handle()
{
foreach ($this->commands as $cmd) {
$this->call($cmd, [
'--ci' => $this->option('ci')
'--ci' => $this->option('ci'),
'output' => $this->argument('output'),
]);
}
}
Expand Down
13 changes: 10 additions & 3 deletions app/Console/Commands/ExportAccounts.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class ExportAccounts extends AbstractExportCommand
*/
protected $signature = 'export:accounts
{--empty : Create empty file if no record}
{--ci : No progress bar (eg for CI)}';
{--ci : No interaction (no progess, no confirmation)}
{output? : Target location for export (default Storage)}';

/**
* The console command description.
Expand All @@ -37,7 +38,7 @@ class ExportAccounts extends AbstractExportCommand
/**
* Execute the console command.
*
* @return mixed
* @return void
*/
public function handle()
{
Expand All @@ -52,7 +53,13 @@ public function handle()

$count = $this->exportAccounts($accounts, $filename, true, $this->option('ci'));

$url = $this->storage->path($filename);
try {
$url = $this->exportToLocation($filename);
} catch(\Exception $e) {
$this->error('Export cancelled by user!');
return;
}

$this->info("{$count} accounts exported into file '{$url}'");

}
Expand Down
11 changes: 9 additions & 2 deletions app/Console/Commands/ExportCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ExportCategories extends AbstractExportCommand
* @var string
*/
protected $signature = 'export:categories
{--ci : No progress bar (eg for CI)}';
{--ci : No interaction (no progess, no confirmation)}
{output? : Target location for export (default Storage)}';

/**
* The console command description.
Expand Down Expand Up @@ -52,7 +53,13 @@ public function handle()

$count = $this->exportAccounts($accounts, $filename, false, $this->option('ci'));

$url = $this->storage->path($filename);
try {
$url = $this->exportToLocation($filename);
} catch(\Exception $e) {
$this->error('Export cancelled by user!');
return;
}

$this->info("{$count} accounts '{$category->name}' exported into file '{$url}'");
}
}
Expand Down
11 changes: 9 additions & 2 deletions app/Console/Commands/ExportGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ExportGroups extends AbstractExportCommand
* @var string
*/
protected $signature = 'export:groups
{--ci : No progress bar (eg for CI)}';
{--ci : No interaction (no progess, no confirmation)}
{output? : Target location for export (default Storage)}';

/**
* The console command description.
Expand Down Expand Up @@ -52,7 +53,13 @@ public function handle()

$count = $this->exportAccounts($accounts, $filename, false, $this->option('ci'));

$url = $this->storage->path($filename);
try {
$url = $this->exportToLocation($filename);
} catch(\Exception $e) {
$this->error('Export cancelled by user!');
return;
}

$this->info("{$count} accounts '{$group->name}' exported into file '{$url}'");
}
}
Expand Down
11 changes: 9 additions & 2 deletions app/Console/Commands/ExportWhitelists.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ExportWhitelists extends AbstractExportCommand
protected $signature = 'export:whitelists
{--list= : Type of whitelist (domain or url)}
{--blacklist : Export blacklist instead of whitelist}
{--ci : No progress bar (eg for CI)}';
{--ci : No interaction (no progess, no confirmation)}
{output? : Target location for export (default Storage)}';

/**
* The console command description.
Expand Down Expand Up @@ -80,7 +81,13 @@ private function exportProxyList($type, $isWhiteList)
$this->info("\n");
}

$url = $this->storage->path($filename);
try {
$url = $this->exportToLocation($filename);
} catch(\Exception $e) {
$this->error('Export cancelled by user!');
return;
}

$this->info("{$count} {$type}s exported into file '{$url}'");
}
}

0 comments on commit 13acd4e

Please sign in to comment.