Skip to content

Commit

Permalink
refactor: implement flagsInterceptor for command flag handling across…
Browse files Browse the repository at this point in the history
… multiple commands
  • Loading branch information
msudgh committed Jan 2, 2025
1 parent 6b3065f commit 6ce73f4
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 68 deletions.
3 changes: 2 additions & 1 deletion src/commands/config/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
InitCommandCallback,
InitFlags,
} from '../../types/commands'
import { flagsInterceptor } from '../../utils/command'

/**
* Init command configure an ovm.json config file in user's home dir.
Expand All @@ -31,7 +32,7 @@ export default class Init extends FactoryCommand {
public async run() {
try {
const { args, flags } = await this.parse(Init)
return action(args, this.flagsInterceptor<InitFlags>(flags))
return action(args, flagsInterceptor<FactoryFlags<InitFlags>>(flags))
} catch (error) {
this.handleError(error)
} finally {
Expand Down
8 changes: 6 additions & 2 deletions src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Args, Flags, flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import installService from '../../services/install'
import { InstallFlags } from '../../types/commands'
import { FactoryFlagsWithVaults, InstallFlags } from '../../types/commands'
import { flagsInterceptor } from '../../utils/command'

const { action } = installService

Expand Down Expand Up @@ -43,7 +44,10 @@ export default class Install extends FactoryCommandWithVaults {
public async run(): Promise<void> {
try {
const { args, flags } = await this.parse(Install)
return action(args, this.flagsInterceptor<InstallFlags>(flags))
return action(
args,
flagsInterceptor<FactoryFlagsWithVaults<InstallFlags>>(flags),
)
} catch (error) {
this.handleError(error)
throw error
Expand Down
8 changes: 6 additions & 2 deletions src/commands/plugins/prune.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import pruneService from '../../services/prune'
import { PruneFlags } from '../../types/commands'
import { FactoryFlagsWithVaults, PruneFlags } from '../../types/commands'
import { flagsInterceptor } from '../../utils/command'

const { action } = pruneService

Expand All @@ -28,7 +29,10 @@ export default class Prune extends FactoryCommandWithVaults {
public async run() {
try {
const { args, flags } = await this.parse(Prune)
await action(args, this.flagsInterceptor<PruneFlags>(flags))
await action(
args,
flagsInterceptor<FactoryFlagsWithVaults<PruneFlags>>(flags),
)
} catch (error) {
this.handleError(error)
} finally {
Expand Down
8 changes: 6 additions & 2 deletions src/commands/plugins/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Args, flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import uninstallService from '../../services/uninstall'
import { UninstallFlags } from '../../types/commands'
import { FactoryFlagsWithVaults, UninstallFlags } from '../../types/commands'
import { flagsInterceptor } from '../../utils/command'

const { action } = uninstallService

Expand Down Expand Up @@ -35,7 +36,10 @@ export default class Uninstall extends FactoryCommandWithVaults {
public async run() {
try {
const { args, flags } = await this.parse(Uninstall)
await action(args, this.flagsInterceptor<UninstallFlags>(flags))
await action(
args,
flagsInterceptor<FactoryFlagsWithVaults<UninstallFlags>>(flags),
)
} catch (error) {
this.handleError(error)
} finally {
Expand Down
8 changes: 6 additions & 2 deletions src/commands/reports/stats.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Flags, flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import statsService from '../../services/stats'
import { StatsFlags } from '../../types/commands'
import { FactoryFlagsWithVaults, StatsFlags } from '../../types/commands'
import { flagsInterceptor } from '../../utils/command'

const { action } = statsService

Expand Down Expand Up @@ -34,7 +35,10 @@ export default class Stats extends FactoryCommandWithVaults {
public async run(): Promise<void> {
try {
const { args, flags } = await this.parse(Stats)
return await action(args, this.flagsInterceptor<StatsFlags>(flags))
return await action(
args,
flagsInterceptor<FactoryFlagsWithVaults<StatsFlags>>(flags),
)
} catch (error) {
this.handleError(error)
throw error
Expand Down
8 changes: 6 additions & 2 deletions src/commands/vaults/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Args, Flags, flush } from '@oclif/core'
import { FactoryCommandWithVaults } from '../../providers/command'
import runService from '../../services/run'
import { RunFlags } from '../../types/commands'
import { FactoryFlagsWithVaults, RunFlags } from '../../types/commands'
import { flagsInterceptor } from '../../utils/command'

const { action } = runService

Expand Down Expand Up @@ -66,7 +67,10 @@ export default class Run extends FactoryCommandWithVaults {
public async run(): Promise<void> {
try {
const { args, flags } = await this.parse(Run)
await action(args, this.flagsInterceptor<RunFlags>(flags))
await action(
args,
flagsInterceptor<FactoryFlagsWithVaults<RunFlags>>(flags),
)
} catch (error) {
this.handleError(error)
} finally {
Expand Down
55 changes: 0 additions & 55 deletions src/providers/command.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { Command, Flags } from '@oclif/core'
import { ParserInput } from '@oclif/core/lib/interfaces/parser'
import { exec } from 'child_process'
import { Vault } from 'obsidian-utils'
import { homedir } from 'os'
import path from 'path'
import { FactoryFlags, FactoryFlagsWithVaults } from '../types/commands'
import { handlerCommandError } from '../utils/command'
import {
OVM_CONFIG_FILENAME,
RESERVED_VARIABLES,
VAULTS_PATH_FLAG_DESCRIPTION,
} from '../utils/constants'
import { logger } from '../utils/logger'

const DEFAULT_CONFIG_PATH = path.join(homedir(), OVM_CONFIG_FILENAME)

Expand Down Expand Up @@ -41,33 +38,6 @@ class FactoryCommand extends Command {
throw new Error('Method not implemented.')
}

public enableLoggingTimestamp(timestamp: boolean): void {
process.env.OVM_ENABLE_LOG_TIMESTAMP = timestamp ? '0' : '1'
}

public enableDebugLogLevel(
debug: boolean,
flags: ParserInput['flags'],
): void {
if (debug) {
logger.level = 'debug'
logger.debug(`Command called`, { flags })
}
}

public flagsInterceptor<T>(flags: FactoryFlags<T>): FactoryFlags<T> {
const { debug, timestamp } = flags

this.enableLoggingTimestamp(timestamp)

if (debug) {
logger.level = 'debug'
logger.debug(`Command called`, { flags })
}

return flags
}

public handleError(error: unknown) {
handlerCommandError(error)
}
Expand All @@ -89,31 +59,6 @@ class FactoryCommandWithVaults extends Command {
throw new Error('Method not implemented.')
}

public enableLoggingTimestamp(timestamp: boolean): void {
process.env.OVM_ENABLE_LOG_TIMESTAMP = timestamp ? '0' : '1'
}

public enableDebugLogLevel(
debug: boolean,
flags: ParserInput['flags'],
): void {
if (debug) {
logger.level = 'debug'
logger.debug(`Command called`, { flags })
}
}

public flagsInterceptor<T>(
flags: FactoryFlagsWithVaults<T>,
): FactoryFlagsWithVaults<T> {
const { debug, timestamp } = flags

this.enableLoggingTimestamp(timestamp)
this.enableDebugLogLevel(debug, flags as ParserInput['flags'])

return flags
}

public handleError(error: unknown) {
handlerCommandError(error)
}
Expand Down
12 changes: 11 additions & 1 deletion src/utils/command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { ExitPromptError } from '@inquirer/core'
import { handle } from '@oclif/core'
import { logger } from './logger'
import { CommonFlags } from '../types/commands'
import { enableDebugLogLevel, enableLoggingTimestamp, logger } from './logger'

export const flagsInterceptor = <T extends CommonFlags>(flags: T): T => {
const { debug, timestamp } = flags

enableLoggingTimestamp(timestamp)
enableDebugLogLevel(debug, flags)

return flags
}

export const handlerCommandError = (error: unknown) => {
if (process.env.CI || process.env.CI === 'true') {
Expand Down
20 changes: 19 additions & 1 deletion src/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { tmpdir } from 'os'
import { join } from 'path'
import { createLogger, format, transports } from 'winston'
import { FactoryFlags, FactoryFlagsWithVaults } from '../types/commands'
import {
CommonFlags,
FactoryFlags,
FactoryFlagsWithVaults,
} from '../types/commands'

export const CUSTOM_COMMAND_LOGGER_FILE = join(
tmpdir(),
Expand Down Expand Up @@ -42,3 +46,17 @@ export const customCommandLogger = createLogger({
export const silentCheck = <T>(
flags?: FactoryFlags<T> | FactoryFlagsWithVaults<T>,
) => flags && 'silent' in flags && flags.silent

export const enableLoggingTimestamp = (timestamp: boolean) => {
process.env.OVM_ENABLE_LOG_TIMESTAMP = timestamp ? 'true' : 'false'
}

export const enableDebugLogLevel = (
debug: boolean,
flags: CommonFlags | FactoryFlags<unknown> | FactoryFlagsWithVaults<unknown>,
) => {
if (debug) {
logger.level = 'debug'
logger.debug(`Command called`, { flags })
}
}

0 comments on commit 6ce73f4

Please sign in to comment.