forked from darkninja13/gsxlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsxcl
executable file
·215 lines (179 loc) · 5.48 KB
/
gsxcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#!/usr/bin/env php
<?php
/**
* gsxlib/gsxcl
* A test package and command line client to the GSX library
* @package gsxlib
* @author Filipp Lepalaan <[email protected]>
* @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 (TRUE) {
error_reporting( E_ALL|E_STRICT );
}
$nouns = array( 'repair', 'part', 'dispatch', 'order', 'return', 'warranty' );
$verbs = array( 'create', 'lookup', 'update', 'status', 'label', 'pending', 'details' );
$nouns_str = implode( ', ', $nouns );
$verbs_str = implode( ', ', $verbs );
require 'gsxlib.php';
if( count( $argv ) < 6 ) {
echo <<<EOT
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
-r region either "am" (America), "emea" (default, Europe, Middle-East, Africa)
"apac" (Asia-Pacific) or "la" (Latin America)
-e environment the GSX environment. Either empty (production, default), "it" or "ut"
-f format the output format. Either print_r (default), json, xml or csv
-d data data for the query (serial number, order confirmation, repair number, EEE code, etc
Defaults to this machine's serial number. Or path to a JSON file.
verb one of: {$nouns_str}
noun one of: {$verbs_str}
EOT;
exit();
}
$opts = getopt( 's:u:p:r:e:m:q:f:d:' );
list( $noun, $verb ) = array_slice( $argv, -2, 2 );
if( !in_array( $noun, $nouns )) {
exit( "Error: invalid noun - {$noun}.\n" );
}
if( !in_array( $verb, $verbs )) {
exit( "Error: invalid verb - {$verb}.\n" );
}
$region = ( isset( $opts['r'] )) ? $opts['r'] : 'emea';
$format = ( isset($opts['f'] )) ? $opts['f'] : 'print_r';
$environment = ( isset( $opts['e'] )) ? $opts['e'] : null;
switch( $noun )
{
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();
}
}
$gsx = GsxLib::getInstance( $opts['s'], $opts['u'], $opts['p'], $environment, $region );
if( isset( $opts['d'] )) {
$data = $opts['d'];
} else {
// No data provided, default to this machine's serial number
$data = `/usr/sbin/system_profiler SPHardwareDataType | awk '/Serial Number/ {print $4}'`;
$data = "serialNumber={$data}";
}
@list( $k, $v ) = explode( '=', $data );
$data = ($k) ? array( $k => $v ) : $data;
switch( $noun ) {
case 'warranty':
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':
switch( $verb ) {
case 'lookup':
$result = $gsx->repairLookup( $data );
break;
case 'details':
$result = $gsx->partsPendingReturn( $data );
break;
case 'status':
$result = $gsx->repairStatus( $query );
break;
case 'create':
$json = file_get_contents( $opts['d'] );
$data = json_decode( $json, TRUE );
$result = $gsx->createCarryInRepair( $data );
break;
}
case 'model':
$result = $gsx->productModel( $query );
break;
case 'osdispatchdetail':
$result = $gsx->onsiteDispatchDetail( $query );
break;
case 'label':
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':
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 );
}
}
}
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($result);
break;
}