diff --git a/completion/_balena b/completion/_balena index a8792f51..8f202678 100644 --- a/completion/_balena +++ b/completion/_balena @@ -12,7 +12,7 @@ _balena() { # Sub-completions api_key_cmds=( generate ) config_cmds=( generate inject read reconfigure write ) - device_cmds=( deactivate identify init local-mode move os-update pin public-url purge reboot register rename restart rm shutdown track-fleet ) + device_cmds=( deactivate identify init local-mode move os-update pin public-url purge reboot register rename restart rm services shutdown track-fleet ) devices_cmds=( supported ) env_cmds=( add rename rm ) fleet_cmds=( create pin purge rename restart rm track-latest ) diff --git a/completion/balena-completion.bash b/completion/balena-completion.bash index 592f75cd..1e047539 100644 --- a/completion/balena-completion.bash +++ b/completion/balena-completion.bash @@ -11,7 +11,7 @@ _balena_complete() # Sub-completions api_key_cmds="generate" config_cmds="generate inject read reconfigure write" - device_cmds="deactivate identify init local-mode move os-update pin public-url purge reboot register rename restart rm shutdown track-fleet" + device_cmds="deactivate identify init local-mode move os-update pin public-url purge reboot register rename restart rm services shutdown track-fleet" devices_cmds="supported" env_cmds="add rename rm" fleet_cmds="create pin purge rename restart rm track-latest" diff --git a/lib/commands/device/services.ts b/lib/commands/device/services.ts new file mode 100644 index 00000000..ddffdf36 --- /dev/null +++ b/lib/commands/device/services.ts @@ -0,0 +1,93 @@ +/** + * @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 type { IArg } from '@oclif/parser/lib/args'; +import { ImageInstall } from 'balena-sdk'; +import Command from '../../command'; +import * as cf from '../../utils/common-flags'; +import { getBalenaSdk, getVisuals, stripIndent } from '../../utils/lazy'; +import { getExpandedProp } from '../../utils/pine'; + +interface FlagsDef { + help: void; +} + +interface ArgsDef { + uuid: string; +} + +interface AugmentedImageInstall extends ImageInstall { + name?: string; + release?: string; +} + +export default class DeviceServicesCmd extends Command { + public static description = stripIndent` + Show info about a device's services. + + Show info about a device's services. + `; + public static examples = ['$ balena device services 23c73a1']; + + public static args: Array> = [ + { + name: 'uuid', + description: 'the uuid of the device whose services to show info about', + required: true, + }, + ]; + + public static usage = 'device services '; + + public static flags: flags.Input = { + help: cf.help, + }; + + public static authenticated = true; + + public async run() { + const { args: params } = this.parse(DeviceServicesCmd); + + const balena = getBalenaSdk(); + + try { + const device = await balena.models.device.getWithServiceDetails( + params.uuid, + ); + console.log( + getVisuals().table.horizontal( + device.image_install?.map((imageInstall) => { + const newImageInstall: AugmentedImageInstall = { ...imageInstall }; + newImageInstall.name = getExpandedProp( + getExpandedProp(imageInstall.image, 'is_a_build_of__service')!, + 'service_name', + ); + newImageInstall.release = getExpandedProp( + imageInstall.is_provided_by__release, + 'commit', + ); + return newImageInstall; + }), + ['name', 'status', 'release', 'id'], + ), + ); + } catch (e) { + throw e; + } + } +}