diff options
Diffstat (limited to 'gsxcl')
-rwxr-xr-x | gsxcl | 177 |
1 files changed, 107 insertions, 70 deletions
@@ -14,19 +14,22 @@ * http://sam.zoy.org/wtfpl/COPYING for more details. */ -if (FALSE) { - error_reporting(E_ALL|E_STRICT); +if (TRUE) { + error_reporting( E_ALL|E_STRICT ); } -$modes = array( 'warranty', 'parts', 'pending', 'repair', 'lookup', 'status', 'label', 'model', 'osdispatchdetail' ); -$modes_str = implode(', ', $modes); +$verbs = array( 'create', 'lookup', 'update', 'status', 'label', 'pending', 'details' ); +$nouns = array( 'repair', 'part', 'dispatch', 'order', 'return', 'warranty' ); + +$nouns_str = implode( ', ', $nouns ); +$verbs_str = implode( ', ', $verbs ); require 'gsxlib.php'; -if (count($argv) < 6) { +if( count( $argv ) < 6 ) { echo <<<EOT -usage: gsxcl -s sold-to -u username -p password [-r region] [-e environment] [-f format] [-m mode] [-q query] +usage: gsxcl -s sold-to -u username -p password [-r region] [-e environment] [-f format] verb noun -s sold-to your GSX Sold-To account -u username the Apple ID with GSX WS API access -p password the password for the Apple ID @@ -35,68 +38,91 @@ usage: gsxcl -s sold-to -u username -p password [-r region] [-e environment] [-f -e environment the GSX environment. Either empty (production), "it" or "ut" Defaults to production -f format the output format. Either print_r (default), json, xml or csv - -m mode what to search for. Currently one of: {$modes_str}. - Defaults to "warranty" - -q query a query string (serial number, order confirmation, repair number, EEE code, etc + -d data data for the query (serial number, order confirmation, repair number, EEE code, etc Defaults to this machine's serial number + verb one of: {$nouns_str} + noun one of: {$verbs_str} EOT; exit(); } -$opts = getopt('s:u:p:i::r:e:m:q:f:'); - -$region = (isset($opts['r'])) ? $opts['r'] : 'emea'; -$mode = (isset($opts['m'])) ? $opts['m'] : 'warranty'; -$format = (isset($opts['f'])) ? $opts['f'] : 'print_r'; -$environment = (isset($opts['e'])) ? $opts['e'] : null; +$opts = getopt( 's:u:p:r:e:m:q:f:d:' ); +list( $verb, $noun ) = array_slice( $argv, -2, 2 ); -$gsx = new GsxLib($opts['s'], $opts['u'], $opts['p'], $environment, $region); +if( !in_array( $verb, $verbs )) { + exit( "Error: invalid verb - {$verb}.\n" ); +} -if (!isset($opts['q'])) { - $query = `/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial Number/ {print $4}'`; -} else { - $query = $opts['q']; +if( !in_array( $noun, $nouns )) { + exit( "Error: invalid noun - {$noun}.\n" ); } -if (isset($opts['i'])) +$region = ( isset( $opts['r'] )) ? $opts['r'] : 'emea'; +$format = ( isset($opts['f'] )) ? $opts['f'] : 'print_r'; +$environment = ( isset( $opts['e'] )) ? $opts['e'] : null; + +switch( $noun ) { - if (!function_exists('readline')) { - exit("Sorry, but your PHP lacks readline support\n"); - } - - echo "Entering interactive mode\n"; - - while ($line != 'quit') - { - $line = readline('gsx: '); - readline_add_history($line); - } + case 'warranty': + $valid_verbs = array( 'status' ); + if( !in_array( $verb, $valid_verbs )) { + printf( "Error: verb should be one of - %s\n", implode( ',', $valid_verbs )); + exit(); + } } -switch ($mode) -{ +$gsx = new GsxLib( $opts['s'], $opts['u'], $opts['p'], $environment, $region ); + +if( !isset( $opts['d'] )) { + $data = `/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial Number/ {print $4}'`; + $data = "serialNumber={$data}"; +} else { + $data = $opts['d']; +} + +@list( $k, $v ) = explode( '=', $data ); +$data = ($k) ? array( $k => $v ) : $data; + +switch( $noun ) { + case 'warranty': - $result = $gsx->warrantyStatus($query); - break; - case 'parts': - $result = $gsx->partsLookup($query); - break; - case 'pending': - $result = $gsx->partsPendingReturn($query); - break; + switch( $verb ) { + case 'status': + $result = $gsx->warrantyStatus( $data ); + break; + } + + break; + + case 'part': + switch( $verb ) { + case 'lookup': + $result = $gsx->partsLookup( $data ); + break; + case 'pending': + $result = $gsx->partsPendingReturn( $data ); + break; + case 'details': + $result = $gsx->partsPendingReturn( $data ); + break; + } + + break; + case 'repair': - $result = $gsx->repairDetails($query); - break; - case 'lookup': - $result = $gsx->repairLookup($query); - break; - case 'status': - $result = $gsx->repairStatus($query); - break; - case 'comptia': - $result = $gsx->compTiaCodes(); - break; + switch( $verb ) { + case 'lookup': + $result = $gsx->repairLookup( $data ); + break; + case 'details': + $result = $gsx->partsPendingReturn( $data ); + break; + case 'status': + $result = $gsx->repairStatus( $query ); + break; + } + case 'model': $result = $gsx->productModel( $query ); break; @@ -104,55 +130,66 @@ switch ($mode) $result = $gsx->onsiteDispatchDetail( $query ); break; case 'label': - list($order, $part) = explode(':', $query); - $result = $gsx->returnLabel($order, $part); + list($order, $part) = explode( ':', $query ); + $result = $gsx->returnLabel( $order, $part ); $name = $result->returnLabelFileName; echo $result->returnLabelFileData; break; } -switch ($format) +switch( $format ) { case 'json': - echo json_encode($result); + echo json_encode( $result ); break; + case 'xml': - $xml = simplexml_load_string('<?xml version="1.0" encoding="utf-8"?><gsxResult />'); + if( !function_exists( 'simplexml_load_string' )) { + exit( "Error: your PHP lacks SimpleXML support!\n" ); + } + + $xml = simplexml_load_string( '<?xml version="1.0" encoding="utf-8"?><gsxResult />' ); + foreach ($result as $k => $v) { - $key = (is_numeric($k)) ? 'item' : $k; - $value = (is_object($v)) ? null : $v; - $item = $xml->addChild($key, $value); - if (is_object($v)) { - foreach ($v as $vk => $vv) { - $item->addChild($vk, $vv); + $key = (is_numeric( $k )) ? 'item' : $k; + $value = (is_object( $v )) ? null : $v; + $item = $xml->addChild( $key, $value ); + if( is_object( $v )) { + foreach( $v as $vk => $vv ) { + $item->addChild( $vk, $vv ); } } } + echo $xml->asXML(); break; + case 'csv': $i = 0; $fo = fopen('php://stdout', 'w'); - foreach ($result as $k => $v) + foreach( $result as $k => $v ) { - if (is_object($v)) + if( is_object( $v )) { $keys = array(); $vals = array(); - foreach ($v as $vk => $vv) - { + + foreach ($v as $vk => $vv) { if ($i == 0) { $keys[] = $vk; } $vals[] = $vv; } + // treat field names of first item as header row if ($i == 0) { - fputcsv($fo, $keys); + fputcsv( $fo, $keys ); } - fputcsv($fo, $vals); + + fputcsv( $fo, $vals ); + } else { $keys[] = $k; $vals[] = $v; |