6 Commits

Author SHA1 Message Date
Aerex
df7ab092cc fix: Added logger as a dependency for storage class 2020-06-11 11:43:56 -05:00
Aerex
8cafda3f26 fix: Upgraded sabre/dav 2020-06-11 11:24:06 -05:00
Aerex
b9d27d9aa2 fix: Upgrade sabre/dav 2020-06-11 11:21:28 -05:00
Aerex
728fce1b78 fix: Created log file if files does not exist 2020-06-11 11:10:45 -05:00
Aerex
c302c4653a feat(tw): Applied timezone to datetimes that are not given in local time 2020-06-03 00:46:06 -05:00
Aerex
c2e181aa75 refactor: Added general config validations and timezone config 2020-06-03 00:04:43 -05:00
12 changed files with 119 additions and 73 deletions

View File

@@ -17,7 +17,7 @@
], ],
"require": { "require": {
"php": ">=5.5", "php": ">=5.5",
"sabre/dav" : "~4.0.2", "sabre/dav" : "~4.1.0",
"sabre/vobject": "^4.0", "sabre/vobject": "^4.0",
"nesbot/carbon": "^2.0.0", "nesbot/carbon": "^2.0.0",
"laminas/laminas-validator": "^2.13", "laminas/laminas-validator": "^2.13",

View File

@@ -6,6 +6,7 @@ use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Processor; use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;
use Carbon\CarbonTimeZone;
class ConfigBuilder implements ConfigurationInterface { class ConfigBuilder implements ConfigurationInterface {
private $configs = []; private $configs = [];
@@ -24,6 +25,8 @@ class ConfigBuilder implements ConfigurationInterface {
$treeBuilder = new TreeBuilder(); $treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('configs'); $rootNode = $treeBuilder->root('configs');
$ref = $rootNode->children() $ref = $rootNode->children()
->arrayNode('general')
->children()
->arrayNode('logger') ->arrayNode('logger')
->canBeEnabled() ->canBeEnabled()
->children() ->children()
@@ -36,8 +39,18 @@ class ConfigBuilder implements ConfigurationInterface {
->end() ->end()
->end() ->end()
->end() ->end()
->end(); ->end()
->scalarNode('timezone')
->defaultValue('UTC')
->validate()
->IfNotInArray(CarbonTimeZone::listIdentifiers())
->thenInvalid('Invalid timezone identifier %s')
->end()
->end()
->end()
->end()
->arrayNode('storages')
->children();
foreach ($this->configs as $config) { foreach ($this->configs as $config) {
$ref = $ref->append($config->get()); $ref = $ref->append($config->get());
} }

View File

@@ -25,7 +25,7 @@ class Console extends AbstractConsole {
if (isset($input)) { if (isset($input)) {
$input = $this->convertToString($input); $input = $this->convertToString($input);
} }
$process = new Process(implode(' ', $stdin), $input, $envs); $process = new Process(implode(' ', $stdin), null, $envs, $input);
$process->inheritEnvironmentVariables(); $process->inheritEnvironmentVariables();
try { try {

View File

@@ -9,16 +9,25 @@ class Logger {
private $configs = ['enabled' => false]; private $configs = ['enabled' => false];
function __construct($configs, $tag) { function __construct($configs, $tag) {
if (isset($configs['logger'])) { if (isset($configs['general']) && isset($configs['general']['logger'])) {
$this->configs = $configs['logger']; $this->configs = $configs['general']['logger'];
} }
if ($this->configs['enabled']) { if ($this->configs['enabled']) {
$this->createLoggerFile();
$this->logger = new Monolog($tag); $this->logger = new Monolog($tag);
$logLevel = Monolog::getLevels()[$this->configs['level']]; $logLevel = Monolog::getLevels()[$this->configs['level']];
$this->logger->pushHandler(new StreamHandler($this->configs['file'], $logLevel)); $this->logger->pushHandler(new StreamHandler($this->configs['file'], $logLevel));
} }
} }
public function createLoggerFile() {
if (!file_exists($this->configs['file'])) {
if (!fopen($this->configs['file'], 'w')) {
throw new \Exception(sprintf('Could not create logger file %s', $this->configs['file']));
}
}
}
public function debug($message) { public function debug($message) {
if ($this->configs['enabled']) { if ($this->configs['enabled']) {
$this->logger->debug($message); $this->logger->debug($message);

View File

@@ -2,6 +2,7 @@
namespace Aerex\BaikalStorage; namespace Aerex\BaikalStorage;
use Aerex\BaikalStorage\Logger;
use Aerex\BaikalStorage\Storages\Taskwarrior; use Aerex\BaikalStorage\Storages\Taskwarrior;
use Aerex\BaikalStorage\Configs\ConfigBuilder; use Aerex\BaikalStorage\Configs\ConfigBuilder;
use Aerex\BaikalStorage\Configs\TaskwarriorConfig; use Aerex\BaikalStorage\Configs\TaskwarriorConfig;
@@ -33,7 +34,7 @@ class Plugin extends ServerPlugin {
/** /**
* Creates the Taskwarrior plugin * Creates the Storage plugin
* *
* @param CalendarProcessor $TWCalManager * @param CalendarProcessor $TWCalManager
* *
@@ -56,7 +57,7 @@ class Plugin extends ServerPlugin {
*/ */
public function initializeStorages($configs) { public function initializeStorages($configs) {
$taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), $configs); $taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), $configs, new Logger($configs, 'Taskwarrior'););
$this->storageManager->addStorage(Taskwarrior::NAME, $taskwarrior); $this->storageManager->addStorage(Taskwarrior::NAME, $taskwarrior);
} }

View File

@@ -38,7 +38,7 @@ class StorageManager {
if (!isset($this->configs)) { if (!isset($this->configs)) {
throw new \Exception('StorageManger was not initialize or configs are not defined'); throw new \Exception('StorageManger was not initialize or configs are not defined');
} }
foreach ($this->configs as $key => $value) { foreach ($this->configs['storages'] as $key => $value) {
$storage = $this->storages[$key]; $storage = $this->storages[$key];
if (!isset($storage)){ if (!isset($storage)){
throw new \Exception(); throw new \Exception();

View File

@@ -3,8 +3,8 @@
namespace Aerex\BaikalStorage\Storages; namespace Aerex\BaikalStorage\Storages;
use Sabre\VObject\Component\VCalendar as Calendar; use Sabre\VObject\Component\VCalendar as Calendar;
use Aerex\BaikalStorage\Logger;
use Carbon\Carbon; use Carbon\Carbon;
use Carbon\CarbonTimeZone;
class Taskwarrior implements IStorage { class Taskwarrior implements IStorage {
@@ -12,11 +12,13 @@ class Taskwarrior implements IStorage {
private $tasks = []; private $tasks = [];
private $configs; private $configs;
private $logger; private $logger;
private $tz;
public function __construct($console, $configs) { public function __construct($console, $configs, $logger) {
$this->console = $console; $this->console = $console;
$this->configs = $configs['taskwarrior']; $this->configs = $configs['storages']['taskwarrior'];
$this->logger = new Logger($configs, 'Taskwarrior'); $this->logger = $logger;
$this->tz = new CarbonTimeZone($configs['general']['timezone']);
} }
public function getConfig() { public function getConfig() {
@@ -26,9 +28,9 @@ class Taskwarrior implements IStorage {
public function refresh() { public function refresh() {
$output = $this->console->execute('task', ['sync'], null, $output = $this->console->execute('task', ['sync'], null,
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]); ['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
$tasks = json_decode($this->console->execute('task', ['export'], null, $this->tasks = json_decode($this->console->execute('task', ['export'], null,
['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true); ['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true);
foreach ($tasks as $task) { foreach ($this->tasks as $task) {
if (isset($task['uid'])) { if (isset($task['uid'])) {
$this->tasks[$task['uid']] = $task; $this->tasks[$task['uid']] = $task;
} }
@@ -51,19 +53,19 @@ class Taskwarrior implements IStorage {
} }
if (isset($vtodo->DTSTAMP)){ if (isset($vtodo->DTSTAMP)){
$task['entry'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C)); $task['entry'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
} }
if (isset($vtodo->DTSTART)) { if (isset($vtodo->DTSTART)) {
$task['start'] = new Carbon($vtodo->DTSTART->getDateTime()->format(\DateTime::W3C)); $task['start'] = new Carbon($vtodo->DTSTART->getDateTime()->format(\DateTime::W3C), $this->tz);
} }
if (isset($vtodo->DTEND)){ if (isset($vtodo->DTEND)){
$task['end'] = new Carbon($vtodo->DTEND->getDateTime()->format(\DateTime::W3C)); $task['end'] = new Carbon($vtodo->DTEND->getDateTime()->format(\DateTime::W3C), $this->tz);
} }
if (isset($vtodo->{'LAST-MODIFIED'})) { if (isset($vtodo->{'LAST-MODIFIED'})) {
$task['modified'] = new Carbon($vtodo->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C)); $task['modified'] = new Carbon($vtodo->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C), $this->tz);
} }
if (isset($vtodo->PRIORITY)) { if (isset($vtodo->PRIORITY)) {
@@ -78,7 +80,7 @@ class Taskwarrior implements IStorage {
} }
if (isset($vtodo->DUE)){ if (isset($vtodo->DUE)){
$task['due'] = new Carbon($vtodo->DUE->getDateTime()->format(\DateTime::W3C)); $task['due'] = new Carbon($vtodo->DUE->getDateTime());
} }
if (isset($vtodo->RRULE)) { if (isset($vtodo->RRULE)) {
@@ -99,13 +101,13 @@ class Taskwarrior implements IStorage {
case 'COMPLETED': case 'COMPLETED':
$task['status'] = 'completed'; $task['status'] = 'completed';
if (!isset($task['end'])) { if (!isset($task['end'])) {
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C)); $task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
} }
break; break;
case 'CANCELED': case 'CANCELED':
$task['status'] = 'deleted'; $task['status'] = 'deleted';
if (!isset($task['end'])) { if (!isset($task['end'])) {
$task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C)); $task['end'] = new Carbon($vtodo->DTSTAMP->getDateTime()->format(\DateTime::W3C), $this->tz);
} }
break; break;
} }

View File

@@ -13,18 +13,21 @@ class ConfigTest extends TestCase {
public $mockConfigBuilder; public $mockConfigBuilder;
public function testLoggerConfigs() { public function testGeneralLoggerConfigs() {
$configs = new ConfigBuilder(__DIR__ . '/Fixtures/LoggerConfig.yaml'); $configs = new ConfigBuilder(__DIR__ . '/Fixtures/LoggerConfig.yaml');
$contents = $configs->loadYaml(); $contents = $configs->loadYaml();
$this->assertEquals(sizeof($contents), 1); $this->assertEquals(sizeof($contents), 1);
$this->assertArrayHasKey('logger', $contents, 'config missing logger property'); $this->assertArrayHasKey('general', $contents, 'config missing general config');
$this->assertArrayHasKey('file', $contents['logger'], 'config missing logger.file property'); $generalConfigs = $contents['general'];
$this->assertEquals($contents['logger']['file'], '/home/user/logger.yaml'); $this->assertArrayHasKey('logger', $generalConfigs, 'general config is missing logger property');
$this->assertArrayHasKey('level', $contents['logger'], 'config missing logger.level property'); $this->assertArrayHasKey('file', $generalConfigs['logger'], 'general logger config missing file property');
$this->assertEquals($contents['logger']['level'], 'ERROR', 'ERROR is not set as default logger level'); $this->assertEquals($generalConfigs['logger']['file'], '/home/user/logger.yaml');
$this->assertArrayHasKey('enabled', $contents['logger'], 'config missing logger.enabled property'); $this->assertArrayHasKey('level', $generalConfigs['logger'], 'general logger config missing level property');
$this->assertTrue($contents['logger']['enabled']); $this->assertEquals($generalConfigs['logger']['level'], 'ERROR', 'ERROR is not set as default logger level');
$this->assertArrayHasKey('enabled', $generalConfigs['logger'], 'general config logger enabled property is missing');
$this->assertTrue($generalConfigs['logger']['enabled']);
$this->assertArrayHasKey('timezone', $generalConfigs, 'general config is missing timezone property');
$this->assertEquals($generalConfigs['timezone'], 'UTC', 'UTC is not set as default timezone');
} }
public function testTaskwarriorConfig() { public function testTaskwarriorConfig() {
@@ -32,19 +35,15 @@ class ConfigTest extends TestCase {
$configs->add(new TaskwarriorConfig()); $configs->add(new TaskwarriorConfig());
$contents = $configs->loadYaml(); $contents = $configs->loadYaml();
$this->assertEquals(sizeof($contents), 2); $this->assertEquals(sizeof($contents), 2);
$this->assertArrayHasKey('logger', $contents, 'config missing logger property'); $this->assertArrayHasKey('storages', $contents, 'storages config missing');
$this->assertArrayHasKey('file', $contents['logger'], 'config missing logger.file property'); $this->assertArrayHasKey('taskwarrior', $contents['storages'], 'storage config missing taskwarrior property');
$this->assertArrayHasKey('level', $contents['logger'], 'config missing logger.level property'); $taskwarriorConfigs = $contents['storages']['taskwarrior'];
$this->assertArrayHasKey('enabled', $contents['logger'], 'config missing logger.enabled property'); $this->assertArrayHasKey('taskrc', $taskwarriorConfigs, 'taskwarrior config is missing taskrc property');
$this->assertArrayHasKey('taskwarrior', $contents, 'config missing taskwarrior property'); $this->assertEquals($taskwarriorConfigs['taskrc'], '/home/aerex/.taskrc');
$this->assertArrayHasKey('taskrc', $contents['taskwarrior'], 'config missing taskwarrior.taskrc property'); $this->assertArrayHasKey('taskdata', $taskwarriorConfigs, 'taskwarrior config is missing taskdata property');
$this->assertEquals($contents['taskwarrior']['taskrc'], '/home/aerex/.taskrc'); $this->assertEquals($taskwarriorConfigs['taskdata'], '/home/aerex/.task');
$this->assertArrayHasKey('taskdata', $contents['taskwarrior'], 'config missing taskwarrior.taskdata property'); $this->assertArrayHasKey('project_tag_suffix', $taskwarriorConfigs, 'taskwarrior config is missing project_tag_suffix property');
$this->assertEquals($contents['taskwarrior']['taskdata'], '/home/aerex/.task'); $this->assertEquals($taskwarriorConfigs['project_tag_suffix'], 'project_');
$this->assertArrayHasKey('project_tag_suffix', $contents['taskwarrior'], 'config missing taskwarrior.taskdata property'); }
$this->assertEquals($contents['taskwarrior']['project_tag_suffix'], 'project_');
}
} }

View File

@@ -1,2 +1,3 @@
general:
logger: logger:
file: /home/user/logger.yaml file: /home/user/logger.yaml

View File

@@ -1,6 +1,9 @@
general:
logger: logger:
file: /home/aerex/baikal-storage-plugin.log file: /home/aerex/baikal-storage-plugin.log
level: DEBUG level: DEBUG
timezone: 'America/Denver'
storages:
taskwarrior: taskwarrior:
taskdata: /home/aerex/.task taskdata: /home/aerex/.task
taskrc: /home/aerex/.taskrc taskrc: /home/aerex/.taskrc

View File

@@ -6,7 +6,7 @@ use PHPUnit\Framework\TestCase;
use Aerex\BaikalStorage\AbstractConsole; use Aerex\BaikalStorage\AbstractConsole;
use Aerex\BaikalStorage\Configs\ConfigBuilder; use Aerex\BaikalStorage\Configs\ConfigBuilder;
use Aerex\BaikalStorage\Storages\Taskwarrior; use Aerex\BaikalStorage\Storages\Taskwarrior;
use Aerex\BaikalStorage\Storages\IStorage; use Aerex\BaikalStorage\Logger;
use Sabre\VObject\Component\VCalendar as Calendar; use Sabre\VObject\Component\VCalendar as Calendar;
class StorageManagerTest extends TestCase { class StorageManagerTest extends TestCase {
@@ -27,11 +27,20 @@ class StorageManagerTest extends TestCase {
->getMock(); ->getMock();
$this->mockConsole = $this->createMock(AbstractConsole::class); $this->mockConsole = $this->createMock(AbstractConsole::class);
$this->mockStorage = $this->createMock(Taskwarrior::class); $this->mockStorage = $this->createMock(Taskwarrior::class);
$this->mockLogger = $this->createMock(Logger::class);
$this->configs = [
'general' => [
'logger' => ['file' => '', 'level'=> 'DEBUG', 'enabled' => true],
'timezone' => 'UTC'
],
'storages' => [
'taskwarrior' => ['taskrc' => '', 'taskdata' => '']
]
];
} }
public function testAddTaskwarriorStorage() { public function testAddTaskwarriorStorage() {
$configs = ['taskwarrior' => ['taskrc' => '', 'taskdata' => '']]; $tw = new Taskwarrior($this->mockConsole, $this->configs, $this->mockLogger);
$tw = new Taskwarrior($this->mockConsole, $configs);
$manager = new StorageManager($this->mockConfigBuilder); $manager = new StorageManager($this->mockConfigBuilder);
$manager->addStorage(Taskwarrior::NAME, $tw); $manager->addStorage(Taskwarrior::NAME, $tw);
$storages = $manager->getStorages(); $storages = $manager->getStorages();
@@ -46,8 +55,7 @@ class StorageManagerTest extends TestCase {
->method('save') ->method('save')
->with($this->equalTo($cal)); ->with($this->equalTo($cal));
$configs = ['taskwarrior' => ['taskrc' => '', 'taskdata' => '']]; $manager = new StorageManager($this->configs);
$manager = new StorageManager($configs);
$manager->addStorage(Taskwarrior::NAME, $this->mockStorage); $manager->addStorage(Taskwarrior::NAME, $this->mockStorage);
$manager->import($cal); $manager->import($cal);

View File

@@ -4,6 +4,7 @@ namespace Aerex\BaikalStorage;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Aerex\BaikalStorage\AbstractConsole; use Aerex\BaikalStorage\AbstractConsole;
use Aerex\BaikalStorage\Logger;
use Sabre\VObject\Component\VCalendar as Calendar; use Sabre\VObject\Component\VCalendar as Calendar;
use Aerex\BaikalStorage\Storages\Taskwarrior; use Aerex\BaikalStorage\Storages\Taskwarrior;
@@ -16,11 +17,20 @@ class TaskwarriorTest extends TestCase {
protected function setUp(): void { protected function setUp(): void {
$this->mockConsole = $this->createMock(AbstractConsole::class); $this->mockConsole = $this->createMock(AbstractConsole::class);
$this->mockLogger = $this->createMock(Logger::class);
} }
public function testVObjectToTask() { public function testVObjectToTask() {
$configs = ['taskwarrior' => ['taskrc' => '', 'taskdata' => ''], 'logger' => ['file' => '', 'level'=> 'DEBUG', 'enabled' => true]]; $configs = [
$this->taskwarrior = new Taskwarrior($this->mockConsole, $configs); 'general' => [
'logger' => ['file' => '', 'level'=> 'DEBUG', 'enabled' => true],
'timezone' => 'UTC'
],
'storages' => [
'taskwarrior' => ['taskrc' => '', 'taskdata' => '']
]
];
$this->taskwarrior = new Taskwarrior($this->mockConsole, $configs, $this->mockLogger);
$vcalendar = new Calendar([ $vcalendar = new Calendar([
'VTODO' => [ 'VTODO' => [
'SUMMARY' => 'Finish project', 'SUMMARY' => 'Finish project',