Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use oclif flag options where possible #2886

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions docs/balena-cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1678,10 +1678,6 @@ note content

device UUID

#### --dev DEV



## device os-update

### Description
Expand Down
2 changes: 1 addition & 1 deletion src/commands/api-key/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class APIKeyListCmd extends Command {
char: 'u',
description: 'show API keys for your user',
}),
fleet: cf.fleet,
fleet: cf.fleet(),
};

public static authenticated = true;
Expand Down
16 changes: 4 additions & 12 deletions src/commands/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,15 @@ ${dockerignoreHelp}
arch: Flags.string({
description: 'the architecture to build for',
char: 'A',
exclusive: ['fleet'],
dependsOn: ['deviceType'],
}),
deviceType: Flags.string({
description: 'the type of device this build is for',
char: 'd',
exclusive: ['fleet'],
}),
fleet: cf.fleet,
fleet: cf.fleet({ exclusive: ['deviceType', 'arch'] }),
...composeCliFlags,
...dockerCliFlags,
};
Expand Down Expand Up @@ -148,17 +151,6 @@ ${dockerignoreHelp}
}

protected async validateOptions(opts: FlagsDef, sdk: BalenaSDK) {
// Validate option combinations
if (
(opts.fleet == null && (opts.arch == null || opts.deviceType == null)) ||
(opts.fleet != null && (opts.arch != null || opts.deviceType != null))
) {
const { ExpectedError } = await import('../../errors');
throw new ExpectedError(
'You must specify either a fleet (-f), or the device type (-d) and optionally the architecture (-A)',
);
}

// Validate project directory
const { validateProjectDirectory } = await import('../../utils/compose_ts');
const { dockerfilePath, registrySecrets } = await validateProjectDirectory(
Expand Down
11 changes: 3 additions & 8 deletions src/commands/config/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ export default class ConfigGenerateCmd extends Command {
description: 'a balenaOS version',
required: true,
}),
fleet: { ...cf.fleet, exclusive: ['device'] },
fleet: cf.fleet({ exclusive: ['device'] }),
dev: cf.dev,
secureBoot: cf.secureBoot,
device: {
...cf.device,
...cf.device(),
exclusive: [
'fleet',
'provisioning-key-name',
Expand All @@ -83,6 +83,7 @@ export default class ConfigGenerateCmd extends Command {
deviceType: Flags.string({
description:
"device type slug (run 'balena device-type list' for possible values)",
dependsOn: ['fleet'],
}),
'generate-device-api-key': Flags.boolean({
description: 'generate a fresh device key for the device',
Expand Down Expand Up @@ -240,9 +241,6 @@ export default class ConfigGenerateCmd extends Command {
$ balena help config generate
`;

protected readonly deviceTypeNotAllowedMessage =
'The --deviceType option can only be used alongside the --fleet option';

protected async validateOptions(
options: Interfaces.InferredFlags<typeof ConfigGenerateCmd.flags>,
) {
Expand All @@ -252,9 +250,6 @@ export default class ConfigGenerateCmd extends Command {
throw new ExpectedError(this.missingDeviceOrAppMessage);
}

if (!options.fleet && options.deviceType) {
throw new ExpectedError(this.deviceTypeNotAllowedMessage);
}
const { normalizeOsVersion } = await import('../../utils/normalization');
options.version = normalizeOsVersion(options.version);
const { validateDevOptionAndWarn } = await import('../../utils/config');
Expand Down
7 changes: 4 additions & 3 deletions src/commands/device/deactivate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ export default class DeviceDeactivateCmd extends Command {
public static authenticated = true;

public async run() {
const { args: params, flags: options } =
await this.parse(DeviceDeactivateCmd);
const {
args: { uuid },
flags: options,
} = await this.parse(DeviceDeactivateCmd);

const balena = getBalenaSdk();
const patterns = await import('../../utils/patterns');

const uuid = params.uuid;
const deactivationWarning = `
Warning! Deactivating a device will charge a fee equivalent to the
normal monthly cost for the device (e.g. $1 for an essentials device);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/device/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class DeviceInitCmd extends Command {
];

public static flags = {
fleet: cf.fleet,
fleet: cf.fleet(),
yes: cf.yes,
advanced: Flags.boolean({
char: 'v',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/device/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class DeviceListCmd extends Command {
];

public static flags = {
fleet: cf.fleet,
fleet: cf.fleet(),
json: cf.json,
};

Expand Down
2 changes: 1 addition & 1 deletion src/commands/device/move.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default class DeviceMoveCmd extends Command {
};

public static flags = {
fleet: cf.fleet,
fleet: cf.fleet(),
};

public static authenticated = true;
Expand Down
28 changes: 8 additions & 20 deletions src/commands/device/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
* limitations under the License.
*/

import { Flags, Args, Command } from '@oclif/core';
import { ExpectedError } from '../../errors';
import { Args, Command } from '@oclif/core';
import * as cf from '../../utils/common-flags';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';

Expand All @@ -41,35 +40,24 @@ export default class DeviceNoteCmd extends Command {
public static args = {
note: Args.string({
description: 'note content',
required: true,
}),
};

public static flags = {
device: { exclusive: ['dev'], ...cf.device },
dev: Flags.string({
exclusive: ['device'],
hidden: true,
}),
device: cf.device({ required: true }),
};

public static authenticated = true;

public async run() {
const { args: params, flags: options } = await this.parse(DeviceNoteCmd);

if (params.note?.length === 0) {
throw new ExpectedError('Missing note content');
}

options.device = options.device || options.dev;
delete options.dev;

if (options.device == null || options.device.length === 0) {
throw new ExpectedError('Missing device UUID (--device)');
}
const {
args: params,
flags: { device },
} = await this.parse(DeviceNoteCmd);

const balena = getBalenaSdk();

return balena.models.device.setNote(options.device, params.note ?? '');
return balena.models.device.setNote(device!, params.note!);
}
}
11 changes: 2 additions & 9 deletions src/commands/device/tunnel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
*/

import { Flags, Args, Command } from '@oclif/core';
import {
NoPortsDefinedError,
InvalidPortMappingError,
ExpectedError,
} from '../../errors';
import { InvalidPortMappingError, ExpectedError } from '../../errors';
import { getBalenaSdk, stripIndent } from '../../utils/lazy';
import { lowercaseIfSlug } from '../../utils/normalization';

Expand Down Expand Up @@ -85,6 +81,7 @@ export default class DeviceTunnelCmd extends Command {
'port mapping in the format <remotePort>[:[localIP:]localPort]',
char: 'p',
multiple: true,
required: true,
}),
};

Expand Down Expand Up @@ -117,10 +114,6 @@ export default class DeviceTunnelCmd extends Command {
}
};

if (options.port === undefined) {
throw new NoPortsDefinedError();
}

// Ascertain device uuid
const { getOnlineTargetDeviceUuid } = await import('../../utils/patterns');
const uuid = await getOnlineTargetDeviceUuid(sdk, params.deviceOrFleet);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/env/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ export default class EnvListCmd extends Command {
];

public static flags = {
fleet: { ...cf.fleet, exclusive: ['device'] },
fleet: { ...cf.fleet(), exclusive: ['device'] },
config: Flags.boolean({
default: false,
char: 'c',
description: 'show configuration variables only',
exclusive: ['service'],
}),
device: { ...cf.device, exclusive: ['fleet'] },
device: { ...cf.device(), exclusive: ['fleet'] },
json: cf.json,
service: { ...cf.service, exclusive: ['config'] },
};
Expand Down
4 changes: 2 additions & 2 deletions src/commands/env/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ export default class EnvSetCmd extends Command {
public static strict = false;

public static flags = {
fleet: { ...cf.fleet, exclusive: ['device'] },
device: { ...cf.device, exclusive: ['fleet'] },
fleet: { ...cf.fleet(), exclusive: ['device'] },
device: { ...cf.device(), exclusive: ['fleet'] },
quiet: cf.quiet,
service: cf.service,
};
Expand Down
2 changes: 1 addition & 1 deletion src/commands/join/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export default class JoinCmd extends Command {
};

public static flags = {
fleet: cf.fleet,
fleet: cf.fleet(),
pollInterval: Flags.integer({
description: 'the interval in minutes to check for updates',
char: 'i',
Expand Down
4 changes: 2 additions & 2 deletions src/commands/os/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export default class OsConfigureCmd extends Command {
description:
'ask advanced configuration questions (when in interactive mode)',
}),
fleet: { ...cf.fleet, exclusive: ['device'] },
fleet: { ...cf.fleet(), exclusive: ['device'] },
config: Flags.string({
description:
'path to a pre-generated config.json file to be injected in the OS image',
Expand All @@ -120,7 +120,7 @@ export default class OsConfigureCmd extends Command {
dev: cf.dev,
secureBoot: cf.secureBoot,
device: {
...cf.device,
...cf.device(),
exclusive: [
'fleet',
'provisioning-key-name',
Expand Down
2 changes: 1 addition & 1 deletion src/commands/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class PreloadCmd extends Command {
};

public static flags = {
fleet: cf.fleet,
fleet: cf.fleet(),
commit: Flags.string({
description: `\
The commit hash of the release to preload. Use "current" to specify the current
Expand Down
2 changes: 1 addition & 1 deletion src/commands/support/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default class SupportCmd extends Command {
char: 'd',
}),
fleet: {
...cf.fleet,
...cf.fleet(),
description:
'comma-separated list (no spaces) of fleet names or slugs (preferred)',
},
Expand Down
4 changes: 2 additions & 2 deletions src/commands/tag/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ export default class TagListCmd extends Command {

public static flags = {
fleet: {
...cf.fleet,
...cf.fleet(),
exclusive: ['device', 'release'],
},
device: {
...cf.device,
...cf.device(),
exclusive: ['fleet', 'release'],
},
release: {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/tag/rm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export default class TagRmCmd extends Command {

public static flags = {
fleet: {
...cf.fleet,
...cf.fleet(),
exclusive: ['device', 'release'],
},
device: {
...cf.device,
...cf.device(),
exclusive: ['fleet', 'release'],
},
release: {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/tag/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ export default class TagSetCmd extends Command {

public static flags = {
fleet: {
...cf.fleet,
...cf.fleet(),
exclusive: ['device', 'release'],
},
device: {
...cf.device,
...cf.device(),
exclusive: ['fleet', 'release'],
},
release: {
Expand Down
6 changes: 0 additions & 6 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,6 @@ export class InvalidPortMappingError extends ExpectedError {
}
}

export class NoPortsDefinedError extends ExpectedError {
constructor() {
super('No ports have been provided.');
}
}

export class SIGINTError extends ExpectedError {}

/**
Expand Down
24 changes: 15 additions & 9 deletions src/utils/common-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ import { Flags } from '@oclif/core';
import { stripIndent } from './lazy';
import { lowercaseIfSlug } from './normalization';

export const fleet = Flags.string({
char: 'f',
description: 'fleet name or slug (preferred)',
parse: lowercaseIfSlug,
});
// eslint-disable-next-line id-denylist
export const fleet = (extraOpts?: Partial<typeof Flags.string>) =>
Flags.string({
char: 'f',
description: 'fleet name or slug (preferred)',
parse: lowercaseIfSlug,
...extraOpts,
});

export const device = Flags.string({
char: 'd',
description: 'device UUID',
});
// eslint-disable-next-line id-denylist
export const device = (extraOpts?: Partial<typeof Flags.string>) =>
Flags.string({
char: 'd',
description: 'device UUID',
...extraOpts,
});

export const quiet = Flags.boolean({
char: 'q',
Expand Down
Loading