Merge pull request #1854 from balena-io/convert-devices-publicurl

Convert 'balena devices public-url' commands to oclif
This commit is contained in:
bulldozer-balena[bot] 2020-06-02 20:01:40 +00:00 committed by GitHub
commit 3e97669b3c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 193 additions and 121 deletions

View File

@ -16,7 +16,6 @@
*/
import * as path from 'path';
import { MarkdownFileParser } from './utils';
/**
@ -53,6 +52,7 @@ const capitanoDoc = {
files: [
'build/actions/device.js',
'build/actions-oclif/devices/supported.js',
'build/actions-oclif/device/public-url.js',
],
},
{

View File

@ -174,15 +174,12 @@ Users are encouraged to regularly update the balena CLI to the latest version.
- [device identify <uuid>](#device-identify-uuid)
- [device reboot <uuid>](#device-reboot-uuid)
- [device shutdown <uuid>](#device-shutdown-uuid)
- [device public-url enable <uuid>](#device-public-url-enable-uuid)
- [device public-url disable <uuid>](#device-public-url-disable-uuid)
- [device public-url <uuid>](#device-public-url-uuid)
- [device public-url status <uuid>](#device-public-url-status-uuid)
- [device rename <uuid> [newName]](#device-rename-uuid-newname)
- [device move <uuid>](#device-move-uuid)
- [device init](#device-init)
- [device os-update <uuid>](#device-os-update-uuid)
- [devices supported](#devices-supported)
- [device public-url <uuid>](#device-public-url-uuid)
- Environment Variables
@ -555,38 +552,6 @@ Examples:
force action if the update lock is set
## device public-url enable <uuid>
Use this command to enable public URL for a device
Examples:
$ balena device public-url enable 23c73a1
## device public-url disable <uuid>
Use this command to disable public URL for a device
Examples:
$ balena device public-url disable 23c73a1
## device public-url <uuid>
Use this command to get the public URL of a device
Examples:
$ balena device public-url 23c73a1
## device public-url status <uuid>
Use this command to determine if public URL is enabled for a device
Examples:
$ balena device public-url status 23c73a1
## device rename <uuid> [newName]
Use this command to rename a device.
@ -716,6 +681,46 @@ produce JSON output instead of tabular output
add extra columns in the tabular output (ALIASES, ARCH, STATE)
## device public-url <uuid>
This command will output the current public URL for the
specified device. It can also enable or disable the URL,
or output the enabled status, using the respective options.
The old command style 'balena device public-url enable <uuid>'
is deprecated, but still supported.
Examples:
$ balena device public-url 23c73a1
$ balena device public-url 23c73a1 --enable
$ balena device public-url 23c73a1 --disable
$ balena device public-url 23c73a1 --status
### Arguments
#### UUID
the uuid of the device to manage
#### LEGACYUUID
### Options
#### --enable
enable the public URL
#### --disable
disable the public URL
#### --status
determine if public URL is enabled
# Environment Variables
## envs

View File

@ -0,0 +1,149 @@
/**
* @license
* Copyright 2016-2020 Balena Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { flags } from '@oclif/command';
import { IArg } from '@oclif/parser/lib/args';
import { stripIndent } from 'common-tags';
import Command from '../../command';
import { ExpectedError } from '../../errors';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk } from '../../utils/lazy';
import { tryAsInteger } from '../../utils/validation';
interface FlagsDef {
enable: boolean;
disable: boolean;
status: boolean;
help?: void;
}
interface ArgsDef {
uuid: string;
// Optional hidden arg to support old command format
legacyUuid?: string;
}
export default class DevicePublicUrlCmd extends Command {
public static description = stripIndent`
Get or manage the public URL for a device.
This command will output the current public URL for the
specified device. It can also enable or disable the URL,
or output the enabled status, using the respective options.
The old command style 'balena device public-url enable <uuid>'
is deprecated, but still supported.
`;
public static examples = [
'$ balena device public-url 23c73a1',
'$ balena device public-url 23c73a1 --enable',
'$ balena device public-url 23c73a1 --disable',
'$ balena device public-url 23c73a1 --status',
];
public static args: Array<IArg<any>> = [
{
name: 'uuid',
description: 'the uuid of the device to manage',
parse: dev => tryAsInteger(dev),
required: true,
},
{
// Optional hidden arg to support old command format
name: 'legacyUuid',
parse: dev => tryAsInteger(dev),
hidden: true,
},
];
public static usage = 'device public-url <uuid>';
public static flags: flags.Input<FlagsDef> = {
enable: flags.boolean({
description: 'enable the public URL',
exclusive: ['disable', 'status'],
}),
disable: flags.boolean({
description: 'disable the public URL',
exclusive: ['enable', 'status'],
}),
status: flags.boolean({
description: 'determine if public URL is enabled',
exclusive: ['enable', 'disable'],
}),
help: cf.help,
};
public static authenticated = true;
public async run() {
const { args: params, flags: options } = this.parse<FlagsDef, ArgsDef>(
DevicePublicUrlCmd,
);
// Legacy command format support.
// Previously this command used the following format
// (changed due to oclif technicalities):
// `balena device public-url enable|disable|status <uuid>`
if (params.legacyUuid) {
const action = params.uuid;
if (!['enable', 'disable', 'status'].includes(action)) {
throw new ExpectedError(
`Unexpected arguments: ${params.uuid} ${params.legacyUuid}`,
);
}
options.enable = action === 'enable';
options.disable = action === 'disable';
options.status = action === 'status';
params.uuid = params.legacyUuid;
delete params.legacyUuid;
}
const balena = getBalenaSdk();
if (options.enable) {
// Enable public URL
await balena.models.device.enableDeviceUrl(params.uuid);
} else if (options.disable) {
// Disable public URL
await balena.models.device.disableDeviceUrl(params.uuid);
} else if (options.status) {
// Output bool indicating if public URL enabled
const hasUrl = await balena.models.device.hasDeviceUrl(params.uuid);
console.log(hasUrl);
} else {
// Output public URL
try {
const url = await balena.models.device.getDeviceUrl(params.uuid);
console.log(url);
} catch (e) {
if (e.message.includes('Device is not web accessible')) {
throw new ExpectedError(stripIndent`
Public URL is not enabled for this device.
To enable, use:
balena device public-url ${params.uuid} --enable
`);
} else {
throw e;
}
}
}
}
}

View File

@ -265,82 +265,6 @@ Examples:
},
};
export const enableDeviceUrl = {
signature: 'device public-url enable <uuid>',
description: 'enable public URL for a device',
help: `\
Use this command to enable public URL for a device
Examples:
$ balena device public-url enable 23c73a1\
`,
permission: 'user',
action(params) {
normalizeUuidProp(params);
const balena = getBalenaSdk();
return balena.models.device.enableDeviceUrl(params.uuid);
},
};
export const disableDeviceUrl = {
signature: 'device public-url disable <uuid>',
description: 'disable public URL for a device',
help: `\
Use this command to disable public URL for a device
Examples:
$ balena device public-url disable 23c73a1\
`,
permission: 'user',
action(params) {
normalizeUuidProp(params);
const balena = getBalenaSdk();
return balena.models.device.disableDeviceUrl(params.uuid);
},
};
export const getDeviceUrl = {
signature: 'device public-url <uuid>',
description: 'gets the public URL of a device',
help: `\
Use this command to get the public URL of a device
Examples:
$ balena device public-url 23c73a1\
`,
permission: 'user',
action(params) {
normalizeUuidProp(params);
const balena = getBalenaSdk();
return balena.models.device.getDeviceUrl(params.uuid).then(url => {
console.log(url);
});
},
};
export const hasDeviceUrl = {
signature: 'device public-url status <uuid>',
description: 'Returns true if public URL is enabled for a device',
help: `\
Use this command to determine if public URL is enabled for a device
Examples:
$ balena device public-url status 23c73a1\
`,
permission: 'user',
action(params) {
normalizeUuidProp(params);
const balena = getBalenaSdk();
return balena.models.device.hasDeviceUrl(params.uuid).then(hasUrl => {
console.log(hasUrl);
});
},
};
export const rename = {
signature: 'device rename <uuid> [newName]',
description: 'rename a balena device',

View File

@ -62,10 +62,6 @@ capitano.command(actions.device.remove);
capitano.command(actions.device.identify);
capitano.command(actions.device.reboot);
capitano.command(actions.device.shutdown);
capitano.command(actions.device.enableDeviceUrl);
capitano.command(actions.device.disableDeviceUrl);
capitano.command(actions.device.getDeviceUrl);
capitano.command(actions.device.hasDeviceUrl);
capitano.command(actions.device.register);
capitano.command(actions.device.move);
capitano.command(actions.device.osUpdate);

View File

@ -143,6 +143,7 @@ export const convertedCommands = [
'app:rm',
'apps',
'api-key:generate',
'device:public-url',
'devices:supported',
'envs',
'env:add',

View File

@ -41,10 +41,7 @@ Additional commands:
device init initialise a device with balenaOS
device move <uuid> move a device to another application
device os-update <uuid> Start a Host OS update for a device
device public-url <uuid> gets the public URL of a device
device public-url disable <uuid> disable public URL for a device
device public-url enable <uuid> enable public URL for a device
device public-url status <uuid> Returns true if public URL is enabled for a device
device public-url <uuid> get or manage the public URL for a device
device reboot <uuid> restart a device
device register <application> register a device
device rename <uuid> [newName] rename a balena device