Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CakeDC repo downloader, and updater. For the truly lazy.
#!/usr/bin/php
<?php
if ($argc <= 1) {
$account = 'CakeDC';
} else {
$account = $argv[1];
}
if ($argc <= 2) {
$type = 'all';
} else {
$types = array(
'all',
'app', 'apps',
'plugin', 'plugins',
);
$type = $argv[2];
if (!in_array($type, $types)) {
die("Invalid repo type specified: '${type}' \n");
}
if ($type[strlen($type) - 1] === 's') {
$type = substr($type, 0, -1);
}
}
echo "Login to Github\n";
echo "---------------\n";
echo "Username: ";
$username = trim(fgets(STDIN));
`stty -echo`;
echo "Password: ";
$password = trim(fgets(STDIN));
`stty echo`;
echo "\n\n";
$ch = curl_init();
$options = array(
CURLOPT_URL => 'http://github.com/api/v2/json/repos/show/' . $account,
CURLOPT_RETURNTRANSFER => true,
);
if (!empty($username)) {
$options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
$options[CURLOPT_USERPWD] = $username . ':' . $password;
}
echo "Fetching data from Github, please wait...\n";
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
if (!($data = json_decode($result, true))) {
echo "Failed to parse response.\n";
die(1);
}
foreach ($data['repositories'] as $repo) {
$name = $repo['name'];
if (
$type === 'all' ||
($type === 'app' && preg_match('/^[A-Z].*$/', $name)) ||
($type === 'plugin' && preg_match('/^[a-z].*$/', $name))) {
if (is_dir($name)) {
echo "Updating repository: $name [git --git-dir=$name/.git remote update]\n";
`git --git-dir=$name/.git remote update`;
} else {
echo "Cloning repository: $name [git clone git@github.com:$account/$name.git]\n";
`git clone git@github.com:$account/$name.git`;
}
}
}
echo "\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment