UNPKG

@nu-art/commando

Version:

Shell command execution framework with interactive sessions, CLI parameter resolution, and plugin system for building and executing shell scripts programmatically

109 lines (108 loc) 3.68 kB
/* * commando provides shell command execution framework with interactive sessions and plugin system * * Copyright (C) 2020 Adam van der Kruk aka TacB0sS * * 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 { BaseCommando } from '../core/BaseCommando.js'; /** * Programming constructs plugin for Commando. * * Provides control flow structures for building shell scripts: * - Conditionals (`if`/`else`) * - Loops (`for`, `while`) * - Functions * * These methods build shell script structures with proper indentation * and syntax, allowing programmatic construction of complex shell scripts. */ export class Commando_Programming extends BaseCommando { /** * Constructs an if-else conditional command structure. * @param {string} condition - The condition for the if statement. * @param {CliBlock} ifBlock - Block of commands to execute if the condition is true. * @param {CliBlock} [elseBlock] - Optional block of commands to execute if the condition is false. * @returns {this} - The Cli instance for method chaining. */ if(condition, ifBlock, elseBlock) { this.append(`if ${condition}; then`); this.indentIn(); ifBlock(this); if (elseBlock) { this.indentOut(); this.append('else'); this.indentIn(); elseBlock(this); } this.indentOut(); this.append('fi'); this.emptyLine(); return this; } /** * Constructs a for loop command structure. * @param {string} varName - The variable name used in the loop. * @param {string | string[]} arrayNameOrValues - The array name or array of values to iterate over. * @param {CliBlock} loop - Block of commands to execute in each iteration of the loop. * @returns {this} - The Cli instance for method chaining. */ for(varName, arrayNameOrValues, loop) { if (typeof arrayNameOrValues === 'string') { this.append(`for ${varName} in "\${${arrayNameOrValues}[@]}"; do`); } else { const values = arrayNameOrValues.map(value => `"${value}"`).join(' '); this.append(`for ${varName} in ${values}; do`); } this.indentIn(); loop(this); this.indentOut(); this.append('done'); this.emptyLine(); return this; } /** * Appends a 'continue' command for loop control. * * Skips to the next iteration of the innermost loop. * * @returns This instance for method chaining */ continue() { this.append('continue'); return this; } /** * Appends a 'break' command for loop control. * * Exits the innermost loop. * * @returns This instance for method chaining */ break() { this.append('break'); return this; } /** * Appends a 'return' command for exiting a function or script. * * Exits the current function or script with optional exit code. * * @returns This instance for method chaining */ return() { this.append('return'); return this; } }