-
-
Save romainneutron/04484346e88e97fec301328cc26ccbdc to your computer and use it in GitHub Desktop.
Slack delete all your files (rewrite of https://gist.github.com/jamescmartinez/909401b19c0f779fc9c1)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
<<<CONFIG | |
packages: | |
- "kriswallsmith/buzz: ^0.15.0" | |
- "symfony/console: ^3.2@dev" | |
CONFIG; | |
// Find you token on https://api.slack.com/docs/oauth-test-tokens | |
use Buzz\Message\Response; | |
use Symfony\Component\Console\Application; | |
use Symfony\Component\Console\Helper\ProgressBar; | |
use Symfony\Component\Console\Input\InputArgument; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Question\ConfirmationQuestion; | |
(new Application()) | |
->register('delete-files') | |
->addArgument('token', InputArgument::REQUIRED) | |
->setCode(function(InputInterface $input, OutputInterface $output) { | |
$token = $input->getArgument('token'); | |
$validateResponse = function( $response) { | |
$responseDecoded = json_decode($response->getContent(), true); | |
if (!$responseDecoded) { | |
throw new \Exception('Something goes wrong. Unable to decode the response.'); | |
} | |
if (!$responseDecoded['ok']) { | |
throw new \Exception('Something goes wrong. Error: '.$responseDecoded['error']); | |
} | |
return $responseDecoded; | |
}; | |
$client = new Buzz\Browser(); | |
// get "all" files | |
$response = $client->get('https://slack.com/api/files.list?'.http_build_query([ | |
'token' => $token, | |
'count' => 1000, | |
'ts_to' => (new \DateTime('-5 months'))->format('U'), | |
])); | |
$responseDecoded = $validateResponse($response); | |
$total = $responseDecoded['paging']['total']; | |
// Validate | |
$q = new ConfirmationQuestion(sprintf('You are going to delete %d files, are you sure? [Y/n]', $total)); | |
if (!$this->getHelper('question')->ask($input, $output, $q)) { | |
$output->writeln('Abort the mission'); | |
return; | |
} | |
// delete | |
$bar = new ProgressBar($output, $total); | |
foreach ($responseDecoded['files'] as $file) { | |
$response = $client->get('https://slack.com/api/files.delete?'.http_build_query([ | |
'token' => $token, | |
'file' => $file['id'], | |
])); | |
$validateResponse($response); | |
$bar->advance(); | |
} | |
$output->writeln('Done'); | |
}) | |
->getApplication() | |
->setDefaultCommand('delete-files', true) | |
->run() | |
; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment