diff --git a/php/includes/argvParser.php b/php/includes/argvParser.php index dcb3f4d..eda0ad2 100644 --- a/php/includes/argvParser.php +++ b/php/includes/argvParser.php @@ -1,85 +1,87 @@ -set($name, $default); - } - - return $this; - } - - public function get(string $key, mixed $default = null): mixed - { - return $this->parsed[$key] ?? $default; - } - - public function set(string $key, mixed $value): self - { - $this->parsed[$key] = $value; - return $this; - } - - public function getRest(): string - { - return $this->get('rest', ''); - } -} - -readonly class argvParser -{ - /** - * @param array $options - * @param array $arguments - */ - public function __construct( - private array $options = [], - private array $arguments = [], - ) {} - - public function getOptionsHelp(): string - { - $line = ''; - foreach ($this->options as $option => $description) { - $line .= sprintf('[-%s (%s)] ', $option, $description); - } - foreach ($this->arguments as $argument => $description) { - $line .= sprintf('<%s (%s)> ', $argument, $description); - } - return $line; - } - - public function parseArgv(array $argv): argParsed|false - { - array_shift($argv); // shift the script name - - $parsed = (new argParsed())->initOptions($this->options); - while (str_starts_with($argv[0] ?? '', '-')) { - $option = substr($argv[0], 1, 1); - if (substr($argv[0], 2, 1) === ':') { - $data = substr($argv[0], 3); - } else { - $data = true; - } - if ($this->options[$option]) { - $parsed->set($option, $data); - } - array_shift($argv); - } - if (count($argv) < count($this->arguments)) { - return false; - } - foreach ($this->arguments as $arg => $description) { - $parsed->set($arg, array_shift($argv)); - } - $parsed->set('rest', implode(' ', $argv)); - - return $parsed; - } -} \ No newline at end of file +set($name, $default); + } + + return $this; + } + + public function get(string $key, mixed $default = null): mixed + { + return $this->parsed[$key] ?? $default; + } + + public function set(string $key, mixed $value): self + { + $this->parsed[$key] = $value; + return $this; + } + + public function getRest(): string + { + return $this->get('rest', ''); + } +} + +readonly class argvParser +{ + /** + * @param array $options + * @param array $arguments + */ + public function __construct( + private array $options = [], + private array $arguments = [], + ) { + } + + public function getOptionsHelp(): string + { + $line = ''; + foreach ($this->options as $option => $description) { + $line .= sprintf('[-%s (%s)] ', $option, $description); + } + foreach ($this->arguments as $argument => $description) { + $line .= sprintf('<%s (%s)> ', $argument, $description); + } + return $line; + } + + public function parseArgv(array $argv): argParsed|false + { + array_shift($argv); // shift the script name + + $parsed = (new argParsed())->initOptions($this->options); + while (str_starts_with($argv[0] ?? '', '-')) { + $option = substr($argv[0], 1, 1); + if (substr($argv[0], 2, 1) === ':') { + $data = substr($argv[0], 3); + } else { + $data = true; + } + if ($this->options[$option]) { + $parsed->set($option, $data); + } + array_shift($argv); + } + if (count($argv) < count($this->arguments)) { + return false; + } + foreach ($this->arguments as $arg => $description) { + $parsed->set($arg, array_shift($argv)); + } + $parsed->set('rest', implode(' ', $argv)); + + return $parsed; + } +} diff --git a/php/includes/config.php b/php/includes/config.php index 3ef709f..6473b90 100644 --- a/php/includes/config.php +++ b/php/includes/config.php @@ -1,76 +1,76 @@ -createConfig(); - } - - private function createConfig(): void - { - if (file_exists($this->getConfigFile())) { - return; - } else { - if (!is_dir($this->getConfigPath())) { - mkdir($this->getConfigPath(), 0755, true); - } - $this->writeConfig([]); - } - } -} - -readonly class config -{ - public function __construct( - private string $path, - private string $fileName = 'config.php', - ) {} - - public function configExists(): bool - { - return file_exists($this->getConfigFile()); - } - - public function get(string $key, mixed $default = null): mixed - { - if (!$this->configExists()) { - return $default; - } - $config = include $this->getConfigFile(); - return $config[$key] ?? $default; - } - - public function set(string $key, mixed $value): void - { - $config = include $this->getConfigFile(); - $config[$key] = $value; - $this->writeConfig($config); - } - - protected function getConfigFile(): string - { - return path($this->getConfigPath(), $this->fileName); - } - - protected function getConfigPath(): string - { - return $this->path; - } - - protected function writeConfig(array $config): void - { - file_put_contents($this->getConfigFile(), 'createConfig(); + } + + private function createConfig(): void + { + if (file_exists($this->getConfigFile())) { + return; + } else { + if (!is_dir($this->getConfigPath())) { + mkdir($this->getConfigPath(), 0755, true); + } + $this->writeConfig([]); + } + } +} + +readonly class config +{ + public function __construct( + private string $path, + private string $fileName = 'config.php', + ) { + } + + public function configExists(): bool + { + return file_exists($this->getConfigFile()); + } + + public function get(string $key, mixed $default = null): mixed + { + if (!$this->configExists()) { + return $default; + } + $config = include $this->getConfigFile(); + return $config[$key] ?? $default; + } + + public function set(string $key, mixed $value): void + { + $config = include $this->getConfigFile(); + $config[$key] = $value; + $this->writeConfig($config); + } + + protected function getConfigFile(): string + { + return path($this->getConfigPath(), $this->fileName); + } + + protected function getConfigPath(): string + { + return $this->path; + } + + protected function writeConfig(array $config): void + { + file_put_contents($this->getConfigFile(), 'baseUrl . 'snip/' . $id; - $headers = [ - 'X-AUTH-TOKEN: ' . $this->apiKey, - 'Accept: application/json', - ]; - - $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - $response = curl_exec($ch); - curl_close($ch); - - $response = $this->getResponse($response); - - if (isset($response['success']) && $response['success'] === true) { - return $response['data']['content']; - } else { - throw new Exception('Error fetching snip: ' . json_encode($response)); - } - // rewrite to curl - } - - public function postSnip(int $id, string $content): string - { - $url = $this->baseUrl . 'snip/' . $id; - $headers = [ - 'X-AUTH-TOKEN: ' . $this->apiKey, - 'Accept: application/json', - ]; - - $ch = curl_init($url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); - curl_setopt($ch, CURLOPT_POSTFIELDS, ['content' => $content]); - $response = curl_exec($ch); - curl_close($ch); - $response = $this->getResponse($response); - - if (isset($response['success']) && $response['success'] === true) { - return $response['data']['content']; - } else { - throw new Exception('Error updating snip: ' . json_encode($response)); - } - } - - private function getResponse(bool|string $response): array - { - $response = json_decode($response, true); - if (json_last_error() !== JSON_ERROR_NONE) { - throw new Exception('Error decoding JSON response: ' . json_last_error_msg()); - } - return $response; - } -} \ No newline at end of file +baseUrl . 'snip/' . $id; + $headers = [ + 'X-AUTH-TOKEN: ' . $this->apiKey, + 'Accept: application/json', + ]; + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + $response = curl_exec($ch); + curl_close($ch); + + $response = $this->getResponse($response); + + if (isset($response['success']) && $response['success'] === true) { + return $response['data']['content']; + } else { + throw new Exception('Error fetching snip: ' . json_encode($response)); + } + // rewrite to curl + } + + public function postSnip(int $id, string $content): string + { + $url = $this->baseUrl . 'snip/' . $id; + $headers = [ + 'X-AUTH-TOKEN: ' . $this->apiKey, + 'Accept: application/json', + ]; + + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); + curl_setopt($ch, CURLOPT_POSTFIELDS, ['content' => $content]); + $response = curl_exec($ch); + curl_close($ch); + $response = $this->getResponse($response); + + if (isset($response['success']) && $response['success'] === true) { + return $response['data']['content']; + } else { + throw new Exception('Error updating snip: ' . json_encode($response)); + } + } + + private function getResponse(bool|string $response): array + { + $response = json_decode($response, true); + if (json_last_error() !== JSON_ERROR_NONE) { + throw new Exception('Error decoding JSON response: ' . json_last_error_msg()); + } + return $response; + } +} diff --git a/php/includes/stow.php b/php/includes/stow.php index 32d6b60..9901927 100644 --- a/php/includes/stow.php +++ b/php/includes/stow.php @@ -1,126 +1,127 @@ -getNoStow($sourcePath); - $sourceFiles = array_diff(scandir($sourcePath), ['..', '.', self::CONFIG_FILE_NAME, self::STOW_IGNORE_FILE, ...$noStow]); - - foreach ($sourceFiles as $sourceFile) { - $targetFile = path($targetPath, $sourceFile); - $stowFile = path($sourcePath, $sourceFile); - - switch ($this->action) { - case self::ACTION_STOW: - $this->stow($stowFile, $targetFile); - break; - case self::ACTION_UNSTOW: - $this->unstow($stowFile, $targetFile); - break; - default: - line("Unknown action: {$this->action}"); - return; - } - } - } - - public function stow(string $stowFile, string $targetFile): void - { - if (is_link($targetFile)) { - $targetLink = readlink($targetFile); - if ($targetLink === $stowFile) { - line("File $targetFile is already stowed from $stowFile"); - } else { - if (is_dir($targetFile)) { - if ($this->unwrap) { - line("Unwrapping $targetFile from $targetLink"); - unlink($targetFile); - mkdir($targetFile); - $this->run($targetLink, $targetFile); - $this->run($stowFile, $targetFile); - } else { - line("File $targetFile is linked from $targetLink, ignoring, add -w to unwrap"); - } - }else{ - line("File $targetFile is already linked from $targetLink"); - } - } - return; - } - if (is_dir($targetFile)) { - if (is_dir($stowFile)) { - line("File $targetFile and $stowFile are folders, recurse into existing folders"); - $this->run($stowFile, $targetFile); - } else { - line("File $targetFile is a folder, but stow file $stowFile is not a folder"); - } - return; - } - if (is_file($targetFile)) { - if ($this->backup) { - $backFile = $targetFile . '.bak'; - if (is_file($backFile)) { - line("Backup file $backFile already exists"); - return; - } - rename($targetFile, $backFile); - line("Backup file $targetFile to $backFile"); - } elseif ($this->force) { - line("Todo: delete file $targetFile"); - return; - } else { - line("File $targetFile already exists"); - return; - } - } - line("Stow $targetFile from $stowFile"); - symlink($stowFile, $targetFile); - } - - public function unstow(string $stowFile, string $targetFile): void - { - if (is_link($targetFile) && readlink($targetFile) === $stowFile) { - line("Unstow $targetFile from $stowFile"); - unlink($targetFile); - return; - } - if (is_dir($targetFile)) { - if (is_dir($stowFile)) { - line("File $targetFile and $stowFile are folders, recurse into existing folders"); - $this->run($stowFile, $targetFile); - return; - } else { - line("File $targetFile is a folder, but stow file $stowFile is not a folder"); - } - } - line("File $targetFile is not stowed from $stowFile"); - } -} +getNoStow($sourcePath); + $sourceFiles = array_diff(scandir($sourcePath), ['..', '.', self::CONFIG_FILE_NAME, self::STOW_IGNORE_FILE, ...$noStow]); + + foreach ($sourceFiles as $sourceFile) { + $targetFile = path($targetPath, $sourceFile); + $stowFile = path($sourcePath, $sourceFile); + + switch ($this->action) { + case self::ACTION_STOW: + $this->stow($stowFile, $targetFile); + break; + case self::ACTION_UNSTOW: + $this->unstow($stowFile, $targetFile); + break; + default: + line("Unknown action: {$this->action}"); + return; + } + } + } + + public function stow(string $stowFile, string $targetFile): void + { + if (is_link($targetFile)) { + $targetLink = readlink($targetFile); + if ($targetLink === $stowFile) { + line("File $targetFile is already stowed from $stowFile"); + } else { + if (is_dir($targetFile)) { + if ($this->unwrap) { + line("Unwrapping $targetFile from $targetLink"); + unlink($targetFile); + mkdir($targetFile); + $this->run($targetLink, $targetFile); + $this->run($stowFile, $targetFile); + } else { + line("File $targetFile is linked from $targetLink, ignoring, add -w to unwrap"); + } + } else { + line("File $targetFile is already linked from $targetLink"); + } + } + return; + } + if (is_dir($targetFile)) { + if (is_dir($stowFile)) { + line("File $targetFile and $stowFile are folders, recurse into existing folders"); + $this->run($stowFile, $targetFile); + } else { + line("File $targetFile is a folder, but stow file $stowFile is not a folder"); + } + return; + } + if (is_file($targetFile)) { + if ($this->backup) { + $backFile = $targetFile . '.bak'; + if (is_file($backFile)) { + line("Backup file $backFile already exists"); + return; + } + rename($targetFile, $backFile); + line("Backup file $targetFile to $backFile"); + } elseif ($this->force) { + line("Todo: delete file $targetFile"); + return; + } else { + line("File $targetFile already exists"); + return; + } + } + line("Stow $targetFile from $stowFile"); + symlink($stowFile, $targetFile); + } + + public function unstow(string $stowFile, string $targetFile): void + { + if (is_link($targetFile) && readlink($targetFile) === $stowFile) { + line("Unstow $targetFile from $stowFile"); + unlink($targetFile); + return; + } + if (is_dir($targetFile)) { + if (is_dir($stowFile)) { + line("File $targetFile and $stowFile are folders, recurse into existing folders"); + $this->run($stowFile, $targetFile); + return; + } else { + line("File $targetFile is a folder, but stow file $stowFile is not a folder"); + } + } + line("File $targetFile is not stowed from $stowFile"); + } +}