@iobroker/js-controller-cli 4.0.0-alpha.45-20220114-88aaebdb → 4.0.0-alpha.49-20220121-e7aa4308

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,7 +11,6 @@ module.exports = class CLICompact extends CLICommand {
11
11
  super(options);
12
12
  this.config = fs.readJSONSync(tools.getConfigFileName());
13
13
  }
14
-
15
14
  /**
16
15
  * Executes a command
17
16
  * @param {any[]} args
@@ -283,24 +283,19 @@ module.exports = class CLIHost extends CLICommand {
283
283
  * @param {any} objects The objects DB to use
284
284
  * @param {any} instance The instance object
285
285
  * @param {string} newHostname The new hostname the instance should be running on
286
+ * @return Promise<void>
286
287
  */
287
- function changeInstanceHost(objects, instance, newHostname) {
288
- return new Promise(resolve => {
289
- const oldInstanceHost = instance.common.host;
290
- instance.from = getObjectFrom();
291
- instance.ts = Date.now();
292
- instance.common.host = newHostname;
293
- // and save it
294
- objects
295
- .setObjectAsync(instance._id, instance)
296
- .then(() => {
297
- CLI.success.instanceHostChanged(instance._id, oldInstanceHost, newHostname);
298
- resolve();
299
- })
300
- .catch(err => {
301
- CLI.error.cannotChangeObject(instance._id, err.message);
302
- // resolve anyways, we don't want to cause errors
303
- resolve();
304
- });
305
- });
288
+ async function changeInstanceHost(objects, instance, newHostname) {
289
+ const oldInstanceHost = instance.common.host;
290
+ instance.from = getObjectFrom();
291
+ instance.ts = Date.now();
292
+ instance.common.host = newHostname;
293
+ // and save it
294
+ try {
295
+ await objects.setObjectAsync(instance._id, instance);
296
+ CLI.success.instanceHostChanged(instance._id, oldInstanceHost, newHostname);
297
+ } catch (e) {
298
+ CLI.error.cannotChangeObject(instance._id, e.message);
299
+ // resolve anyways, we don't want to cause errors
300
+ }
306
301
  }
@@ -469,7 +469,7 @@ module.exports = class CLIObjects extends CLICommand {
469
469
  input: process.stdin,
470
470
  output: process.stdout
471
471
  });
472
- rl.question(result.length + ' object(s) will be deleted. Are you sure? [y/N]: ', answer => {
472
+ rl.question(`${result.length} object(s) will be deleted. Are you sure? [y/N]: `, answer => {
473
473
  rl.close();
474
474
  if (
475
475
  answer === 'y' ||
@@ -491,21 +491,19 @@ module.exports = class CLIObjects extends CLICommand {
491
491
  });
492
492
  } else {
493
493
  // only one object
494
- objects.delObject(id, err => {
494
+ objects.delObject(id, async err => {
495
495
  if (err) {
496
496
  CLI.error.objectNotFound(id, err);
497
- return void callback(3);
497
+ callback(3);
498
498
  } else {
499
- tools
500
- .removeIdFromAllEnums(objects, id)
501
- .then(() => {
502
- CLI.success.objectDeleted(id);
503
- return void callback();
504
- })
505
- .catch(e => {
506
- CLI.error.cannotDeleteObjectFromEnums(id, e.message);
507
- return void callback(3);
508
- });
499
+ try {
500
+ await tools.removeIdFromAllEnums(objects, id);
501
+ CLI.success.objectDeleted(id);
502
+ callback();
503
+ } catch (e) {
504
+ CLI.error.cannotDeleteObjectFromEnums(id, e.message);
505
+ callback(3);
506
+ }
509
507
  }
510
508
  });
511
509
  }
@@ -7,7 +7,7 @@ const ALIAS_STARTS_WITH = 'alias.';
7
7
 
8
8
  /** Command iobroker state ... */
9
9
  module.exports = class CLIStates extends CLICommand {
10
- /** @param {import('./cliCommand').CLICommandOptions} options */
10
+ /** @param {CLICommandOptions} options */
11
11
  constructor(options) {
12
12
  super(options);
13
13
  }
@@ -39,6 +39,9 @@ module.exports = class CLIStates extends CLICommand {
39
39
  case 'getvalue':
40
40
  resultTransform = obj => (obj ? formatValue(obj.val, pretty) : 'null');
41
41
  return this.get_(args, resultTransform);
42
+ case 'getBinary':
43
+ case 'getbinary':
44
+ return this._getBinary(args);
42
45
  case 'set':
43
46
  return this.set_(args);
44
47
  case 'chmod':
@@ -102,6 +105,53 @@ module.exports = class CLIStates extends CLICommand {
102
105
  });
103
106
  }
104
107
 
108
+ /**
109
+ * Checks if state is a binary state
110
+ * @param {string} id id of the state
111
+ * @param {object} objects the objects db
112
+ * @param {ioBroker.OtherObject?} obj cached object
113
+ * @return {Promise<boolean>}
114
+ * @private
115
+ */
116
+ async _isBinary(id, objects, obj) {
117
+ obj = obj || (await objects.getObjectAsync(id));
118
+
119
+ return !!(obj && (obj.binary || (obj.common && obj.common.type === 'file')));
120
+ }
121
+
122
+ /**
123
+ * Get and show binary state
124
+ *
125
+ * @param {any[]} args
126
+ * @private
127
+ */
128
+ _getBinary(args) {
129
+ const { callback, dbConnect } = this.options;
130
+ const id = args[1];
131
+ dbConnect(async (objects, states) => {
132
+ try {
133
+ /** @type Buffer | null */
134
+ const state = await states.getBinaryState(id);
135
+
136
+ if (!state) {
137
+ CLI.error.stateNotFound(id);
138
+ return void callback(1);
139
+ }
140
+
141
+ if (Buffer.isBuffer(state)) {
142
+ console.log(state.toString(this.options.encoding));
143
+ return void callback();
144
+ } else {
145
+ CLI.error.stateNotBinary(id);
146
+ return void callback(1);
147
+ }
148
+ } catch (e) {
149
+ CLI.error.unknown(e);
150
+ return void callback(1);
151
+ }
152
+ });
153
+ }
154
+
105
155
  /**
106
156
  * Returns the value of a state
107
157
  * @param {any[]} args
@@ -111,7 +161,7 @@ module.exports = class CLIStates extends CLICommand {
111
161
  const { callback, dbConnect } = this.options;
112
162
  const id = args[1];
113
163
 
114
- dbConnect((objects, states) => {
164
+ dbConnect(async (objects, states) => {
115
165
  if (id.startsWith(ALIAS_STARTS_WITH)) {
116
166
  objects.getObject(id, (err, targetObj) => {
117
167
  // alias
@@ -120,17 +170,25 @@ module.exports = class CLIStates extends CLICommand {
120
170
  typeof targetObj.common.alias.id.read === 'string'
121
171
  ? targetObj.common.alias.id.read
122
172
  : targetObj.common.alias.id;
123
- objects.getObject(aliasId, (err, sourceObj) => {
124
- // write target
125
- states.getState(aliasId, (err, state) => {
126
- if (err || !targetObj) {
127
- CLI.error.unknown(err);
173
+ objects.getObject(aliasId, async (err, sourceObj) => {
174
+ // read target
175
+ try {
176
+ if (await this._isBinary(aliasId, objects, targetObj)) {
177
+ CLI.error.stateBinaryGetUnsupported(aliasId);
178
+ return void callback(1);
179
+ }
180
+
181
+ const state = await states.getStateAsync(aliasId);
182
+ if (!state) {
183
+ CLI.error.stateNotFound(id);
128
184
  } else {
129
185
  tools.formatAliasValue(sourceObj.common, targetObj.common, state, console, '');
130
186
  console.log(resultTransform(state));
131
187
  }
132
- return void callback();
133
- });
188
+ } catch (e) {
189
+ CLI.error.unknown(e);
190
+ }
191
+ return void callback();
134
192
  });
135
193
  } else {
136
194
  CLI.error.unknown(err || `Alias ${id} has no target`);
@@ -138,14 +196,21 @@ module.exports = class CLIStates extends CLICommand {
138
196
  }
139
197
  });
140
198
  } else {
141
- states.getState(id, (err, state) => {
142
- if (err || !state) {
143
- CLI.error.stateNotFound(id, err);
199
+ try {
200
+ if (await this._isBinary(id, objects)) {
201
+ CLI.error.stateBinaryGetUnsupported(id);
202
+ return void callback(1);
203
+ }
204
+ const state = await states.getStateAsync(id);
205
+ if (!state) {
206
+ CLI.error.stateNotFound(id);
144
207
  } else {
145
208
  console.log(resultTransform(state));
146
209
  }
147
- return void callback();
148
- });
210
+ } catch (e) {
211
+ CLI.error.unknown(e);
212
+ }
213
+ return void callback();
149
214
  }
150
215
  });
151
216
  }
@@ -173,7 +238,11 @@ module.exports = class CLIStates extends CLICommand {
173
238
  const newVal = ack === undefined ? { val, ack: false } : { val, ack: !!ack };
174
239
 
175
240
  if (id.startsWith(ALIAS_STARTS_WITH)) {
176
- objects.getObject(id, (err, obj) => {
241
+ objects.getObject(id, async (err, obj) => {
242
+ if (await this._isBinary(id, objects, obj)) {
243
+ CLI.error.stateBinarySetUnsupported(id);
244
+ return void callback(1);
245
+ }
177
246
  // alias
178
247
  if (obj && obj.common && obj.common.alias && obj.common.alias.id) {
179
248
  const aliasId =
@@ -227,11 +296,17 @@ module.exports = class CLIStates extends CLICommand {
227
296
  }
228
297
  });
229
298
  } else {
230
- objects.getObject(id, (err, obj) => {
299
+ objects.getObject(id, async (err, obj) => {
231
300
  if (err) {
232
301
  CLI.error.unknown(err);
233
302
  return void callback(1); // access error
234
303
  }
304
+
305
+ if (await this._isBinary(id, objects, obj)) {
306
+ CLI.error.stateBinarySetUnsupported(id);
307
+ return void callback(1);
308
+ }
309
+
235
310
  if (!obj && !force) {
236
311
  CLI.error.objectNotFound(id, 'null');
237
312
  return void callback(1); // object not exists
@@ -4,6 +4,11 @@ const { tools } = require('@iobroker/js-controller-common');
4
4
  const errorMessages = Object.freeze({
5
5
  stateNotFound: (/** @type {string} */ stateID, /** @type {string?} */ error) =>
6
6
  `The state ${stateID} was not found!` + (error ? ` Reason: ${error}` : ''),
7
+ stateNotBinary: (/** @type {string} */ stateId) => `State "${stateId}" is not binary.`,
8
+ stateBinaryGetUnsupported: (/** @type {string} */ stateId) =>
9
+ `State "${stateId}" is a binary state, please use getBinary.`,
10
+ stateBinarySetUnsupported: (/** @type {string} */ stateId) =>
11
+ `State "${stateId}" is a binary state and cannot be set via cli.`,
7
12
  objectNotFound: (/** @type {string} */ objectID, /** @type {string?} */ error) =>
8
13
  `The object ${objectID} was not found!` + (error ? ` Reason: ${error}` : ''),
9
14
  cannotUpdateObject: (/** @type {string} */ objectID, /** @type {string?} */ error) =>
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@iobroker/js-controller-cli",
3
- "version": "4.0.0-alpha.45-20220114-88aaebdb",
3
+ "version": "4.0.0-alpha.49-20220121-e7aa4308",
4
4
  "engines": {
5
5
  "node": ">=12.0.0"
6
6
  },
7
7
  "dependencies": {
8
- "@iobroker/js-controller-common": "4.0.0-alpha.45-20220114-88aaebdb",
9
- "@iobroker/js-controller-common-db": "4.0.0-alpha.45-20220114-88aaebdb",
8
+ "@iobroker/js-controller-common": "4.0.0-alpha.49-20220121-e7aa4308",
9
+ "@iobroker/js-controller-common-db": "4.0.0-alpha.49-20220121-e7aa4308",
10
10
  "chokidar": "^3.5.2",
11
11
  "daemonize2": "^0.4.2",
12
12
  "debug": "^4.3.3",
@@ -38,5 +38,5 @@
38
38
  "lib/",
39
39
  "index.js"
40
40
  ],
41
- "gitHead": "e2fd3e7af91eb83c30f8ae45f71d6e178edc7b28"
41
+ "gitHead": "f42f117b74d3c4bfac0bc872c087d382ca897b7c"
42
42
  }