@hashgraph/solo
Version:
An opinionated CLI tool to deploy and manage private Hedera Networks.
200 lines • 7.42 kB
JavaScript
// SPDX-License-Identifier: Apache-2.0
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var __param = (this && this.__param) || function (paramIndex, decorator) {
return function (target, key) { decorator(target, key, paramIndex); }
};
var HelmExecutionBuilder_1;
import { HelmExecution } from './helm-execution.js';
import { inject, injectable } from 'tsyringe-neo';
import { InjectTokens } from '../../../core/dependency-injection/inject-tokens.js';
import { patchInject } from '../../../core/dependency-injection/container-helper.js';
import * as constants from '../../../core/constants.js';
import { ExecutionBuilder } from '../../execution-builder.js';
let HelmExecutionBuilder = class HelmExecutionBuilder extends ExecutionBuilder {
static { HelmExecutionBuilder_1 = this; }
logger;
helmInstallationDirectory;
static NAME_MUST_NOT_BE_NULL = 'name must not be null';
static VALUE_MUST_NOT_BE_NULL = 'value must not be null';
/**
* The path to the helm executable.
*/
helmExecutable;
/**
* The list of subcommands to be used when execute the helm command.
*/
_subcommands = [];
/**
* The arguments to be passed to the helm command.
*/
_arguments = new Map();
/**
* The list of options and a list of their one or more values.
*/
_optionsWithMultipleValues = [];
/**
* The flags to be passed to the helm command.
*/
_flags = [];
/**
* The positional arguments to be passed to the helm command.
*/
_positionals = [];
/**
* The environment variables to be set when executing the helm command.
*/
_environmentVariables = new Map();
/**
* Creates a new HelmExecutionBuilder instance.
*/
constructor(logger, helmInstallationDirectory) {
super();
this.logger = logger;
this.helmInstallationDirectory = helmInstallationDirectory;
this.logger = patchInject(logger, InjectTokens.SoloLogger, this.constructor.name);
this.helmInstallationDirectory = patchInject(helmInstallationDirectory, InjectTokens.HelmInstallationDirectory, this.constructor.name);
try {
this.helmExecutable = constants.HELM;
}
catch (error) {
this.logger?.error('Failed to find helm executable:', error);
throw new Error('Failed to find helm executable. Please ensure helm is installed and in your PATH.');
}
}
/**
* Adds the list of subcommands to the helm execution.
* @param commands the list of subcommands to be added
* @returns this builder
*/
subcommands(...commands) {
if (!commands) {
throw new Error('commands must not be null');
}
this._subcommands.push(...commands);
return this;
}
/**
* Adds an argument to the helm execution.
* @param name the name of the argument
* @param value the value of the argument
* @returns this builder
*/
argument(name, value) {
if (!name) {
throw new Error(HelmExecutionBuilder_1.NAME_MUST_NOT_BE_NULL);
}
if (!value) {
throw new Error(HelmExecutionBuilder_1.VALUE_MUST_NOT_BE_NULL);
}
this._arguments.set(name, value);
return this;
}
/**
* Adds an option with multiple values to the helm execution.
* @param name the name of the option
* @param value the list of values for the option
* @returns this builder
*/
optionsWithMultipleValues(name, value) {
if (!name) {
throw new Error(HelmExecutionBuilder_1.NAME_MUST_NOT_BE_NULL);
}
if (!value) {
throw new Error(HelmExecutionBuilder_1.VALUE_MUST_NOT_BE_NULL);
}
this._optionsWithMultipleValues.push({ key: name, value });
return this;
}
/**
* Adds a positional argument to the helm execution.
* @param value the value of the positional argument
* @returns this builder
*/
positional(value) {
if (!value) {
throw new Error(HelmExecutionBuilder_1.VALUE_MUST_NOT_BE_NULL);
}
this._positionals.push(value);
return this;
}
/**
* Adds an environment variable to the helm execution.
* @param name the name of the environment variable
* @param value the value of the environment variable
* @returns this builder
*/
environmentVariable(name, value) {
if (!name) {
throw new Error(HelmExecutionBuilder_1.NAME_MUST_NOT_BE_NULL);
}
if (!value) {
throw new Error(HelmExecutionBuilder_1.VALUE_MUST_NOT_BE_NULL);
}
this._environmentVariables.set(name, value);
return this;
}
/**
* Adds a flag to the helm execution.
* @param flag the flag to be added
* @returns this builder
*/
flag(flag) {
if (!flag) {
throw new Error('flag must not be null');
}
this._flags.push(flag);
return this;
}
/**
* Builds the HelmExecution instance.
* @returns the HelmExecution instance
*/
build() {
const command = this.buildCommand();
const environment = { ...process.env };
for (const [key, value] of this._environmentVariables.entries()) {
environment[key] = value;
}
this.prefixPath(environment, this.helmInstallationDirectory);
return new HelmExecution(command, environment, this.logger);
}
/**
* Builds the command array for the helm execution.
* @returns the command array
*/
buildCommand() {
const command = [this.helmExecutable, ...this._subcommands, ...this._flags];
for (const [key, value] of this._arguments.entries()) {
command.push(`--${key}`, value);
}
for (const entry of this._optionsWithMultipleValues) {
for (const value of entry.value) {
command.push(`--${entry.key}`, value);
}
}
command.push(...this._positionals);
const redactedCommand = HelmExecution.redactCommand(command);
this.logger.debug(`Helm command: helm ${redactedCommand.slice(1).join(' ')}`);
return command;
}
};
HelmExecutionBuilder = HelmExecutionBuilder_1 = __decorate([
injectable()
/**
* A builder for creating a helm command execution.
*/
,
__param(0, inject(InjectTokens.SoloLogger)),
__param(1, inject(InjectTokens.HelmInstallationDirectory)),
__metadata("design:paramtypes", [Object, String])
], HelmExecutionBuilder);
export { HelmExecutionBuilder };
//# sourceMappingURL=helm-execution-builder.js.map