Skip to content

Commit

Permalink
style: attempt to fix codacy errors
Browse files Browse the repository at this point in the history
Signed-off-by: Lenin Mehedy <[email protected]>
  • Loading branch information
leninmehedy committed Feb 22, 2024
1 parent 19a7407 commit dc5af56
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/commands/base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ export class BaseCommand extends ShellRunner {

let error = null
try {
this.logger.debug(`==== Start: '${argv._.join(' ')}' ===`)
this.logger.debug(`==== Start: '${argv._.join(' ')}' ===`, { config: this.configManager.config, argv })
await ConfigManager.acquireProcessLock(this.logger)
await handleFunc(argv)
} catch (e) {
error = new FullstackTestingError(`Error occurred: ${e.message}`, e)
} finally {
await ConfigManager.releaseProcessLock(this.logger)
this.logger.debug(`==== End: '${argv._.join(' ')}' ===`)
this.logger.debug(`==== End: '${argv._.join(' ')}' ===`, { config: this.configManager.config, argv })
}

if (error) {
Expand Down
25 changes: 14 additions & 11 deletions src/core/config_manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class ConfigManager {
case 'string':
if (flag.name === flags.chartDirectory.name || flag.name === flags.cacheDir.name) {
this.logger.debug(`Resolving directory path for '${flag.name}': ${val}`)
val = path.resolve(val)
val = path.resolve(path.normalize(val))
}
this.logger.debug(`Setting flag '${flag.name}' of type '${flag.definition.type}': ${val}`)
this.config.flags[flag.name] = `${val}` // force convert to string
Expand Down Expand Up @@ -156,6 +156,8 @@ export class ConfigManager {
if (persist) {
this.persist()
}

this.logger.debug('Updated cached config', { argv, config: this.config })
}
}

Expand Down Expand Up @@ -235,18 +237,18 @@ export class ConfigManager {
*/
static acquireProcessLock (logger) {
const pid = process.pid.toString()

if (!fs.existsSync(constants.SOLO_PID_FILE)) {
fs.writeFileSync(constants.SOLO_PID_FILE, pid)
const pidFile = path.normalize(constants.SOLO_PID_FILE)
if (!fs.existsSync(pidFile)) {

Check warning on line 241 in src/core/config_manager.mjs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/core/config_manager.mjs#L241

The application dynamically constructs file or path information.
fs.writeFileSync(pidFile, pid)

Check warning on line 242 in src/core/config_manager.mjs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/core/config_manager.mjs#L242

The application dynamically constructs file or path information.
logger.debug(`Acquired process lock '${pid}'`, {
pidFile: constants.SOLO_PID_FILE,
pidFile,
pid
})
return true
}

// pid lock exists
const existingPid = fs.readFileSync(constants.SOLO_PID_FILE).toString()
const existingPid = fs.readFileSync(pidFile).toString()

Check warning on line 251 in src/core/config_manager.mjs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/core/config_manager.mjs#L251

The application dynamically constructs file or path information.
logger.showUserError(new FullstackTestingError(`Process lock exists: ${constants.SOLO_PID_FILE}` +
`\nEnsure process '${existingPid}' is not running [ ps -p ${existingPid} ]`))
process.exit(1)
Expand All @@ -262,20 +264,21 @@ export class ConfigManager {
* @return {Promise<void>}
*/
static releaseProcessLock (logger) {
if (fs.existsSync(constants.SOLO_PID_FILE)) {
const existingPid = fs.readFileSync(constants.SOLO_PID_FILE).toString()
const pidFile = path.normalize(constants.SOLO_PID_FILE)
if (fs.existsSync(pidFile)) {

Check warning on line 268 in src/core/config_manager.mjs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/core/config_manager.mjs#L268

The application dynamically constructs file or path information.
const existingPid = fs.readFileSync(pidFile).toString()
const pid = process.pid.toString()

if (existingPid === process.pid.toString()) {
logger.debug(`Releasing process lock '${pid}'`, {
pidFile: constants.SOLO_PID_FILE,
pidFile,
pid
})

fs.rmSync(constants.SOLO_PID_FILE)
fs.rmSync(pidFile)
} else {
logger.warn(`Unable to release process lock '${pid}'.\nEnsure process '${existingPid}' is not running [ps -p ${existingPid}]`, {
pidFile: constants.SOLO_PID_FILE,
pidFile,
pid,
existingPid
})
Expand Down
16 changes: 10 additions & 6 deletions src/core/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,24 @@ import * as path from 'path'
import { fileURLToPath } from 'url'
import chalk from 'chalk'

function sanitizePath (p) {
return path.resolve(path.normalize(p))

Check warning on line 24 in src/core/constants.mjs

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/core/constants.mjs#L24

Detected possible user input going into a `path.join` or `path.resolve` function.
}

// -------------------- solo related constants ---------------------------------------------------------------------
export const CUR_FILE_DIR = path.dirname(fileURLToPath(import.meta.url))
export const USER = `${process.env.USER}` // force it to be a string
export const USER_SANITIZED = USER.replace(/[\W_]+/g, '-')
export const SOLO_HOME_DIR = path.join(process.env.HOME, '.solo')
export const SOLO_LOGS_DIR = path.join(SOLO_HOME_DIR, 'logs')
export const SOLO_HOME_DIR = sanitizePath(path.join(process.env.HOME, '.solo'))
export const SOLO_LOGS_DIR = sanitizePath(path.join(SOLO_HOME_DIR, 'logs'))

export const SOLO_PID_FILE = path.join(SOLO_HOME_DIR, 'solo.pid')
export const SOLO_CACHE_DIR = path.join(SOLO_HOME_DIR, 'cache')
export const SOLO_PID_FILE = sanitizePath(path.join(SOLO_HOME_DIR, 'solo.pid'))
export const SOLO_CACHE_DIR = sanitizePath(path.join(SOLO_HOME_DIR, 'cache'))
export const DEFAULT_NAMESPACE = 'default'
export const HELM = 'helm'
export const CWD = process.cwd()
export const SOLO_CONFIG_FILE = path.join(SOLO_HOME_DIR, 'solo.config')
export const RESOURCES_DIR = path.normalize(path.join(CUR_FILE_DIR, '/../../resources'))
export const SOLO_CONFIG_FILE = sanitizePath(path.join(SOLO_HOME_DIR, 'solo.config'))
export const RESOURCES_DIR = sanitizePath(path.join(CUR_FILE_DIR, '/../../resources'))

export const ROOT_CONTAINER = 'root-container'

Expand Down

0 comments on commit dc5af56

Please sign in to comment.