import * as chalk from 'chalk'; import {command, help, namespace, param, required, option, defaultValue} from 'oo-cli'; import {default as parseDuration} from 'parse-duration'; import {appContext} from '../../lib/AppContext'; import {die} from '../../lib/die'; import {Rivendell} from '../../lib/Rivendell'; import {formatError} from '../../lib/formatError'; import LogLevel = Rivendell.LogLevel; @namespace('app') export class SetLogLevelCommand { @param @required @help('The App ID and version') public appVersion!: string; @param @required @help(`The desired logLevel to set. Possible values: ${Object.values(LogLevel).join(', ')}`) public logLevel!: string; @option('trackerId') @help('The Tracker ID of the installed account (required for developers). If not provided, log level is set for the app version') public trackerId?: string; @option('ttl') @help('LogLevel expiration period (TTL) as relative string (i.e. "5m" for 5 minutes). Default: 2h') @defaultValue('2h') public timeToLive!: string; @option('a') @help('The availability zone that will be targeted (default: us)') private availability: string = ''; @command('set-log-level') @help('Set the logLevel of a particular app version or an installation') public async setLogLevel() { try { const context = appContext(this.appVersion); if (!context.appId || !context.version) { return die('App ID and version are required'); } if (!this.trackerId && !await Rivendell.isAdmin()) { die('Tracker ID must be specified for developer role.'); } const level = this.getLogLevel(this.logLevel); const ttlSeconds = parseDuration(this.timeToLive, 'second'); if (!ttlSeconds) { die(chalk.red('Invalid "--ttl" input, should be relative string (i.e. "5m" for 5 minutes.')); } await Rivendell.setLogLevel( this.availability, context.appId, context.version, level.toString(), ttlSeconds!, this.trackerId); if (this.trackerId) { console.log(chalk.green(`Successfully set log level for tracker ${this.trackerId} of the app version ${this.appVersion}`)); } else { console.log(chalk.green(`Successfully set log level for the app version ${this.appVersion}`)); } } catch (e: any) { die(formatError(e)); } } private getLogLevel(logLevel: string): LogLevel { const key = logLevel.toUpperCase(); if (!LogLevel[key as LogLevel]) { die(`${logLevel} is not a valid LogLevel`); } return LogLevel[key as LogLevel]; } }