Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
b9d27d9aa2 | ||
|
728fce1b78 | ||
|
c302c4653a | ||
|
c2e181aa75 | ||
|
00d0ea624f | ||
|
2f87752f6e |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
vendor/
|
||||
main.uml
|
||||
composer.lock
|
||||
.phpunit.result.cache
|
||||
|
@@ -17,7 +17,7 @@
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.5",
|
||||
"sabre/dav" : "~4.0.2",
|
||||
"sabre/dav" : "~4.0.3",
|
||||
"sabre/vobject": "^4.0",
|
||||
"nesbot/carbon": "^2.0.0",
|
||||
"laminas/laminas-validator": "^2.13",
|
||||
|
@@ -6,6 +6,7 @@ use Symfony\Component\Config\Definition\ConfigurationInterface;
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\Processor;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
use Carbon\CarbonTimeZone;
|
||||
|
||||
class ConfigBuilder implements ConfigurationInterface {
|
||||
private $configs = [];
|
||||
@@ -24,19 +25,32 @@ class ConfigBuilder implements ConfigurationInterface {
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$rootNode = $treeBuilder->root('configs');
|
||||
$ref = $rootNode->children()
|
||||
->arrayNode('logger')
|
||||
->canBeEnabled()
|
||||
->arrayNode('general')
|
||||
->children()
|
||||
->scalarNode('file')
|
||||
->end()
|
||||
->scalarNode('level')
|
||||
->defaultValue('ERROR')
|
||||
->validate()
|
||||
->IfNotInArray(['DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY'])
|
||||
->thenInvalid('Invalid log level %s')
|
||||
->end()
|
||||
->end();
|
||||
|
||||
->arrayNode('logger')
|
||||
->canBeEnabled()
|
||||
->children()
|
||||
->scalarNode('file')->end()
|
||||
->scalarNode('level')
|
||||
->defaultValue('ERROR')
|
||||
->validate()
|
||||
->IfNotInArray(['DEBUG', 'INFO', 'NOTICE', 'WARNING', 'ERROR', 'CRITICAL', 'ALERT', 'EMERGENCY'])
|
||||
->thenInvalid('Invalid log level %s')
|
||||
->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) {
|
||||
$ref = $ref->append($config->get());
|
||||
}
|
||||
|
@@ -2,24 +2,24 @@
|
||||
|
||||
namespace Aerex\BaikalStorage\Configs;
|
||||
|
||||
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
|
||||
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
|
||||
|
||||
class TaskwarriorConfig {
|
||||
public function get() {
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$node = $treeBuilder->root('taskwarrior');
|
||||
$node->children()
|
||||
->scalarNode('taskdata')
|
||||
->defaultValue('~/.task')
|
||||
->end()
|
||||
->scalarNode('taskrc')
|
||||
->defaultValue('~/.taskrc')
|
||||
->end()
|
||||
->scalarNode('project_tag_suffix')
|
||||
->defaultValue('project_')
|
||||
->end()
|
||||
->end();
|
||||
|
||||
$node = new ArrayNodeDefinition('taskwarrior');
|
||||
$node->canBeEnabled()
|
||||
->children()
|
||||
->scalarNode('taskdata')
|
||||
->defaultValue('~/.task')
|
||||
->end()
|
||||
->scalarNode('taskrc')
|
||||
->defaultValue('~/.taskrc')
|
||||
->end()
|
||||
->scalarNode('project_tag_suffix')
|
||||
->defaultValue('project_')
|
||||
->end()
|
||||
->end();
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
||||
|
@@ -23,16 +23,15 @@ class Console extends AbstractConsole {
|
||||
$stdin = array_merge($stdin, $this->defaultArgs, $args);
|
||||
|
||||
if (isset($input)) {
|
||||
$stdin[] = $this->convertToString($input);
|
||||
$input = $this->convertToString($input);
|
||||
}
|
||||
$process = new Process(implode(' ', $stdin), $input, $envs);
|
||||
$process = new Process(implode(' ', $stdin), null, $envs, $input);
|
||||
$process->inheritEnvironmentVariables();
|
||||
|
||||
try {
|
||||
$process->mustRun();
|
||||
return $process->getOutput();
|
||||
} catch (ProcessFailedException $error) {
|
||||
echo $error->getMessage();
|
||||
throw $error;
|
||||
}
|
||||
}
|
||||
|
@@ -9,16 +9,25 @@ class Logger {
|
||||
private $configs = ['enabled' => false];
|
||||
|
||||
function __construct($configs, $tag) {
|
||||
if (isset($configs['logger'])) {
|
||||
$this->configs = $configs['logger'];
|
||||
if (isset($configs['general']) && isset($configs['general']['logger'])) {
|
||||
$this->configs = $configs['general']['logger'];
|
||||
}
|
||||
if ($this->configs['enabled']) {
|
||||
$this->createLoggerFile();
|
||||
$this->logger = new Monolog($tag);
|
||||
$logLevel = Monolog::getLevels()[$this->configs['level']];
|
||||
$this->logger->pushHandler(new StreamHandler($this->configs['file'], $logLevel));
|
||||
}
|
||||
}
|
||||
|
||||
private 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) {
|
||||
if ($this->configs['enabled']) {
|
||||
$this->logger->debug($message);
|
||||
|
@@ -38,7 +38,7 @@ class StorageManager {
|
||||
if (!isset($this->configs)) {
|
||||
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];
|
||||
if (!isset($storage)){
|
||||
throw new \Exception();
|
||||
|
@@ -5,6 +5,7 @@ namespace Aerex\BaikalStorage\Storages;
|
||||
use Sabre\VObject\Component\VCalendar as Calendar;
|
||||
use Aerex\BaikalStorage\Logger;
|
||||
use Carbon\Carbon;
|
||||
use Carbon\CarbonTimeZone;
|
||||
|
||||
class Taskwarrior implements IStorage {
|
||||
|
||||
@@ -12,11 +13,13 @@ class Taskwarrior implements IStorage {
|
||||
private $tasks = [];
|
||||
private $configs;
|
||||
private $logger;
|
||||
private $tz;
|
||||
|
||||
public function __construct($console, $configs) {
|
||||
$this->console = $console;
|
||||
$this->configs = $configs['taskwarrior'];
|
||||
$this->configs = $configs['storages']['taskwarrior'];
|
||||
$this->logger = new Logger($configs, 'Taskwarrior');
|
||||
$this->tz = new CarbonTimeZone($configs['general']['timezone']);
|
||||
}
|
||||
|
||||
public function getConfig() {
|
||||
@@ -26,6 +29,13 @@ class Taskwarrior implements IStorage {
|
||||
public function refresh() {
|
||||
$output = $this->console->execute('task', ['sync'], null,
|
||||
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
|
||||
$this->tasks = json_decode($this->console->execute('task', ['export'], null,
|
||||
['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true);
|
||||
foreach ($this->tasks as $task) {
|
||||
if (isset($task['uid'])) {
|
||||
$this->tasks[$task['uid']] = $task;
|
||||
}
|
||||
}
|
||||
$this->logger->info($output);
|
||||
}
|
||||
|
||||
@@ -37,26 +47,26 @@ class Taskwarrior implements IStorage {
|
||||
$task['uid'] = (string)$vtodo->UID;
|
||||
}
|
||||
|
||||
if (isset($vtodo->SUMMARY) && !isset($vtodo->DESCRIPTION)){
|
||||
if (isset($vtodo->SUMMARY)){
|
||||
$task['description'] = (string)$vtodo->SUMMARY;
|
||||
} else if(isset($vtodo->DESCRIPTION)) {
|
||||
$task['description'] = (string)$vtodo->DESCRIPTION;
|
||||
}
|
||||
|
||||
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)) {
|
||||
$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)){
|
||||
$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'})) {
|
||||
$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)) {
|
||||
@@ -71,7 +81,7 @@ class Taskwarrior implements IStorage {
|
||||
}
|
||||
|
||||
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)) {
|
||||
@@ -92,13 +102,13 @@ class Taskwarrior implements IStorage {
|
||||
case 'COMPLETED':
|
||||
$task['status'] = 'completed';
|
||||
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;
|
||||
case 'CANCELED':
|
||||
$task['status'] = 'deleted';
|
||||
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;
|
||||
}
|
||||
@@ -108,8 +118,8 @@ class Taskwarrior implements IStorage {
|
||||
$task['tags'] = [];
|
||||
foreach ($vtodo->CATEGORIES as $category) {
|
||||
if (isset($this->configs['project_tag_suffix'])) {
|
||||
$projTagSuffixRegExp = sprintf('/^%s_/', $this->configs['project_tag_suffix']);
|
||||
if (preg_match($category, $projTagSuffixRegExp)) {
|
||||
$projTagSuffixRegExp = sprintf('/^%s/', $this->configs['project_tag_suffix']);
|
||||
if (preg_match($projTagSuffixRegExp, $category)) {
|
||||
$task['project'] = preg_replace($projTagSuffixRegExp, '', $category);
|
||||
continue;
|
||||
}
|
||||
@@ -122,19 +132,23 @@ class Taskwarrior implements IStorage {
|
||||
}
|
||||
|
||||
public function save(Calendar $c) {
|
||||
if (!isset($c->VTODO)){
|
||||
throw new \Exception('Calendar event does not contain VTODO');
|
||||
$this->logger->error('Calendar event does not contain VTODO');
|
||||
try {
|
||||
if (!isset($c->VTODO)){
|
||||
throw new \Exception('Calendar event does not contain VTODO');
|
||||
}
|
||||
$this->logger->info(json_encode($c->jsonSerialize()));
|
||||
$this->refresh();
|
||||
$task = $this->vObjectToTask($c->VTODO);
|
||||
$this->logger->info(json_encode($task));
|
||||
$this->logger->info(
|
||||
sprintf('Executing TASKRC = %s TASKDATA = %s task import %s', $this->configs['taskrc'], $this->configs['taskdata'], json_encode($task))
|
||||
);
|
||||
$output = $this->console->execute('task', ['import'], $task,
|
||||
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
|
||||
$this->logger->info($output);
|
||||
} catch (\Exception $e) {
|
||||
$this->logger->error($e->getTraceAsString());
|
||||
throw $e;
|
||||
}
|
||||
$this->logger->info($c->VTODO->getJsonValue());
|
||||
$this->refresh();
|
||||
$task = $this->vObjectToTask($c->VTODO);
|
||||
$this->logger->info(json_encode($task));
|
||||
$this->logger->info(
|
||||
sprintf('Executing TASKRC = %s TASKDATA = %s task import %s', $this->configs['taskrc'], $this->configs['taskdata'], $task)
|
||||
);
|
||||
$output = $this->console->execute('task', ['import'], $task,
|
||||
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
|
||||
$this->logger->info($output);
|
||||
}
|
||||
}
|
||||
|
@@ -3,6 +3,7 @@ namespace Aerex\BaikalStorage;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Aerex\BaikalStorage\Configs\ConfigBuilder;
|
||||
use Aerex\BaikalStorage\Configs\TaskwarriorConfig;
|
||||
|
||||
class ConfigTest extends TestCase {
|
||||
|
||||
@@ -12,20 +13,37 @@ class ConfigTest extends TestCase {
|
||||
|
||||
public $mockConfigBuilder;
|
||||
|
||||
public function testLoggerConfigs() {
|
||||
public function testGeneralLoggerConfigs() {
|
||||
$configs = new ConfigBuilder(__DIR__ . '/Fixtures/LoggerConfig.yaml');
|
||||
$contents = $configs->loadYaml();
|
||||
$this->assertEquals(sizeof($contents), 1);
|
||||
$this->assertArrayHasKey('logger', $contents, 'config missing logger property');
|
||||
$this->assertArrayHasKey('file', $contents['logger'], 'config missing logger.file property');
|
||||
$this->assertEquals($contents['logger']['file'], '/home/user/logger.yaml');
|
||||
$this->assertArrayHasKey('level', $contents['logger'], 'config missing logger.level property');
|
||||
$this->assertEquals($contents['logger']['level'], 'ERROR', 'ERROR is not set as default logger level');
|
||||
$this->assertArrayHasKey('enabled', $contents['logger'], 'config missing logger.enabled property');
|
||||
$this->assertTrue($contents['logger']['enabled']);
|
||||
|
||||
$this->assertArrayHasKey('general', $contents, 'config missing general config');
|
||||
$generalConfigs = $contents['general'];
|
||||
$this->assertArrayHasKey('logger', $generalConfigs, 'general config is missing logger property');
|
||||
$this->assertArrayHasKey('file', $generalConfigs['logger'], 'general logger config missing file property');
|
||||
$this->assertEquals($generalConfigs['logger']['file'], '/home/user/logger.yaml');
|
||||
$this->assertArrayHasKey('level', $generalConfigs['logger'], 'general logger config missing level property');
|
||||
$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() {
|
||||
$configs = new ConfigBuilder(__DIR__ . '/Fixtures/TaskwarriorConfig.yaml');
|
||||
$configs->add(new TaskwarriorConfig());
|
||||
$contents = $configs->loadYaml();
|
||||
$this->assertEquals(sizeof($contents), 2);
|
||||
$this->assertArrayHasKey('storages', $contents, 'storages config missing');
|
||||
$this->assertArrayHasKey('taskwarrior', $contents['storages'], 'storage config missing taskwarrior property');
|
||||
$taskwarriorConfigs = $contents['storages']['taskwarrior'];
|
||||
$this->assertArrayHasKey('taskrc', $taskwarriorConfigs, 'taskwarrior config is missing taskrc property');
|
||||
$this->assertEquals($taskwarriorConfigs['taskrc'], '/home/aerex/.taskrc');
|
||||
$this->assertArrayHasKey('taskdata', $taskwarriorConfigs, 'taskwarrior config is missing taskdata property');
|
||||
$this->assertEquals($taskwarriorConfigs['taskdata'], '/home/aerex/.task');
|
||||
$this->assertArrayHasKey('project_tag_suffix', $taskwarriorConfigs, 'taskwarrior config is missing project_tag_suffix property');
|
||||
$this->assertEquals($taskwarriorConfigs['project_tag_suffix'], 'project_');
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,2 +1,3 @@
|
||||
logger:
|
||||
file: /home/user/logger.yaml
|
||||
general:
|
||||
logger:
|
||||
file: /home/user/logger.yaml
|
||||
|
@@ -1,7 +1,10 @@
|
||||
logger:
|
||||
file: /home/aerex/baikal-storage-plugin.log
|
||||
level: DEBUG
|
||||
taskwarrior:
|
||||
taskdata: /home/aerex/.task
|
||||
taskrc: /home/aerex/.taskrc
|
||||
project_tag_suffix: project_
|
||||
general:
|
||||
logger:
|
||||
file: /home/aerex/baikal-storage-plugin.log
|
||||
level: DEBUG
|
||||
timezone: 'America/Denver'
|
||||
storages:
|
||||
taskwarrior:
|
||||
taskdata: /home/aerex/.task
|
||||
taskrc: /home/aerex/.taskrc
|
||||
project_tag_suffix: project_
|
||||
|
Reference in New Issue
Block a user