mirror of
https://github.com/balena-io/balena-cli.git
synced 2024-12-24 07:46:39 +00:00
Refactor balena build for clarity
Change-type: patch
This commit is contained in:
parent
735af9f6a9
commit
6155509f4c
@ -36,14 +36,16 @@ import { buildProject, composeCliFlags } from '../../utils/compose_ts';
|
|||||||
import type { BuildOpts, DockerCliFlags } from '../../utils/docker';
|
import type { BuildOpts, DockerCliFlags } from '../../utils/docker';
|
||||||
import { dockerCliFlags } from '../../utils/docker';
|
import { dockerCliFlags } from '../../utils/docker';
|
||||||
|
|
||||||
// TODO: For this special one we can't use Interfaces.InferredFlags/InferredArgs
|
type ComposeGenerateOptsParam = Parameters<typeof compose.generateOpts>[0];
|
||||||
// because of the 'registry-secrets' type which is defined in the actual code
|
|
||||||
// as a path (string | undefined) but then the cli turns it into an object
|
interface PrepareBuildOpts
|
||||||
interface FlagsDef extends ComposeCliFlags, DockerCliFlags {
|
extends ComposeCliFlags,
|
||||||
|
DockerCliFlags,
|
||||||
|
ComposeGenerateOptsParam {
|
||||||
arch?: string;
|
arch?: string;
|
||||||
deviceType?: string;
|
deviceType?: string;
|
||||||
fleet?: string;
|
fleet?: string;
|
||||||
source?: string; // Not part of command profile - source param copied here.
|
source?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default class BuildCmd extends Command {
|
export default class BuildCmd extends Command {
|
||||||
@ -113,29 +115,31 @@ ${dockerignoreHelp}
|
|||||||
const logger = Logger.getLogger();
|
const logger = Logger.getLogger();
|
||||||
logger.logDebug('Parsing input...');
|
logger.logDebug('Parsing input...');
|
||||||
|
|
||||||
// `build` accepts `source` as a parameter, but compose expects it as an option
|
const prepareBuildOpts = {
|
||||||
options.source = params.source;
|
...options,
|
||||||
delete params.source;
|
source: params.source,
|
||||||
|
};
|
||||||
|
|
||||||
await this.resolveArchFromDeviceType(sdk, options);
|
await this.resolveArchFromDeviceType(sdk, prepareBuildOpts);
|
||||||
|
|
||||||
await this.validateOptions(options, sdk);
|
await this.validateOptions(prepareBuildOpts, sdk);
|
||||||
|
|
||||||
// Build args are under consideration for removal - warn user
|
// Build args are under consideration for removal - warn user
|
||||||
if (options.buildArg) {
|
if (prepareBuildOpts.buildArg) {
|
||||||
console.log(buildArgDeprecation);
|
console.log(buildArgDeprecation);
|
||||||
}
|
}
|
||||||
|
|
||||||
const app = await this.getAppAndResolveArch(options);
|
const app = await this.getAppAndResolveArch(prepareBuildOpts);
|
||||||
|
|
||||||
const { docker, buildOpts, composeOpts } = await this.prepareBuild(options);
|
const { docker, buildOpts, composeOpts } =
|
||||||
|
await this.prepareBuild(prepareBuildOpts);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await this.buildProject(docker, logger, composeOpts, {
|
await this.buildProject(docker, logger, composeOpts, {
|
||||||
appType: app?.application_type?.[0],
|
appType: app?.application_type?.[0],
|
||||||
arch: options.arch!,
|
arch: prepareBuildOpts.arch!,
|
||||||
deviceType: options.deviceType!,
|
deviceType: prepareBuildOpts.deviceType!,
|
||||||
buildEmulated: options.emulated,
|
buildEmulated: prepareBuildOpts.emulated,
|
||||||
buildOpts,
|
buildOpts,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@ -147,7 +151,7 @@ ${dockerignoreHelp}
|
|||||||
logger.logSuccess('Build succeeded!');
|
logger.logSuccess('Build succeeded!');
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async validateOptions(opts: FlagsDef, sdk: BalenaSDK) {
|
protected async validateOptions(opts: PrepareBuildOpts, sdk: BalenaSDK) {
|
||||||
// Validate option combinations
|
// Validate option combinations
|
||||||
if (
|
if (
|
||||||
(opts.fleet == null && (opts.arch == null || opts.deviceType == null)) ||
|
(opts.fleet == null && (opts.arch == null || opts.deviceType == null)) ||
|
||||||
@ -175,7 +179,10 @@ ${dockerignoreHelp}
|
|||||||
opts['registry-secrets'] = registrySecrets;
|
opts['registry-secrets'] = registrySecrets;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async resolveArchFromDeviceType(sdk: BalenaSDK, opts: FlagsDef) {
|
protected async resolveArchFromDeviceType(
|
||||||
|
sdk: BalenaSDK,
|
||||||
|
opts: PrepareBuildOpts,
|
||||||
|
) {
|
||||||
if (opts.deviceType != null && opts.arch == null) {
|
if (opts.deviceType != null && opts.arch == null) {
|
||||||
try {
|
try {
|
||||||
const deviceTypeOpts = {
|
const deviceTypeOpts = {
|
||||||
@ -208,7 +215,7 @@ ${dockerignoreHelp}
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async getAppAndResolveArch(opts: FlagsDef) {
|
protected async getAppAndResolveArch(opts: PrepareBuildOpts) {
|
||||||
if (opts.fleet) {
|
if (opts.fleet) {
|
||||||
const { getAppWithArch } = await import('../../utils/helpers');
|
const { getAppWithArch } = await import('../../utils/helpers');
|
||||||
const app = await getAppWithArch(opts.fleet);
|
const app = await getAppWithArch(opts.fleet);
|
||||||
@ -218,7 +225,7 @@ ${dockerignoreHelp}
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async prepareBuild(options: FlagsDef) {
|
protected async prepareBuild(options: PrepareBuildOpts) {
|
||||||
const { getDocker, generateBuildOpts } = await import('../../utils/docker');
|
const { getDocker, generateBuildOpts } = await import('../../utils/docker');
|
||||||
const [docker, buildOpts, composeOpts] = await Promise.all([
|
const [docker, buildOpts, composeOpts] = await Promise.all([
|
||||||
getDocker(options),
|
getDocker(options),
|
||||||
|
Loading…
Reference in New Issue
Block a user