Skip to content

Instantly share code, notes, and snippets.

@predominant
Created December 22, 2010 23:13
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save predominant/752268 to your computer and use it in GitHub Desktop.
Save predominant/752268 to your computer and use it in GitHub Desktop.
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