Skip to content
Closed
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
21 changes: 2 additions & 19 deletions api/docs/public/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ unraid-api start [--log-level <level>]
Starts the Unraid API service.

Options:

- `--log-level`: Set logging level (trace|debug|info|warn|error|fatal)

Alternative: You can also set the log level using the `LOG_LEVEL` environment variable:

```bash
LOG_LEVEL=trace unraid-api start
```
- `--log-level`: Set logging level (trace|debug|info|warn|error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Document all supported levels (missing fatal).

Code accepts fatal (see levels), but docs omit it. Add for accuracy.

-- `--log-level`: Set logging level (trace|debug|info|warn|error)
+- `--log-level`: Set logging level (trace|debug|info|warn|error|fatal)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
- `--log-level`: Set logging level (trace|debug|info|warn|error)
- `--log-level`: Set logging level (trace|debug|info|warn|error|fatal)
🤖 Prompt for AI Agents
In api/docs/public/cli.md around line 24, the documented --log-level values omit
the supported "fatal" level; update the line to list all supported levels
exactly as accepted by the code (e.g., trace|debug|info|warn|error|fatal) so the
docs match implementation.


### Stop

Expand All @@ -43,21 +36,11 @@ Stops the Unraid API service.
### Restart

```bash
unraid-api restart [--log-level <level>]
unraid-api restart
```

Restarts the Unraid API service.

Options:

- `--log-level`: Set logging level (trace|debug|info|warn|error|fatal)

Alternative: You can also set the log level using the `LOG_LEVEL` environment variable:

```bash
LOG_LEVEL=trace unraid-api restart
```

### Logs

```bash
Expand Down
32 changes: 4 additions & 28 deletions api/src/unraid-api/cli/restart.command.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import { Command, CommandRunner, Option } from 'nest-commander';
import { Command, CommandRunner } from 'nest-commander';

import type { LogLevel } from '@app/core/log.js';
import { levels } from '@app/core/log.js';
import { ECOSYSTEM_PATH, LOG_LEVEL } from '@app/environment.js';
import { ECOSYSTEM_PATH } from '@app/environment.js';
import { LogService } from '@app/unraid-api/cli/log.service.js';
import { PM2Service } from '@app/unraid-api/cli/pm2.service.js';

export interface LogLevelOptions {
logLevel?: LogLevel;
}

export function parseLogLevelOption(val: string, allowedLevels: string[] = [...levels]): LogLevel {
const normalized = val.toLowerCase() as LogLevel;
if (allowedLevels.includes(normalized)) {
return normalized;
}
throw new Error(`Invalid --log-level "${val}". Allowed: ${allowedLevels.join(', ')}`);
}

@Command({ name: 'restart', description: 'Restart the Unraid API' })
export class RestartCommand extends CommandRunner {
constructor(
Expand All @@ -27,12 +13,11 @@ export class RestartCommand extends CommandRunner {
super();
}

async run(_?: string[], options: LogLevelOptions = {}): Promise<void> {
async run(): Promise<void> {
try {
this.logger.info('Restarting the Unraid API...');
const env = { LOG_LEVEL: options.logLevel };
const { stderr, stdout } = await this.pm2.run(
{ tag: 'PM2 Restart', raw: true, extendEnv: true, env },
{ tag: 'PM2 Restart', raw: true },
'restart',
ECOSYSTEM_PATH,
'--update-env'
Expand All @@ -55,13 +40,4 @@ export class RestartCommand extends CommandRunner {
process.exit(1);
}
}

@Option({
flags: `--log-level <${levels.join('|')}>`,
description: 'log level to use',
defaultValue: LOG_LEVEL.toLowerCase(),
})
parseLogLevel(val: string): LogLevel {
return parseLogLevelOption(val);
}
}
23 changes: 15 additions & 8 deletions api/src/unraid-api/cli/start.command.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Command, CommandRunner, Option } from 'nest-commander';

import type { LogLevel } from '@app/core/log.js';
import type { LogLevelOptions } from '@app/unraid-api/cli/restart.command.js';
import { levels } from '@app/core/log.js';
import { ECOSYSTEM_PATH, LOG_LEVEL } from '@app/environment.js';
import { ECOSYSTEM_PATH } from '@app/environment.js';
import { LogService } from '@app/unraid-api/cli/log.service.js';
import { PM2Service } from '@app/unraid-api/cli/pm2.service.js';
import { parseLogLevelOption } from '@app/unraid-api/cli/restart.command.js';

interface StartCommandOptions {
'log-level'?: string;
}

@Command({ name: 'start', description: 'Start the Unraid API' })
export class StartCommand extends CommandRunner {
Expand All @@ -25,12 +27,17 @@ export class StartCommand extends CommandRunner {
await this.pm2.run({ tag: 'PM2 Delete' }, 'delete', ECOSYSTEM_PATH);
}

async run(_: string[], options: LogLevelOptions): Promise<void> {
async run(_: string[], options: StartCommandOptions): Promise<void> {
this.logger.info('Starting the Unraid API');
await this.cleanupPM2State();
const env = { LOG_LEVEL: options.logLevel };

const env: Record<string, string> = {};
if (options['log-level']) {
env.LOG_LEVEL = options['log-level'];
}
Comment on lines +34 to +37
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Only inject LOG_LEVEL when the flag is provided.

With defaultValue: 'info', LOG_LEVEL is always set, which contradicts the intent to inject only when explicitly provided.

-        if (options['log-level']) {
+        if (typeof options['log-level'] !== 'undefined') {
             env.LOG_LEVEL = options['log-level'];
         }

And remove the default to ensure absence when not passed:

 @Option({
   flags: `--log-level <${levels.join('|')}>`,
   description: 'log level to use',
-  defaultValue: 'info',
 })

Also applies to: 57-57

🤖 Prompt for AI Agents
In api/src/unraid-api/cli/start.command.ts around lines 34-37 (and similarly at
line 57), the code always sets LOG_LEVEL because the flag was given a default
('info'); remove the defaultValue from the flag definition so the option is
undefined when not passed, and change the injection check to only set
env.LOG_LEVEL when the option is present (e.g., test options['log-level'] !==
undefined or 'log-level' in options) so LOG_LEVEL is only added to env when the
user explicitly provides the flag; apply the same removal of default and
conditional check at the other occurrence at line 57.


const { stderr, stdout } = await this.pm2.run(
{ tag: 'PM2 Start', raw: true, extendEnv: true, env },
{ tag: 'PM2 Start', env, raw: true },
'start',
ECOSYSTEM_PATH,
'--update-env'
Expand All @@ -47,9 +54,9 @@ export class StartCommand extends CommandRunner {
@Option({
flags: `--log-level <${levels.join('|')}>`,
description: 'log level to use',
defaultValue: LOG_LEVEL.toLowerCase(),
defaultValue: 'info',
})
parseLogLevel(val: string): LogLevel {
return parseLogLevelOption(val);
return levels.includes(val as LogLevel) ? (val as LogLevel) : 'info';
}
}
Loading