Skip to content

Commit

Permalink
Merge pull request #1 from jpmurray/develop
Browse files Browse the repository at this point in the history
Version bump
  • Loading branch information
jpmurray authored Oct 5, 2018
2 parents 30790de + 9eb201f commit 0034813
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 22 deletions.
8 changes: 2 additions & 6 deletions app/Commands/AddRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public function handle()

$this->digitalocean = new DigitalOceanHelper($this->settings->getToken());

$domains = collect($this->digitalocean->domain->getAll())->mapWithKeys(function ($values) {
return [$values->name => $values->name];
})->toArray();
$domains = $this->digitalocean->getDomains();

$selected_domain = $this->menu("Which domain?", $domains)->open();

Expand All @@ -56,9 +54,7 @@ public function handle()
return;
}

$records = collect($this->digitalocean->domainRecord->getAll($selected_domain))->filter(function ($record) {
return $record->type == "CNAME" || $record->type == "A";
});
$records = $this->digitalocean->getDomainRecords($selected_domain);

$records_for_menu = $records->mapWithKeys(function ($values, $key) {
return [$key => "{$values->name} ({$values->type}): {$values->data}"];
Expand Down
5 changes: 4 additions & 1 deletion app/Commands/ListRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Commands;

use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\DB;
use LaravelZero\Framework\Commands\Command;
Expand Down Expand Up @@ -32,8 +33,10 @@ public function handle()
$records = DB::table('records')->get();

$records->each(function ($record, $key) {
$last_update = is_null($record->record_updated_at) ? "Never" : Carbon::parse($record->record_updated_at)->toDatetimeString();

$this->line("");
$this->info("[{$key}] ({$record->record_type}) {$record->record_name} of {$record->domain}");
$this->info("[{$key}] ({$record->record_type}) {$record->record_name} of {$record->domain}. Last updated: {$last_update} (UTC).");
});
}
}
1 change: 1 addition & 0 deletions app/Commands/RemoveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class RemoveRecord extends Command
public function handle()
{
$records = DB::table('records')->get();

$records_for_menu = collect($records)->mapWithKeys(function ($record) {
return [$record->id => "({$record->record_type}) {$record->record_name} of {$record->domain}"];
})->toArray();
Expand Down
35 changes: 20 additions & 15 deletions app/Commands/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,7 @@ class Setup extends Command
public function handle()
{
if ($this->confirm("This will destroy any existing doddns configuration. Is that ok?")) {
$this->task("Creating local database", function () {
if (!is_dir($_SERVER['HOME'].'/.doddns/')) {
mkdir($_SERVER['HOME'].'/.doddns/', 0700);
$this->info("Created .doddns directory in user's home.");
}

file_put_contents(config('database.connections.sqlite.database'), "");
$this->info("Created or overwrited any actual databse");

Artisan::call('migrate', ['--force' => true]);

$this->info("Migrated tables");

return true;
});
$this->createDatabase();
}

$this->settings = new SettingsHelper();
Expand All @@ -65,6 +51,25 @@ public function handle()
$this->info("All done! We're good to go!");
}

private function createDatabase()
{
$this->task("Creating local database", function () {
if (!is_dir($_SERVER['HOME'].'/.doddns/')) {
mkdir($_SERVER['HOME'].'/.doddns/', 0700);
$this->info("Created .doddns directory in user's home.");
}

file_put_contents(config('database.connections.sqlite.database'), "");
$this->info("Created or overwrited any actual databse");

Artisan::call('migrate', ['--force' => true]);

$this->info("Migrated tables");

return true;
});
}

private function updateToken($token)
{
if ($this->confirm('Do you wish to overwrite existing saved token?')) {
Expand Down
3 changes: 3 additions & 0 deletions app/Commands/UpdateRecords.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Helpers\DigitalOceanHelper;
use App\Helpers\SettingsHelper;
use Carbon\Carbon;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\DB;
use Ipify\Ip;
Expand Down Expand Up @@ -52,6 +53,8 @@ public function handle()
$this->digitalocean->domainRecord->update($record->domain, $record->record_id, $record->record_name, $current_ip);
;

DB::update('update records set record_updated_at = ? where id = ?', [Carbon::now()->toDatetimeString(), $record->id]);

$this->info("Updated ({$record->record_type}) {$record->record_name} of {$record->domain} to : {$current_ip}");
});

Expand Down
14 changes: 14 additions & 0 deletions app/Helpers/DigitalOceanHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,18 @@ public function __construct($token)

return $this;
}

public function getDomains()
{
return collect($this->domain->getAll())->mapWithKeys(function ($values) {
return [$values->name => $values->name];
})->toArray();
}

public function getDomainRecords($domain)
{
return collect($this->domainRecord->getAll($domain))->filter(function ($record) {
return $record->type == "CNAME" || $record->type == "A";
});
}
}
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

# X (Next release)

# 1.2.0
= Cleaning code a little
- Keeps a track of the lat update time of a record
- Shows the last update time when doing `doddns records:list`

# 1.1.0
- Hides unnecesary commands

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddRecordUpdateTimestamp extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('records', function (Blueprint $table) {
$table->timestamp('record_updated_at')->before('updated_at')->nullable();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('records', function (Blueprint $table) {
$table->dropColumn('record_updated_at');
});
}
}

0 comments on commit 0034813

Please sign in to comment.