[apps/tidep0084.git] / example / iot-gateway / node_modules / aws-iot-device-sdk / examples / lib / cmdline.js
1 /*
2 * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
16 //node.js deps
17 const fs = require('fs');
19 //npm deps
20 const minimist = require('minimist');
22 //app deps
23 const isUndefined = require('../../common/lib/is-undefined');
25 //begin module
26 var clientIdDefault;
27 if (!isUndefined(process.env.USER)) {
28 clientIdDefault = process.env.USER.concat(Math.floor((Math.random() * 100000) + 1));
29 } else {
30 clientIdDefault = 'nouser' + (Math.floor((Math.random() * 100000) + 1));
31 }
33 module.exports = function(description, args, processFunction, argumentHelp) {
34 var doHelp = function() {
35 var progName = process.argv[1];
36 var lastSlash = progName.lastIndexOf('/');
37 if (lastSlash !== -1) {
38 progName = progName.substring(lastSlash + 1, progName.length);
39 }
40 if (isUndefined(argumentHelp)) {
41 console.log('Usage: ' + progName + ' [OPTION...]');
42 } else {
43 console.log('Usage: ' + progName + ' [OPTION...] ARGUMENTS...');
44 }
45 console.log('\n' + progName + ': ' + description + '\n\n' +
46 ' Options\n\n' +
47 ' -g, --aws-region=REGION AWS IoT region\n' +
48 ' -i, --client-id=ID use ID as client ID\n' +
49 ' -H, --host-name=HOST connect to HOST (overrides --aws-region)\n' +
50 ' -p, --port=PORT connect to PORT (overrides defaults)\n' +
51 ' -P, --protocol=PROTOCOL connect using PROTOCOL (mqtts|wss)\n' +
52 ' -k, --private-key=FILE use FILE as private key\n' +
53 ' -c, --client-certificate=FILE use FILE as client certificate\n' +
54 ' -a, --ca-certificate=FILE use FILE as CA certificate\n' +
55 ' -f, --certificate-dir=DIR look in DIR for certificates\n' +
56 ' -F, --configuration-file=FILE use FILE (JSON format) for configuration\n' +
57 ' -r, --reconnect-period-ms=VALUE use VALUE as the reconnect period (ms)\n' +
58 ' -K, --keepalive=VALUE use VALUE as the keepalive time (seconds)\n' +
59 ' -t, --test-mode=[1-n] set test mode for multi-process tests\n' +
60 ' -T, --thing-name=THINGNAME access thing shadow named THINGNAME\n' +
61 ' -d, --delay-ms=VALUE delay in milliseconds before publishing\n' +
62 ' -D, --debug print additional debugging information\n\n' +
63 ' Default values\n\n' +
64 ' aws-region us-east-1\n' +
65 ' client-id $USER<random-integer>\n' +
66 ' protocol mqtts\n' +
67 ' private-key private.pem.key\n' +
68 ' client-certificate certificate.pem.crt\n' +
69 ' ca-certificate root-CA.crt\n' +
70 ' reconnect-period-ms 3000ms\n' +
71 ' delay-ms 4000ms\n' +
72 ' test-mode 1\n');
73 if (!isUndefined(argumentHelp)) {
74 console.log(argumentHelp);
75 }
76 };
77 args = minimist(args, {
78 string: ['certificate-dir', 'aws-region', 'private-key', 'client-certificate',
79 'ca-certificate', 'client-id', 'thing-name', 'configuration-file',
80 'host-name', 'protocol'
81 ],
82 integer: ['reconnect-period-ms', 'test-mode', 'port', 'delay-ms',
83 'keepalive'
84 ],
85 boolean: ['help', 'debug'],
86 alias: {
87 region: ['g', 'aws-region'],
88 clientId: ['i', 'client-id'],
89 privateKey: ['k', 'private-key'],
90 clientCert: ['c', 'client-certificate'],
91 caCert: ['a', 'ca-certificate'],
92 certDir: ['f', 'certificate-dir'],
93 configFile: ['F', 'configuration-file'],
94 baseReconnectTimeMs: ['r', 'reconnect-period-ms'],
95 keepAlive: ['K', 'keepalive'],
96 testMode: ['t', 'test-mode'],
97 thingName: ['T', 'thing-name'],
98 delay: ['d', 'delay-ms'],
99 Port: ['p', 'port'],
100 Protocol: ['P', 'protocol'],
101 Host: ['H', 'host-name'],
102 Debug: ['D', 'debug'],
103 help: 'h'
104 },
105 default: {
106 region: 'us-east-1',
107 protocol: 'mqtts',
108 clientId: clientIdDefault,
109 privateKey: 'private.pem.key',
110 clientCert: 'certificate.pem.crt',
111 caCert: 'root-CA.crt',
112 testMode: 1,
113 baseReconnectTimeMs: 4000,
114 keepAlive: 30,
115 /* milliseconds */
116 delay: 4000,
117 /* milliseconds */
118 Debug: false
119 },
120 unknown: function() {
121 console.error('***unrecognized options***');
122 doHelp();
123 process.exit(1);
124 }
125 });
126 if (args.help) {
127 doHelp();
128 return;
129 }
130 //
131 // If the user has specified a directory where certificates are located,
132 // prepend it to all of the certificate filenames.
133 //
134 if (!isUndefined(args.certDir)) {
135 args.privateKey = args.certDir + '/' + args.privateKey;
136 args.clientCert = args.certDir + '/' + args.clientCert;
137 args.caCert = args.certDir + '/' + args.caCert;
138 }
139 //
140 // If the configuration file is defined, read it in and set the parameters based
141 // on the values inside; these will override any other arguments specified on
142 // the command line.
143 //
144 if (!isUndefined(args.configFile)) {
145 if (!fs.existsSync(args.configFile)) {
146 console.error('\n' + args.configFile + ' doesn\'t exist (--help for usage)\n');
147 return;
148 }
149 var config = JSON.parse(fs.readFileSync(args.configFile, 'utf8'));
151 if (!isUndefined(config.privateKey)) {
152 if (!isUndefined(args.certDir)) {
153 args.privateKey = args.certDir + '/' + config.privateKey;
154 } else {
155 args.privateKey = config.privateKey;
156 }
157 }
158 if (!isUndefined(config.clientCert)) {
159 if (!isUndefined(args.certDir)) {
160 args.clientCert = args.certDir + '/' + config.clientCert;
161 } else {
162 args.clientCert = config.clientCert;
163 }
164 }
165 if (!isUndefined(config.caCert)) {
166 if (!isUndefined(args.certDir)) {
167 args.caCert = args.certDir + '/' + config.caCert;
168 } else {
169 args.caCert = config.caCert;
170 }
171 }
172 if (!isUndefined(config.host)) {
173 args.host = config.host;
174 }
175 if (!isUndefined(config.port)) {
176 args.port = config.port;
177 }
178 //
179 // When using a JSON configuration document from the AWS Console, allow
180 // the client ID to be overriden by the command line option for client ID.
181 // This is required to run the example programs from a JSON configuration
182 // document, since both instances must use different client IDs.
183 //
184 if (!isUndefined(config.clientId) && isUndefined(args.clientId)) {
185 args.clientId = config.clientId;
186 }
187 if (!isUndefined(config.thingName)) {
188 args.thingName = config.thingName;
189 }
190 }
192 if (args.Protocol === 'mqtts') {
193 //
194 // Client certificate, private key, and CA certificate must all exist if
195 // connecting via mqtts.
196 //
197 if (!fs.existsSync(args.privateKey)) {
198 console.error('\n' + args.privateKey + ' doesn\'t exist (--help for usage)\n');
199 return;
200 }
201 if (!fs.existsSync(args.clientCert)) {
202 console.error('\n' + args.clientCert + ' doesn\'t exist (--help for usage)\n');
203 return;
204 }
205 if (!fs.existsSync(args.caCert)) {
206 console.error('\n' + args.caCert + ' doesn\'t exist (--help for usage)\n');
207 return;
208 }
209 }
211 processFunction(args);
212 };