1
|
|
|
#!/usr/bin/php |
2
|
|
|
|
3
|
|
|
<?php |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This is an example of client application that send commands to the CPE stack |
7
|
|
|
* Your application initiate new workflows |
8
|
|
|
* Using the CpeClientSdk it's easy to send the proper SQS message to the CPE stack |
9
|
|
|
* You message will be read by the CPE Stack InputPoller process |
10
|
|
|
* The InputPoller will read your message and initiate the proper actions |
11
|
|
|
* Such as: Start a new workflow |
12
|
|
|
**/ |
13
|
|
|
|
14
|
|
|
require __DIR__ . "/vendor/autoload.php"; |
15
|
|
|
|
16
|
|
|
function start_job($args) |
17
|
|
|
{ |
18
|
|
|
global $CpeClientSdk; |
19
|
|
|
global $clientInfo; |
20
|
|
|
|
21
|
|
|
if (count($args) != 2) |
22
|
|
|
{ |
23
|
|
|
print("[ERROR] Invalid args! Please provide a filename after the command! (try again if you used [tab] to find your file. It fails somehow.)\n"); |
24
|
|
|
return; |
25
|
|
|
} |
26
|
|
|
if (!file_exists($args[1])) |
27
|
|
|
{ |
28
|
|
|
print("[ERROR] The file provided doesn't exists!\n"); |
29
|
|
|
return; |
30
|
|
|
} |
31
|
|
|
if (!($content = file_get_contents($args[1]))) |
32
|
|
|
{ |
33
|
|
|
print("[ERROR] Unable to read file '$args[1]'!\n"); |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
try { |
38
|
|
|
print("[INFO] Starting a new job!\n"); |
39
|
|
|
// You must JSON decode it |
40
|
|
|
$decodedClient = json_decode($clientInfo); |
41
|
|
|
|
42
|
|
|
$CpeClientSdk->start_job($decodedClient, $content); |
43
|
|
|
} |
44
|
|
|
catch (Exception $e) { |
45
|
|
|
print("[ERROR] " . $e->getMessage() . "\n"); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* CLIENT COMMANDER |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
$help = <<<EOF |
55
|
|
|
Use the following commands to send messages to the stack. |
56
|
|
|
|
57
|
|
|
Commands: |
58
|
|
|
start_job <filepath>: Start a new job. Pass a JSON file containing the instruction (see: input_samples folder) |
59
|
|
|
|
60
|
|
|
EOF; |
61
|
|
|
|
62
|
|
|
|
63
|
|
View Code Duplication |
function check_input_parameters() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
global $region; |
66
|
|
|
global $secret; |
67
|
|
|
global $key; |
68
|
|
|
global $debug; |
69
|
|
|
global $clientInfo; |
70
|
|
|
global $argv; |
71
|
|
|
|
72
|
|
|
// Handle input parameters |
73
|
|
|
if (!($options = getopt("c:k::s::r::hd"))) |
74
|
|
|
usage(); |
75
|
|
|
if (isset($options['h'])) |
76
|
|
|
usage(); |
77
|
|
|
|
78
|
|
|
if (isset($options['d'])) |
79
|
|
|
$debug = true; |
80
|
|
|
|
81
|
|
|
if (isset($options['c'])) |
82
|
|
|
{ |
83
|
|
|
$clientConfFile = $options['c']; |
84
|
|
|
if (!file_exists($clientConfFile)) |
85
|
|
|
throw new Exception("The client config file is not valid!"); |
86
|
|
|
if (!($clientInfo = file_get_contents($clientConfFile))) |
87
|
|
|
throw new Exception("Unable to read the file"); |
88
|
|
|
} |
89
|
|
|
else |
90
|
|
|
throw new Exception("Please provide the client config file!"); |
91
|
|
|
|
92
|
|
|
if (isset($options['k'])) |
93
|
|
|
$key = $options['k']; |
94
|
|
|
else |
95
|
|
|
$key = getenv("AWS_ACCESS_KEY_ID"); |
96
|
|
|
|
97
|
|
|
if (isset($options['s'])) |
98
|
|
|
$secret = $options['s']; |
99
|
|
|
else |
100
|
|
|
$secret = getenv("AWS_SECRET_KEY"); |
101
|
|
|
|
102
|
|
|
if (isset($options['r'])) |
103
|
|
|
$region = $options['r']; |
104
|
|
|
else |
105
|
|
|
$region = getenv("AWS_DEFAULT_REGION"); |
106
|
|
|
if (!$region) |
107
|
|
|
throw new Exception("Please provide your AWS region as parameter or using AWS_DEFAULT_REGION env var !"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
function usage() |
111
|
|
|
{ |
112
|
|
|
global $help; |
113
|
|
|
|
114
|
|
|
echo("Usage: php ". basename(__FILE__) . " -c configFile [-h] [-k <key>] [-s <secret>] [-r <region>]\n"); |
115
|
|
|
echo("-h: Print this help\n"); |
116
|
|
|
echo("-d: Debug mode\n"); |
117
|
|
|
echo("-c: configFile\n"); |
118
|
|
|
echo("-k <AWS key>: Optional. Will use env variables by default\n"); |
119
|
|
|
echo("-s <AWS secret>: Optional. Will use env variables by default\n"); |
120
|
|
|
echo("-r <AWS region>: Optional. Will use env variables by default\n\n"); |
121
|
|
|
echo($help); |
122
|
|
|
exit(0); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
try { |
127
|
|
|
check_input_parameters(); |
128
|
|
|
} |
129
|
|
|
catch (Exception $e) { |
130
|
|
|
print "[ERROR] " . $e->getMessage() . "\n"; |
131
|
|
|
exit(2); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
// Instanciate ComSDK to communicate with the stack |
135
|
|
|
try { |
136
|
|
|
$CpeClientSdk = new SA\CpeClientSdk($key, $secret, $region, $debug); |
137
|
|
|
} catch (Exception $e) { |
138
|
|
|
exit($e->getMessage()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Commands mapping |
142
|
|
|
$commandMap = [ |
143
|
|
|
"start_job" => "start_job", |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
// Look for input commands |
147
|
|
|
print($help); |
148
|
|
|
while (42) |
149
|
|
|
{ |
150
|
|
|
// Prompt (<3 php) |
151
|
|
|
$line = readline("Command [enter]: "); |
152
|
|
|
if (!$line) |
153
|
|
|
continue; |
154
|
|
|
readline_add_history($line); |
155
|
|
|
|
156
|
|
|
// Process user input |
157
|
|
|
$args = explode(" ", $line); |
158
|
|
|
if (!isset($commandMap[$args[0]])) |
159
|
|
|
print "[ERROR] Command not found!\n"; |
160
|
|
|
else |
161
|
|
|
$commandMap[$args[0]]($args); |
162
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.