From 2381556fa438968e293e4eed06090790144d0cc7 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Wed, 11 May 2011 14:22:45 +0300 Subject: fixes, format option for gsxcl --- gsxcl | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 87 insertions(+), 17 deletions(-) (limited to 'gsxcl') diff --git a/gsxcl b/gsxcl index 7c09ecb..2db4d71 100755 --- a/gsxcl +++ b/gsxcl @@ -6,9 +6,15 @@ * A test package and command line client to the GSX library * @package gsxlib * @author Filipp Lepalaan + * @license + * This program is free software. It comes without any warranty, to + * the extent permitted by applicable law. You can redistribute it + * and/or modify it under the terms of the Do What The Fuck You Want + * To Public License, Version 2, as published by Sam Hocevar. See + * http://sam.zoy.org/wtfpl/COPYING for more details. */ -if (FALSE) { +if (TRUE) { error_reporting(E_ALL|E_STRICT); } @@ -20,7 +26,7 @@ require 'gsxlib.php'; if (count($argv) < 6) { echo <<warrantyStatus($opts['q'])); + $result = $gsx->warrantyStatus($query); break; case 'parts': - print_r($gsx->partsLookup($opts['q'])); + $result = $gsx->partsLookup($query); break; case 'pending': - print_r($gsx->partsPendingReturn($opts['q'])); + $result = $gsx->partsPendingReturn($query); break; case 'repair': - print_r($gsx->repairDetails($opts['q'])); + $result = $gsx->repairDetails($query); break; case 'lookup': - print_r($gsx->repairLookup($opts['q'])); + $result = $gsx->repairLookup($query); break; case 'status': - print_r($gsx->repairStatus($opts['q'])); + $result = $gsx->repairStatus($query); break; case 'comptia': - print_r($gsx->compTiaCodes()); + $result = $gsx->compTiaCodes(); break; case 'label': - list($order, $part) = explode(':', $opts['q']); + list($order, $part) = explode(':', $query); $result = $gsx->returnLabel($order, $part); $name = $result->returnLabelFileName; echo $result->returnLabelFileData; break; +} + +switch ($format) +{ + case 'json': + echo json_encode($result); + break; + case 'xml': + $xml = simplexml_load_string(''); + 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); + } + } + } + echo $xml->asXML(); + break; + case 'csv': + $i = 0; + $fo = fopen('php://stdout', 'w'); + + foreach ($result as $k => $v) + { + if (is_object($v)) + { + $keys = array(); + $vals = array(); + 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, $vals); + } else { + $keys[] = $k; + $vals[] = $v; + } + $i++; + } + + if (count($result) === 1) { + fputcsv($fo, $keys); + fputcsv($fo, $vals); + } + + fclose($fo); + break; default: - print_r($gsx->warrantyStatus($opts['q'])); + print_r($result); break; } -- cgit v1.2.3