|
1 | 1 | import { Command } from '@contentstack/cli-command'; |
2 | 2 | import { cliux, flags, configHandler, FlagInput, messageHandler } from '@contentstack/cli-utilities'; |
3 | | -import { interactive } from '../../../utils'; |
| 3 | +import { resolveLogPath } from '../../../utils/log-config-defaults'; |
| 4 | +import * as path from 'path'; |
4 | 5 |
|
5 | 6 | export default class LogSetCommand extends Command { |
6 | 7 | static description = 'Set logging configuration for CLI'; |
7 | 8 |
|
8 | 9 | static flags: FlagInput = { |
9 | | - 'level': flags.string({ |
10 | | - description: 'Set the log level for the CLI.', |
| 10 | + level: flags.string({ |
| 11 | + description: 'Set the log level for the CLI. Defaults to "info" if not specified.', |
11 | 12 | options: ['debug', 'info', 'warn', 'error'], |
12 | 13 | }), |
13 | | - 'path': flags.string({ |
14 | | - description: 'Specify the file path where logs should be saved.', |
| 14 | + path: flags.string({ |
| 15 | + description: |
| 16 | + 'Specify the directory path where logs should be saved. Supports both relative and absolute paths. Defaults to ~/.contentstack/logs if not specified.', |
15 | 17 | }), |
16 | 18 | 'show-console-logs': flags.boolean({ |
17 | 19 | description: 'Enable console logging.', |
18 | 20 | allowNo: true, // no-show-console-logs |
19 | 21 | default: false, |
20 | | - }) |
| 22 | + }), |
21 | 23 | }; |
22 | 24 |
|
23 | | - |
24 | 25 | static examples = [ |
25 | 26 | 'csdx config:set:log', |
26 | | - 'csdx config:set:log --level debug --path ./logs/app.log --show-console-logs', |
| 27 | + 'csdx config:set:log --level debug', |
| 28 | + 'csdx config:set:log --path ./logs', |
| 29 | + 'csdx config:set:log --level debug --path ./logs --show-console-logs', |
27 | 30 | 'csdx config:set:log --no-show-console-logs', |
| 31 | + 'csdx config:set:log --level warn --show-console-logs', |
| 32 | + 'csdx config:set:log --path ~/custom/logs', |
| 33 | + 'csdx config:set:log --path /var/log/contentstack', |
28 | 34 | ]; |
29 | 35 |
|
30 | 36 | async run() { |
31 | 37 | try { |
32 | 38 | const { flags } = await this.parse(LogSetCommand); |
| 39 | + const currentLoggingConfig = configHandler.get('log') || {}; |
| 40 | + if (flags['level'] !== undefined) { |
| 41 | + currentLoggingConfig.level = flags['level']; |
| 42 | + } |
33 | 43 |
|
34 | | - let logLevel: string = flags['level']; |
35 | | - let logPath: string = flags['path']; |
36 | | - const showConsoleLogs: boolean = flags['show-console-logs']; |
37 | | - |
38 | | - // Interactive prompts if not passed via flags |
39 | | - if (!logLevel) { |
40 | | - logLevel = await interactive.askLogLevel(); |
| 44 | + if (flags['path'] !== undefined) { |
| 45 | + // Convert to absolute path and ensure it's a directory |
| 46 | + let resolvedPath = resolveLogPath(flags['path']); |
| 47 | + const pathExt = path.extname(resolvedPath); |
| 48 | + if (pathExt && pathExt.length > 0) { |
| 49 | + resolvedPath = path.dirname(resolvedPath); |
| 50 | + } |
| 51 | + |
| 52 | + currentLoggingConfig.path = resolvedPath; |
41 | 53 | } |
42 | 54 |
|
43 | | - if (!logPath) { |
44 | | - logPath = await interactive.askLogPath(); |
| 55 | + if (flags['show-console-logs'] !== undefined) { |
| 56 | + currentLoggingConfig['show-console-logs'] = flags['show-console-logs']; |
45 | 57 | } |
| 58 | + configHandler.set('log', currentLoggingConfig); |
46 | 59 |
|
47 | | - const currentLoggingConfig = configHandler.get('log') || {}; |
48 | | - if (logLevel) currentLoggingConfig.level = logLevel; |
49 | | - if (logPath) currentLoggingConfig.path = logPath; |
50 | | - currentLoggingConfig['show-console-logs'] = showConsoleLogs; |
| 60 | + if (flags['level'] !== undefined) { |
| 61 | + cliux.success(messageHandler.parse('CLI_CONFIG_LOG_LEVEL_SET', currentLoggingConfig.level)); |
| 62 | + } |
51 | 63 |
|
52 | | - configHandler.set('log', currentLoggingConfig); |
| 64 | + if (flags['path'] !== undefined) { |
| 65 | + cliux.success(messageHandler.parse('CLI_CONFIG_LOG_PATH_SET', currentLoggingConfig.path)); |
| 66 | + } |
53 | 67 |
|
54 | | - cliux.success(messageHandler.parse('CLI_CONFIG_LOG_LEVEL_SET', logLevel)); |
55 | | - cliux.success(messageHandler.parse('CLI_CONFIG_LOG_PATH_SET', logPath)); |
56 | | - cliux.success(messageHandler.parse('CLI_CONFIG_LOG_CONSOLE_SET', String(showConsoleLogs))); |
57 | | - cliux.print(messageHandler.parse('CLI_CONFIG_LOG_SET_SUCCESS'), { color: 'green' }); |
| 68 | + if (flags['show-console-logs'] !== undefined) { |
| 69 | + cliux.success( |
| 70 | + messageHandler.parse('CLI_CONFIG_LOG_CONSOLE_SET', String(currentLoggingConfig['show-console-logs'])), |
| 71 | + ); |
| 72 | + } |
58 | 73 | } catch (error) { |
59 | 74 | cliux.error('error', error); |
60 | 75 | } |
|
0 commit comments