Skip to content

Commit 0034813

Browse files
authored
Merge pull request #1 from jpmurray/develop
Version bump
2 parents 30790de + 9eb201f commit 0034813

8 files changed

+83
-22
lines changed

app/Commands/AddRecord.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ public function handle()
4545

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

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

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

@@ -56,9 +54,7 @@ public function handle()
5654
return;
5755
}
5856

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

6359
$records_for_menu = $records->mapWithKeys(function ($values, $key) {
6460
return [$key => "{$values->name} ({$values->type}): {$values->data}"];

app/Commands/ListRecords.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Commands;
44

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

3435
$records->each(function ($record, $key) {
36+
$last_update = is_null($record->record_updated_at) ? "Never" : Carbon::parse($record->record_updated_at)->toDatetimeString();
37+
3538
$this->line("");
36-
$this->info("[{$key}] ({$record->record_type}) {$record->record_name} of {$record->domain}");
39+
$this->info("[{$key}] ({$record->record_type}) {$record->record_name} of {$record->domain}. Last updated: {$last_update} (UTC).");
3740
});
3841
}
3942
}

app/Commands/RemoveRecord.php

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class RemoveRecord extends Command
3030
public function handle()
3131
{
3232
$records = DB::table('records')->get();
33+
3334
$records_for_menu = collect($records)->mapWithKeys(function ($record) {
3435
return [$record->id => "({$record->record_type}) {$record->record_name} of {$record->domain}"];
3536
})->toArray();

app/Commands/Setup.php

+20-15
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,7 @@ class Setup extends Command
3535
public function handle()
3636
{
3737
if ($this->confirm("This will destroy any existing doddns configuration. Is that ok?")) {
38-
$this->task("Creating local database", function () {
39-
if (!is_dir($_SERVER['HOME'].'/.doddns/')) {
40-
mkdir($_SERVER['HOME'].'/.doddns/', 0700);
41-
$this->info("Created .doddns directory in user's home.");
42-
}
43-
44-
file_put_contents(config('database.connections.sqlite.database'), "");
45-
$this->info("Created or overwrited any actual databse");
46-
47-
Artisan::call('migrate', ['--force' => true]);
48-
49-
$this->info("Migrated tables");
50-
51-
return true;
52-
});
38+
$this->createDatabase();
5339
}
5440

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

54+
private function createDatabase()
55+
{
56+
$this->task("Creating local database", function () {
57+
if (!is_dir($_SERVER['HOME'].'/.doddns/')) {
58+
mkdir($_SERVER['HOME'].'/.doddns/', 0700);
59+
$this->info("Created .doddns directory in user's home.");
60+
}
61+
62+
file_put_contents(config('database.connections.sqlite.database'), "");
63+
$this->info("Created or overwrited any actual databse");
64+
65+
Artisan::call('migrate', ['--force' => true]);
66+
67+
$this->info("Migrated tables");
68+
69+
return true;
70+
});
71+
}
72+
6873
private function updateToken($token)
6974
{
7075
if ($this->confirm('Do you wish to overwrite existing saved token?')) {

app/Commands/UpdateRecords.php

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Helpers\DigitalOceanHelper;
66
use App\Helpers\SettingsHelper;
7+
use Carbon\Carbon;
78
use Illuminate\Console\Scheduling\Schedule;
89
use Illuminate\Support\Facades\DB;
910
use Ipify\Ip;
@@ -52,6 +53,8 @@ public function handle()
5253
$this->digitalocean->domainRecord->update($record->domain, $record->record_id, $record->record_name, $current_ip);
5354
;
5455

56+
DB::update('update records set record_updated_at = ? where id = ?', [Carbon::now()->toDatetimeString(), $record->id]);
57+
5558
$this->info("Updated ({$record->record_type}) {$record->record_name} of {$record->domain} to : {$current_ip}");
5659
});
5760

app/Helpers/DigitalOceanHelper.php

+14
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,18 @@ public function __construct($token)
3030

3131
return $this;
3232
}
33+
34+
public function getDomains()
35+
{
36+
return collect($this->domain->getAll())->mapWithKeys(function ($values) {
37+
return [$values->name => $values->name];
38+
})->toArray();
39+
}
40+
41+
public function getDomainRecords($domain)
42+
{
43+
return collect($this->domainRecord->getAll($domain))->filter(function ($record) {
44+
return $record->type == "CNAME" || $record->type == "A";
45+
});
46+
}
3347
}

changelog.md

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# Changelog
22

3+
# X (Next release)
4+
5+
# 1.2.0
6+
= Cleaning code a little
7+
- Keeps a track of the lat update time of a record
8+
- Shows the last update time when doing `doddns records:list`
9+
310
# 1.1.0
411
- Hides unnecesary commands
512

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class AddRecordUpdateTimestamp extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('records', function (Blueprint $table) {
17+
$table->timestamp('record_updated_at')->before('updated_at')->nullable();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('records', function (Blueprint $table) {
29+
$table->dropColumn('record_updated_at');
30+
});
31+
}
32+
}

0 commit comments

Comments
 (0)