From 34e9e23ec43c4501610ccec8682f42261022d07a Mon Sep 17 00:00:00 2001 From: Kouhei Sano Date: Sat, 3 Apr 2021 09:50:34 +0900 Subject: [PATCH 1/6] refactor: slack --- src/Main.php | 111 ++++++++++----- src/Slack.php | 91 ++++++------ src/Slack/BlockKit/Block/BlockInterface.php | 11 ++ src/Slack/BlockKit/Block/Context.php | 58 ++++++++ src/Slack/BlockKit/Block/Divider.php | 33 +++++ src/Slack/BlockKit/Block/Header.php | 46 ++++++ src/Slack/BlockKit/Block/Image.php | 61 ++++++++ src/Slack/BlockKit/Block/Section.php | 100 +++++++++++++ src/Slack/BlockKit/Element/Button.php | 111 +++++++++++++++ .../BlockKit/Element/ElementInterface.php | 11 ++ .../BlockKit/Element}/Image.php | 14 +- src/Slack/BlockKit/Element/MarkdownText.php | 39 +++++ src/Slack/BlockKit/Element/PlainText.php | 39 +++++ src/Slack/BlockKit/Message.php | 86 +++++++++++ src/SlackBlock.php | 36 ----- src/SlackBlock/Context.php | 39 ----- src/SlackBlock/ContextElement.php | 68 --------- src/SlackBlock/Divider.php | 18 --- src/SlackBlock/Header.php | 36 ----- src/SlackBlock/Section.php | 133 ------------------ src/SlackBlock/SectionField.php | 68 --------- src/SlackBlockInterface.php | 11 -- 22 files changed, 726 insertions(+), 494 deletions(-) create mode 100644 src/Slack/BlockKit/Block/BlockInterface.php create mode 100644 src/Slack/BlockKit/Block/Context.php create mode 100644 src/Slack/BlockKit/Block/Divider.php create mode 100644 src/Slack/BlockKit/Block/Header.php create mode 100644 src/Slack/BlockKit/Block/Image.php create mode 100644 src/Slack/BlockKit/Block/Section.php create mode 100644 src/Slack/BlockKit/Element/Button.php create mode 100644 src/Slack/BlockKit/Element/ElementInterface.php rename src/{SlackBlock => Slack/BlockKit/Element}/Image.php (59%) create mode 100644 src/Slack/BlockKit/Element/MarkdownText.php create mode 100644 src/Slack/BlockKit/Element/PlainText.php create mode 100644 src/Slack/BlockKit/Message.php delete mode 100644 src/SlackBlock.php delete mode 100644 src/SlackBlock/Context.php delete mode 100644 src/SlackBlock/ContextElement.php delete mode 100644 src/SlackBlock/Divider.php delete mode 100644 src/SlackBlock/Header.php delete mode 100644 src/SlackBlock/Section.php delete mode 100644 src/SlackBlock/SectionField.php delete mode 100644 src/SlackBlockInterface.php diff --git a/src/Main.php b/src/Main.php index 6cd2f65..c6cf949 100644 --- a/src/Main.php +++ b/src/Main.php @@ -3,12 +3,13 @@ namespace Rocket; use Phar; -use Rocket\SlackBlock\Context as SlackBlockContext; -use Rocket\SlackBlock\ContextElement as SlackBlockContextElement; -use Rocket\SlackBlock\Divider as SlackBlockDivider; -use Rocket\SlackBlock\Header as SlackBlockHeader; -use Rocket\SlackBlock\Section as SlackBlockSection; -use Rocket\SlackBlock\SectionField as SlackBlockSectionField; +use Rocket\Slack\BlockKit\Block\Context as SlackContext; +use Rocket\Slack\BlockKit\Block\Divider as SlackDivider; +use Rocket\Slack\BlockKit\Block\Header as SlackHeader; +use Rocket\Slack\BlockKit\Block\Section as SlackSection; +use Rocket\Slack\BlockKit\Element\MarkdownText as SlackMarkdownText; +use Rocket\Slack\BlockKit\Element\PlainText as SlackPlainText; +use Rocket\Slack\BlockKit\Message as SlackMessage; class Main { @@ -30,37 +31,45 @@ public function run() // INIT if ($this->options->hasInit()) { $this->printConfig($this->options->getInit()); + return; } // HELP if ($this->options->hasHelp()) { $this->printHelp(); + return; } // UPGRADE if ($this->options->hasUpgrade()) { $this->upgrade(); + return; } // CONFIGURE if (! $this->options->hasConfig()) { $this->printUsage(); + return; } + $config_path = realpath($this->options->getConfig()); if (! file_exists($config_path)) { $this->printInit(); + return; } + if ($this->options->hasVerify()) { if (Configure::verify($config_path)) { $this->printInfo($config_path . ': OK'); } else { $this->printError($config_path . ': NG'); } + return; } @@ -70,6 +79,7 @@ public function run() // USER CHECK if (posix_getpwuid(posix_geteuid())['name'] !== $configure->read('user')) { $this->printError('can not executed user.'); + return; } @@ -84,6 +94,7 @@ public function run() if (! $slackResult->isOk()) { $this->printError($slackResult->getError()); } + return; } @@ -93,6 +104,7 @@ public function run() $directory_path = $configure->read('source.directory'); if (! is_dir($directory_path)) { $this->printNotfoundPath($directory_path); + return; } @@ -117,8 +129,10 @@ public function run() } else { $this->printInfo('> ' . $command->string()); $this->printError($command->getOutputString()); + return; } + if (strpos($command->getOutputString(), 'local out of date') === false) { return; } @@ -144,6 +158,7 @@ public function run() } else { $this->printError('> ' . $command->string()); $this->printError($command->getOutputString()); + return; } @@ -192,6 +207,7 @@ public function run() $this->printError('> rsync'); $this->printError($command->getOutputString()); } + break; case 'force': $command @@ -205,6 +221,7 @@ public function run() $this->printError('> rsync'); $this->printError($command->getOutputString()); } + break; case 'confirm': $_command = clone $command; @@ -234,6 +251,7 @@ public function run() $this->printError($_command->getOutputString()); } } + break; } @@ -275,73 +293,88 @@ public function run() // NOTIFICATION if ($is_file_changed) { - $slackBlock = new SlackBlock(); - $slackBlock + $message = new SlackMessage(); + $message ->addBlock( - new SlackBlockHeader('Deploy successful') + new SlackHeader(new SlackPlainText('Deploy successful')) ) ->addBlock( - SlackBlockSection::plain_text(get_current_user() . ' was deployed :simple_smile:') + (new SlackSection())->setText( + new SlackPlainText(get_current_user() . ' was deployed :simple_smile:') + ) ) ->addBlock( - SlackBlockSection::fields() + (new SlackSection()) ->addField( - SlackBlockSectionField::markdown('*Hostname:*' . PHP_EOL . gethostname()) + new SlackMarkdownText('*Hostname:*' . PHP_EOL . gethostname()) ) ->addField( - SlackBlockSectionField::markdown('*URL:*' . PHP_EOL . $configure->read('url')) + new SlackMarkdownText('*URL:*' . PHP_EOL . $configure->read('url')) ) ); if ($git_pull_log !== null) { - $slackBlock + $message ->addBlock( - new SlackBlockDivider() + new SlackDivider() ) ->addBlock( - SlackBlockSection::bold('Git pull') + (new SlackSection()) + ->addField( + new SlackMarkdownText('*Git pull*') + ) ); - $chunks = str_split($git_pull_log, SlackBlock::MAX_LENGTH - 6); + $chunks = str_split($git_pull_log, SlackSection::TEXT_MAX_LENGTH - 6); foreach ($chunks as $chunk) { - $slackBlock + $message ->addBlock( - SlackBlockSection::code_block($chunk) + (new SlackSection()) + ->addField( + new SlackMarkdownText('```' . $chunk . '```') + ) ); } } + if ($sync_log !== null) { - $slackBlock + $message ->addBlock( - new SlackBlockDivider() + new SlackDivider() ) ->addBlock( - SlackBlockSection::bold('Rsync') + (new SlackSection()) + ->addField( + new SlackMarkdownText('*Rsync*') + ) ); - $chunks = str_split($sync_log, SlackBlock::MAX_LENGTH - 6); + $chunks = str_split($sync_log, SlackSection::TEXT_MAX_LENGTH - 6); foreach ($chunks as $chunk) { - $slackBlock + $message ->addBlock( - SlackBlockSection::code_block($chunk) + (new SlackSection()) + ->addField( + new SlackMarkdownText('```' . $chunk . '```') + ) ); } } - $slackBlock + $message ->addBlock( - new SlackBlockDivider() + new SlackDivider() ) ->addBlock( - (new SlackBlockContext()) + (new SlackContext()) ->addElement( - SlackBlockContextElement::markdown('Date: ' . date('Y/m/d H:i:s')) + new SlackMarkdownText('Date: ' . date("Y/m/d H:i:s")) ) ->addElement( - SlackBlockContextElement::markdown('Version: ' . self::appName() . ' ' . self::VERSION) + new SlackMarkdownText('Version: ' . self::appName() . ' ' . self::VERSION) ) ->addElement( - SlackBlockContextElement::markdown('Configuration: ' . $configure->getConfigPath()) + new SlackMarkdownText('Configuration: ' . $configure->getConfigPath()) ) ); @@ -350,7 +383,7 @@ public function run() $configure->read('slack.channel'), $configure->read('slack.username') ); - $slackResult = $slack->send($slackBlock); + $slackResult = $slack->send($message); if (! $slackResult->isOk()) { $this->printError($slackResult->getError()); } @@ -372,9 +405,11 @@ public static function appName() private function upgrade() { - $working_directory_path = sys_get_temp_dir() . '/' . 'rocket-' . substr(str_shuffle('1234567890abcdefghijklmnopqrstuvwxyz'), 0, 8); - if ( ! mkdir($working_directory_path) && ! is_dir($working_directory_path)) { + $working_directory_path = sys_get_temp_dir() . '/' . 'rocket-' . substr(str_shuffle('1234567890abcdefghijklmnopqrstuvwxyz'), + 0, 8); + if (! mkdir($working_directory_path) && ! is_dir($working_directory_path)) { $this->printError(sprintf('Directory "%s" was not created.', $working_directory_path)); + return; } @@ -384,8 +419,9 @@ private function upgrade() $updater = new Updater($working_directory_path); $result = $updater->upgrade(); - if ( ! $result->isOk()) { + if (! $result->isOk()) { $this->printError($result->getError()); + return; } @@ -419,14 +455,19 @@ private function printConfig($template) { if ($template === 'cakephp3') { echo file_get_contents(__DIR__ . '/config/cakephp3.json') . PHP_EOL; + return; } + if ($template === 'eccube4') { echo file_get_contents(__DIR__ . '/config/eccube4.json') . PHP_EOL; + return; } + if ($template === 'wordpress') { echo file_get_contents(__DIR__ . '/config/wordpress.json') . PHP_EOL; + return; } diff --git a/src/Slack.php b/src/Slack.php index 847532e..c87d232 100644 --- a/src/Slack.php +++ b/src/Slack.php @@ -2,13 +2,14 @@ namespace Rocket; -use Rocket\SlackBlock\Context; -use Rocket\SlackBlock\ContextElement; -use Rocket\SlackBlock\Divider; -use Rocket\SlackBlock\Header; -use Rocket\SlackBlock\Image; -use Rocket\SlackBlock\Section; -use Rocket\SlackBlock\SectionField; +use Rocket\Slack\BlockKit\Block\Context; +use Rocket\Slack\BlockKit\Block\Divider; +use Rocket\Slack\BlockKit\Block\Header; +use Rocket\Slack\BlockKit\Block\Image as BlockImage; +use Rocket\Slack\BlockKit\Block\Section; +use Rocket\Slack\BlockKit\Element\MarkdownText; +use Rocket\Slack\BlockKit\Element\PlainText; +use Rocket\Slack\BlockKit\Message; class Slack { @@ -24,14 +25,14 @@ class Slack /** * Slack constructor. * - * @param string $url + * @param string $url * @param string|null $channel * @param string|null $username */ public function __construct($url, $channel = null, $username = null) { - $this->url = $url; - $this->channel = $channel; + $this->url = $url; + $this->channel = $channel; $this->username = $username; } @@ -64,20 +65,16 @@ private function sendMessage($data) } /** - * @param array|SlackBlock $data + * @param Message $message * * @return SlackIncomingResult */ - public function send($data) + public function send($message) { - if ($data instanceof SlackBlock) { - $data = [ - 'channel' => $this->channel, - 'username' => $this->username, - 'icon_emoji' => ':rocket:', - 'blocks' => $data->build() - ]; - } + $data = array_merge([ + 'channel' => $this->channel, + 'username' => $this->username, + ], $message->toArray()); return $this->sendMessage($data); } @@ -89,40 +86,57 @@ public function send($data) */ public function test($configure) { - $slackBlock = new SlackBlock(); - $slackBlock + $message = new Message(); + $message ->addBlock( - new Header('This is a test') + new Header(new PlainText('This is a test')) ) ->addBlock( - new Image('https://picsum.photos/600/100', 'sample') + new BlockImage('https://picsum.photos/600/100', 'sample') ) ->addBlock( - Section::plain_text(get_current_user() . ' was deployed :simple_smile:') + (new Section())->setText( + new PlainText(get_current_user() . ' was deployed :simple_smile:') + ) ) ->addBlock( - Section::fields() + (new Section()) ->addField( - SectionField::markdown('*Hostname:*' . PHP_EOL . gethostname()) + new MarkdownText('*Hostname:*' . PHP_EOL . gethostname()) ) ->addField( - SectionField::markdown('*URL:*' . PHP_EOL . $configure->read('url')) + new MarkdownText('*URL:*' . PHP_EOL . $configure->read('url')) ) ) ->addBlock( new Divider() ) ->addBlock( - Section::bold('Git pull') + (new Section()) + ->addField( + new MarkdownText('*Git pull*') + ) + ) + ->addBlock( + (new Section()) + ->addField( + new MarkdownText('```HELLO WORLD```') + ) ) ->addBlock( - Section::code_block('HELLO WORLD') + new Divider() ) ->addBlock( - Section::bold('Rsync') + (new Section()) + ->addField( + new MarkdownText('*Rsync*') + ) ) ->addBlock( - Section::code_block('HELLO WORLD') + (new Section()) + ->addField( + new MarkdownText('```HELLO WORLD```') + ) ) ->addBlock( new Divider() @@ -130,21 +144,16 @@ public function test($configure) ->addBlock( (new Context()) ->addElement( - ContextElement::markdown('Date: ' . date("Y/m/d H:i:s")) + new MarkdownText('Date: ' . date("Y/m/d H:i:s")) ) ->addElement( - ContextElement::markdown('Version: ' . Main::appName() . ' ' . Main::VERSION) + new MarkdownText('Version: ' . Main::appName() . ' ' . Main::VERSION) ) ->addElement( - ContextElement::markdown('Configuration: ' . $configure->getConfigPath()) + new MarkdownText('Configuration: ' . $configure->getConfigPath()) ) ); - return $this->sendMessage([ - 'channel' => $this->channel, - 'username' => $this->username, - 'icon_emoji' => ':rocket:', - 'blocks' => $slackBlock->build() - ]); + return $this->send($message); } } diff --git a/src/Slack/BlockKit/Block/BlockInterface.php b/src/Slack/BlockKit/Block/BlockInterface.php new file mode 100644 index 0000000..7b52702 --- /dev/null +++ b/src/Slack/BlockKit/Block/BlockInterface.php @@ -0,0 +1,11 @@ +block_id = $block_id; + } + + /** + * @param BlockInterface|ElementInterface $element + * + * @return $this + */ + public function addElement($element) + { + $this->elements[] = $element; + + return $this; + } + + /** + * @return array{type: 'context', elements: list} + */ + public function toArray() + { + $array = [ + 'type' => 'context', + 'elements' => [], + ]; + + foreach ($this->elements as $element) { + $array['elements'][] = $element->toArray(); + } + + if ($this->block_id !== null) { + $array['block_id'] = $this->block_id; + } + + return $array; + } +} diff --git a/src/Slack/BlockKit/Block/Divider.php b/src/Slack/BlockKit/Block/Divider.php new file mode 100644 index 0000000..2b963d4 --- /dev/null +++ b/src/Slack/BlockKit/Block/Divider.php @@ -0,0 +1,33 @@ +block_id = $block_id; + } + + /** + * @return array{type: 'divider'} + */ + public function toArray() + { + $array = ['type' => 'divider']; + + if ($this->block_id !== null) { + $array['block_id'] = $this->block_id; + } + + return $array; + } +} diff --git a/src/Slack/BlockKit/Block/Header.php b/src/Slack/BlockKit/Block/Header.php new file mode 100644 index 0000000..6a1da94 --- /dev/null +++ b/src/Slack/BlockKit/Block/Header.php @@ -0,0 +1,46 @@ +plainText = $plainText; + $this->block_id = $block_id; + } + + /** + * @return array{type: 'header', text: array{type: 'text', text: string}, block_id: string|null} + */ + public function toArray() + { + $array = [ + 'type' => 'header', + 'text' => $this->plainText->toArray(), + ]; + + if ($this->block_id !== null) { + $array['block_id'] = $this->block_id; + } + + return $array; + } +} diff --git a/src/Slack/BlockKit/Block/Image.php b/src/Slack/BlockKit/Block/Image.php new file mode 100644 index 0000000..c24f694 --- /dev/null +++ b/src/Slack/BlockKit/Block/Image.php @@ -0,0 +1,61 @@ +url = $url; + $this->alt = $alt; + $this->title = $title; + $this->block_id = $block_id; + } + + /** + * @inheritDoc + */ + public function toArray() + { + $array = [ + 'type' => 'image', + 'image_url' => $this->url, + 'alt_text' => $this->alt, + ]; + + if ($this->title !== null) { + $array['title'] = $this->title->toArray(); + } + + if ($this->block_id !== null) { + $array['block_id'] = $this->block_id; + } + + return $array; + } +} diff --git a/src/Slack/BlockKit/Block/Section.php b/src/Slack/BlockKit/Block/Section.php new file mode 100644 index 0000000..689a47c --- /dev/null +++ b/src/Slack/BlockKit/Block/Section.php @@ -0,0 +1,100 @@ +block_id = $block_id; + } + + /** + * @param MarkdownText|PlainText $text + * + * @return Section + */ + public function setText($text) + { + $this->text = $text; + + return $this; + } + + /** + * @param PlainText|MarkdownText $field + * + * @return Section + */ + public function addField($field) + { + $this->fields[] = $field; + + return $this; + } + + /** + * @param ElementInterface|null $accessory + * + * @return Section + */ + public function setAccessory($accessory) + { + $this->accessory = $accessory; + + return $this; + } + + /** + * @inheritDoc + */ + public function toArray() + { + $array = ['type' => 'section']; + + if ($this->text !== null) { + $array['text'] = $this->text->toArray(); + } + + if ($this->fields !== null) { + $array['fields'] = []; + foreach ($this->fields as $field) { + $array['fields'][] = $field->toArray(); + } + } + + if ($this->accessory !== null) { + $array['accessory'] = $this->accessory->toArray(); + } + + if ($this->block_id !== null) { + $array['block_id'] = $this->block_id; + } + + return $array; + } +} diff --git a/src/Slack/BlockKit/Element/Button.php b/src/Slack/BlockKit/Element/Button.php new file mode 100644 index 0000000..73e0819 --- /dev/null +++ b/src/Slack/BlockKit/Element/Button.php @@ -0,0 +1,111 @@ +text = $text; + $this->action_id = $action_id; + } + + /** + * @param string|null $url + * + * @return $this + */ + public function setUrl($url) + { + $this->url = $url; + + return $this; + } + + /** + * @param string|null $value + * + * @return $this + */ + public function setValue($value) + { + $this->value = $value; + + return $this; + } + + /** + * @return $this + */ + public function setStyleDefault() + { + $this->style = null; + + return $this; + } + + /** + * @return $this + */ + public function setStylePrimary() + { + $this->style = 'primary'; + + return $this; + } + + /** + * @return $this + */ + public function setStyleDanger() + { + $this->style = 'danger'; + + return $this; + } + + /** + * @inheritDoc + */ + public function toArray() + { + $array = [ + 'type' => 'button', + 'text' => $this->text->toArray(), + 'action_id' => $this->action_id, + ]; + + if ($this->style !== null) { + $array['style'] = $this->style; + } + + if ($this->url !== null) { + $array['url'] = $this->url; + } + + if ($this->value !== null) { + $array['value'] = $this->value; + } + + return $array; + } +} diff --git a/src/Slack/BlockKit/Element/ElementInterface.php b/src/Slack/BlockKit/Element/ElementInterface.php new file mode 100644 index 0000000..bdc06ea --- /dev/null +++ b/src/Slack/BlockKit/Element/ElementInterface.php @@ -0,0 +1,11 @@ + 'image', 'image_url' => $this->url, - 'alt_text' => $this->alt + 'alt_text' => $this->alt, ]; } } diff --git a/src/Slack/BlockKit/Element/MarkdownText.php b/src/Slack/BlockKit/Element/MarkdownText.php new file mode 100644 index 0000000..a257403 --- /dev/null +++ b/src/Slack/BlockKit/Element/MarkdownText.php @@ -0,0 +1,39 @@ +text = $text; + $this->verbatim = $verbatim; + } + + /** + * @inheritDoc + */ + public function toArray() + { + $array = [ + 'type' => 'mrkdwn', + 'text' => $this->text, + ]; + + if ($this->verbatim) { + $array['verbatim'] = true; + } + + return $array; + } +} diff --git a/src/Slack/BlockKit/Element/PlainText.php b/src/Slack/BlockKit/Element/PlainText.php new file mode 100644 index 0000000..421991f --- /dev/null +++ b/src/Slack/BlockKit/Element/PlainText.php @@ -0,0 +1,39 @@ +text = $text; + $this->emoji = $emoji; + } + + /** + * @inheritDoc + */ + public function toArray() + { + $array = [ + 'type' => 'plain_text', + 'text' => $this->text, + ]; + + if ($this->emoji) { + $array['emoji'] = true; + } + + return $array; + } +} diff --git a/src/Slack/BlockKit/Message.php b/src/Slack/BlockKit/Message.php new file mode 100644 index 0000000..742fb73 --- /dev/null +++ b/src/Slack/BlockKit/Message.php @@ -0,0 +1,86 @@ +text = $text; + $this->icon_emoji = $icon_emoji; + } + + /** + * @return string|null + */ + public function getIconEmoji() + { + return $this->icon_emoji; + } + + /** + * @return string|null + */ + public function getText() + { + return $this->text; + } + + /** + * @param BlockInterface $block + * + * @return $this + */ + public function addBlock(BlockInterface $block) + { + $this->blocks[] = $block; + + return $this; + } + + /** + * @return array + */ + private function blockArray() + { + $array = []; + foreach ($this->blocks as $block) { + $array[] = $block->toArray(); + } + + return $array; + } + + /** + * @return array + */ + public function toArray() + { + $array = ['blocks' => $this->blockArray()]; + + if ($this->text !== null) { + $array['text'] = $this->text; + } + + if ($this->icon_emoji !== null) { + $array['icon_emoji'] = $this->icon_emoji; + } + + return $array; + } +} diff --git a/src/SlackBlock.php b/src/SlackBlock.php deleted file mode 100644 index ba4ff9d..0000000 --- a/src/SlackBlock.php +++ /dev/null @@ -1,36 +0,0 @@ -blocks[] = $block; - - return $this; - } - - /** - * @return array - */ - public function build() - { - $array = []; - foreach ($this->blocks as $block) { - $array[] = $block->build(); - } - - return $array; - } - -} diff --git a/src/SlackBlock/Context.php b/src/SlackBlock/Context.php deleted file mode 100644 index 013c0d3..0000000 --- a/src/SlackBlock/Context.php +++ /dev/null @@ -1,39 +0,0 @@ -elements[] = $element; - - return $this; - } - - /** - * @return array - */ - public function build() - { - $array = []; - foreach ($this->elements as $element) { - $array[] = $element->build(); - } - - return [ - 'type' => 'context', - 'elements' => $array - ]; - } -} diff --git a/src/SlackBlock/ContextElement.php b/src/SlackBlock/ContextElement.php deleted file mode 100644 index 4bf4e06..0000000 --- a/src/SlackBlock/ContextElement.php +++ /dev/null @@ -1,68 +0,0 @@ -type = $type; - $this->text = $text; - } - - /** - * @param string $type - * @param string $text - * - * @return ContextElement - */ - private static function text($type, $text) - { - return new static($type, $text); - } - - /** - * @param string $text - * - * @return ContextElement - */ - public static function plain_text($text) - { - return self::text('plain_text', $text); - } - - /** - * @param string $text - * - * @return ContextElement - */ - public static function markdown($text) - { - return self::text('mrkdwn', $text); - } - - /** - * @return array{type: string, text: string} - */ - public function build() - { - return [ - 'type' => $this->type, - 'text' => $this->text - ]; - } -} diff --git a/src/SlackBlock/Divider.php b/src/SlackBlock/Divider.php deleted file mode 100644 index 5b3edc7..0000000 --- a/src/SlackBlock/Divider.php +++ /dev/null @@ -1,18 +0,0 @@ - 'divider' - ]; - } -} diff --git a/src/SlackBlock/Header.php b/src/SlackBlock/Header.php deleted file mode 100644 index 9d343e3..0000000 --- a/src/SlackBlock/Header.php +++ /dev/null @@ -1,36 +0,0 @@ -text = $text; - } - - /** - * @return array{type: string, text: array{type: string, text: string, emoji: bool}} - */ - public function build() - { - return [ - 'type' => 'header', - 'text' => [ - 'type' => 'plain_text', - 'text' => $this->text, - 'emoji' => true - ] - ]; - } -} diff --git a/src/SlackBlock/Section.php b/src/SlackBlock/Section.php deleted file mode 100644 index a4ead28..0000000 --- a/src/SlackBlock/Section.php +++ /dev/null @@ -1,133 +0,0 @@ -section_type = 'text'; - $instance->type = $type; - $instance->text = $text; - - return $instance; - } - - /** - * @param string $text - * - * @return Section - */ - public static function plain_text($text) - { - return self::text('plain_text', $text); - } - - /** - * @param string $text - * - * @return Section - */ - public static function markdown($text) - { - return self::text('mrkdwn', $text); - } - - /** - * @param string $text - * - * @return Section - */ - public static function bold($text) - { - return self::text('mrkdwn', '*' . $text . '*'); - } - - /** - * @param string $text - * - * @return Section - */ - public static function code_block($text) - { - return self::text('mrkdwn', '```' . $text . '```'); - } - - /** - * @return Section - */ - public static function fields() - { - $instance = new static(); - $instance->section_type = 'fields'; - - return $instance; - } - - /** - * @param SectionField $field - * - * @return $this - * @throws Exception - */ - public function addField($field) - { - if ($this->section_type !== 'fields') { - throw new Exception(''); - } - - $this->fields[] = $field; - - return $this; - } - - /** - * @return array - */ - public function build() - { - if ($this->section_type === 'text') { - return [ - 'type' => 'section', - 'text' => [ - 'type' => $this->type, - 'text' => $this->text - ] - ]; - } - - if ($this->section_type === 'fields') { - $array = []; - foreach ($this->fields as $field) { - $array[] = $field->build(); - } - - return [ - 'type' => 'section', - 'fields' => $array - ]; - } - } -} diff --git a/src/SlackBlock/SectionField.php b/src/SlackBlock/SectionField.php deleted file mode 100644 index b4ecfaf..0000000 --- a/src/SlackBlock/SectionField.php +++ /dev/null @@ -1,68 +0,0 @@ -type = $type; - $this->text = $text; - } - - /** - * @param string $type - * @param string $text - * - * @return SectionField - */ - private static function text($type, $text) - { - return new static($type, $text); - } - - /** - * @param string $text - * - * @return SectionField - */ - public static function plain_text($text) - { - return self::text('plain_text', $text); - } - - /** - * @param string $text - * - * @return SectionField - */ - public static function markdown($text) - { - return self::text('mrkdwn', $text); - } - - /** - * @return array - */ - public function build() - { - return [ - 'type' => $this->type, - 'text' => $this->text - ]; - } -} diff --git a/src/SlackBlockInterface.php b/src/SlackBlockInterface.php deleted file mode 100644 index b9b0f19..0000000 --- a/src/SlackBlockInterface.php +++ /dev/null @@ -1,11 +0,0 @@ - Date: Sat, 3 Apr 2021 10:10:19 +0900 Subject: [PATCH 2/6] feat: support --info option --- src/Main.php | 106 ++++++++++++++++++++++++++++-------------------- src/Options.php | 13 +++++- 2 files changed, 72 insertions(+), 47 deletions(-) diff --git a/src/Main.php b/src/Main.php index c6cf949..50d11f4 100644 --- a/src/Main.php +++ b/src/Main.php @@ -35,6 +35,13 @@ public function run() return; } + // INFO + if ($this->options->hasInfo()) { + $this->printInfo(); + + return; + } + // HELP if ($this->options->hasHelp()) { $this->printHelp(); @@ -65,9 +72,9 @@ public function run() if ($this->options->hasVerify()) { if (Configure::verify($config_path)) { - $this->printInfo($config_path . ': OK'); + $this->info($config_path . ': OK'); } else { - $this->printError($config_path . ': NG'); + $this->error($config_path . ': NG'); } return; @@ -78,7 +85,7 @@ public function run() // USER CHECK if (posix_getpwuid(posix_geteuid())['name'] !== $configure->read('user')) { - $this->printError('can not executed user.'); + $this->error('can not executed user.'); return; } @@ -92,7 +99,7 @@ public function run() ); $slackResult = $slack->test($configure); if (! $slackResult->isOk()) { - $this->printError($slackResult->getError()); + $this->error($slackResult->getError()); } return; @@ -119,16 +126,16 @@ public function run() ->setSubscribeEvent(CommandEvents::BEFORE_EXECUTION, function ($command) use ($self) { /** @var Command $command */ if ($self->options->hasDebug()) { - $self->printDebug('$ ' . $command->string()); + $self->debug('$ ' . $command->string()); } }) ->execute(); if ($command->isSuccess()) { - $this->printInfo('> ' . $command->string()); - $this->printInfo($command->getOutputString()); + $this->info('> ' . $command->string()); + $this->info($command->getOutputString()); } else { - $this->printInfo('> ' . $command->string()); - $this->printError($command->getOutputString()); + $this->info('> ' . $command->string()); + $this->error($command->getOutputString()); return; } @@ -147,17 +154,17 @@ public function run() ->setSubscribeEvent(CommandEvents::BEFORE_EXECUTION, function ($command) use ($self) { /** @var Command $command */ if ($self->options->hasDebug()) { - $self->printDebug('$ ' . $command->string()); + $self->debug('$ ' . $command->string()); } }) ->execute(); if ($command->isSuccess()) { - $this->printInfo('> ' . $command->string()); - $this->printInfo($command->getOutputString()); + $this->info('> ' . $command->string()); + $this->info($command->getOutputString()); } else { - $this->printError('> ' . $command->string()); - $this->printError($command->getOutputString()); + $this->error('> ' . $command->string()); + $this->error($command->getOutputString()); return; } @@ -185,7 +192,7 @@ public function run() ->setSubscribeEvent(CommandEvents::BEFORE_EXECUTION, function ($command) use ($self) { /** @var Command $command */ if ($self->options->hasDebug()) { - $self->printDebug('$ ' . $command->string()); + $self->debug('$ ' . $command->string()); } }); @@ -201,11 +208,11 @@ public function run() ->addArgument('--dry-run') ->execute(); if ($command->isSuccess()) { - $this->printInfo('> rsync dry'); - $this->printInfo($command->getOutputString()); + $this->info('> rsync dry'); + $this->info($command->getOutputString()); } else { - $this->printError('> rsync'); - $this->printError($command->getOutputString()); + $this->error('> rsync'); + $this->error($command->getOutputString()); } break; @@ -214,12 +221,12 @@ public function run() ->execute(); if ($command->isSuccess()) { $sync_log .= $command->getOutputString(); - $this->printInfo('> rsync'); - $this->printInfo($command->getOutputString()); + $this->info('> rsync'); + $this->info($command->getOutputString()); } else { $error = true; - $this->printError('> rsync'); - $this->printError($command->getOutputString()); + $this->error('> rsync'); + $this->error($command->getOutputString()); } break; @@ -231,24 +238,24 @@ public function run() ->execute(); if ($command->isSuccess()) { $sync_log .= $command->getOutputString(); - $this->printInfo('> rsync dry'); - $this->printInfo($command->getOutputString()); + $this->info('> rsync dry'); + $this->info($command->getOutputString()); } else { $error = true; - $this->printError('> rsync'); - $this->printError($command->getOutputString()); + $this->error('> rsync'); + $this->error($command->getOutputString()); } echo 'Do you want to synchronize? [y/N]' . PHP_EOL; if (trim(fgets(STDIN)) === 'y') { $_command->execute(); if ($_command->isSuccess()) { - $this->printInfo('> rsync'); - $this->printInfo($_command->getOutputString()); + $this->info('> rsync'); + $this->info($_command->getOutputString()); } else { $error = true; - $this->printError('> rsync'); - $this->printError($_command->getOutputString()); + $this->error('> rsync'); + $this->error($_command->getOutputString()); } } @@ -277,15 +284,15 @@ public function run() ->setSubscribeEvent(CommandEvents::BEFORE_EXECUTION, function ($command) use ($self) { /** @var Command $command */ if ($self->options->hasDebug()) { - $self->printDebug('$ ' . $command->string()); + $self->debug('$ ' . $command->string()); } }) ->execute(); if ($command->isSuccess()) { - $this->printInfo($command->path() . ': ' . $command->getOutputString()); + $this->info($command->path() . ': ' . $command->getOutputString()); } else { - $this->printError($command->getOutputString()); + $this->error($command->getOutputString()); } } } @@ -385,7 +392,7 @@ public function run() ); $slackResult = $slack->send($message); if (! $slackResult->isOk()) { - $this->printError($slackResult->getError()); + $this->error($slackResult->getError()); } } } @@ -408,7 +415,7 @@ private function upgrade() $working_directory_path = sys_get_temp_dir() . '/' . 'rocket-' . substr(str_shuffle('1234567890abcdefghijklmnopqrstuvwxyz'), 0, 8); if (! mkdir($working_directory_path) && ! is_dir($working_directory_path)) { - $this->printError(sprintf('Directory "%s" was not created.', $working_directory_path)); + $this->error(sprintf('Directory "%s" was not created.', $working_directory_path)); return; } @@ -420,12 +427,21 @@ private function upgrade() $updater = new Updater($working_directory_path); $result = $updater->upgrade(); if (! $result->isOk()) { - $this->printError($result->getError()); + $this->error($result->getError()); return; } - $this->printInfo('New version: ' . $result->getFilePath()); + $this->info('New version: ' . $result->getFilePath()); + } + + private function printInfo() + { + echo 'OS: ' . php_uname() . PHP_EOL; + echo 'PHP binary: ' . PHP_BINARY . PHP_EOL; + echo 'PHP version: ' . PHP_VERSION . PHP_EOL; + echo 'php.ini used: ' . php_ini_loaded_file() . PHP_EOL; + echo 'rocket version: ' . self::VERSION . PHP_EOL; } private function printHelp() @@ -476,12 +492,12 @@ private function printConfig($template) private function printUsage() { - $this->printWarning('Usage: ./rocket.phar --config ./rocket.json --git [pull] --sync [dry|confirm|force]'); + $this->warning('Usage: ./rocket.phar --config ./rocket.json --git [pull] --sync [dry|confirm|force]'); } private function printInit() { - $this->printWarning('Usage: ./rocket.phar --init > ./rocket.json'); + $this->warning('Usage: ./rocket.phar --init > ./rocket.json'); } /** @@ -489,13 +505,13 @@ private function printInit() */ private function printNotfoundPath($path) { - $this->printError($path . ': No such file or directory'); + $this->error($path . ': No such file or directory'); } /** * @param string $reason */ - private function printError($reason) + private function error($reason) { $string = self::appName() . ': ' . $reason; if ($this->options->hasNoColor()) { @@ -508,7 +524,7 @@ private function printError($reason) /** * @param string $text */ - private function printWarning($text) + private function warning($text) { if ($this->options->hasNoColor()) { echo $text . PHP_EOL; @@ -520,7 +536,7 @@ private function printWarning($text) /** * @param string $text */ - private function printInfo($text) + private function info($text) { if ($this->options->hasNoColor()) { echo $text . PHP_EOL; @@ -532,7 +548,7 @@ private function printInfo($text) /** * @param string $text */ - private function printDebug($text) + private function debug($text) { if ($this->options->hasNoColor()) { echo $text . PHP_EOL; diff --git a/src/Options.php b/src/Options.php index 02f4cc2..8639cfa 100644 --- a/src/Options.php +++ b/src/Options.php @@ -19,12 +19,13 @@ public function __construct() 'init:', 'unzip:', + 'debug', 'help', + 'info', 'notify-test', + 'no-color', 'upgrade', 'verify', - 'debug', - 'no-color' ]); $this->options = $options; @@ -67,6 +68,14 @@ public function hasHelp() return $this->has('help') || $this->has('h'); } + /** + * @return bool + */ + public function hasInfo() + { + return $this->has('info'); + } + /** * @return bool */ From 0d3e94835d0a27583444ccaf746427c33d43215c Mon Sep 17 00:00:00 2001 From: Kouhei Sano Date: Sat, 3 Apr 2021 10:27:47 +0900 Subject: [PATCH 3/6] feat: support --notify option --- README.md | 3 +++ src/Main.php | 31 +++++++++++++++++++++++++++++++ src/Options.php | 11 ++++++++++- 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d522037..f70dc48 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,9 @@ chmod u+x rocket.phar # not git ./rocket.phar --config ./rocket.json --sync confirm + +# notify +cat example.txt | ./rocket.phar --config ./rocket.json --notify ``` ## Build phar file diff --git a/src/Main.php b/src/Main.php index 50d11f4..5127523 100644 --- a/src/Main.php +++ b/src/Main.php @@ -9,6 +9,7 @@ use Rocket\Slack\BlockKit\Block\Section as SlackSection; use Rocket\Slack\BlockKit\Element\MarkdownText as SlackMarkdownText; use Rocket\Slack\BlockKit\Element\PlainText as SlackPlainText; +use Rocket\Slack\BlockKit\Message; use Rocket\Slack\BlockKit\Message as SlackMessage; class Main @@ -83,6 +84,36 @@ public function run() $configure = new Configure($config_path); $self = $this; + // NOTIFICATION + if ($this->options->hasNotify()) { + $lines = []; + while ($line = fgets(STDIN)) { + $lines[] = trim($line); + } + + $message = new Message(); + + $chunks = str_split(implode(PHP_EOL, $lines), SlackSection::TEXT_MAX_LENGTH - 6); + foreach ($chunks as $chunk) { + $message + ->addBlock( + (new SlackSection()) + ->addField( + new SlackMarkdownText('```' . $chunk . '```') + ) + ); + } + + $slack = new Slack( + $configure->read('slack.incomingWebhook'), + $configure->read('slack.channel'), + $configure->read('slack.username') + ); + $slack->send($message); + + return; + } + // USER CHECK if (posix_getpwuid(posix_geteuid())['name'] !== $configure->read('user')) { $this->error('can not executed user.'); diff --git a/src/Options.php b/src/Options.php index 8639cfa..519c193 100644 --- a/src/Options.php +++ b/src/Options.php @@ -22,6 +22,7 @@ public function __construct() 'debug', 'help', 'info', + 'notify', 'notify-test', 'no-color', 'upgrade', @@ -76,12 +77,20 @@ public function hasInfo() return $this->has('info'); } + /** + * @return bool + */ + public function hasNotify() + { + return $this->has('notify') || $this->has('n'); + } + /** * @return bool */ public function hasNotifyTest() { - return $this->has('notify-test') || $this->has('n'); + return $this->has('notify-test'); } /** From ed7d7ccd0476b1c7042525530f3fde0918fd0250 Mon Sep 17 00:00:00 2001 From: Kouhei Sano Date: Sat, 3 Apr 2021 10:31:16 +0900 Subject: [PATCH 4/6] feat: support --notify option --- src/Main.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Main.php b/src/Main.php index 5127523..3643ac5 100644 --- a/src/Main.php +++ b/src/Main.php @@ -487,7 +487,8 @@ private function printHelp() echo ' -g, --git [pull] Git operation' . PHP_EOL; echo ' -h, --help Display this help message' . PHP_EOL; echo ' -i, --init [plain|cakephp3|eccube4|wordpress] Print sample configuration file' . PHP_EOL; - echo ' -n, --notify-test Slack notification test' . PHP_EOL; + echo ' -n, --notify Simple slack notification' . PHP_EOL; + echo ' --notify-test Slack notification test' . PHP_EOL; echo ' --no-color Without color' . PHP_EOL; echo ' -s, --sync [dry|confirm|force] Rsync operation' . PHP_EOL; echo ' -u, --upgrade Download new version file' . PHP_EOL; From 80a7672c06e8a67f86dc072ad8e6464ec688a813 Mon Sep 17 00:00:00 2001 From: Kouhei Sano Date: Sat, 3 Apr 2021 10:36:14 +0900 Subject: [PATCH 5/6] refactor: rsync log --- src/Main.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Main.php b/src/Main.php index 3643ac5..4f3cad7 100644 --- a/src/Main.php +++ b/src/Main.php @@ -268,7 +268,7 @@ public function run() ->addArgument('--dry-run') ->execute(); if ($command->isSuccess()) { - $sync_log .= $command->getOutputString(); + //$sync_log .= $command->getOutputString(); $this->info('> rsync dry'); $this->info($command->getOutputString()); } else { @@ -281,6 +281,7 @@ public function run() if (trim(fgets(STDIN)) === 'y') { $_command->execute(); if ($_command->isSuccess()) { + $sync_log .= $_command->getOutputString(); $this->info('> rsync'); $this->info($_command->getOutputString()); } else { From 0111313f5a3938440d83252a4e400431710835e5 Mon Sep 17 00:00:00 2001 From: Kouhei Sano Date: Sat, 3 Apr 2021 10:37:22 +0900 Subject: [PATCH 6/6] chore: update VERSION --- src/Main.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Main.php b/src/Main.php index 4f3cad7..6e757d5 100644 --- a/src/Main.php +++ b/src/Main.php @@ -14,7 +14,7 @@ class Main { - const VERSION = '0.1.7'; + const VERSION = '0.1.8'; /** @var Options */ private $options = null;