You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
maestro/app/classes/state/fs/PermissionState.js

55 lines
1.6 KiB

const State = require('../State')
class PermissionState extends State {
static get services() {
return [...super.services, 'output']
}
async apply() {
if ( !(await this.check()) ) {
const path = await this._path()
await path.permissions(this._config.level, !!this._config.recursive)
}
}
async check() {
try {
const path = await this._path()
const permissions = `${await path.permissions()}`.trim()
const target = `${this._config.level}`.trim()
return permissions === target
} catch (e) {}
return false
}
async reverse() {
if ( this._config.revert_to ) {
const path = await this._path()
await path.permissions(this._config.revert_to, this._config.recursive)
} else {
this.output.warn(`Permission state does not support automatic reversal. Specify the revert_to config for this functionality. (Host: ${this._host.name})`)
}
}
async _path() {
const path = await this._host.get_path(this._config.path)
await path.classify()
if ( !path.is_valid() ) throw new Error(`Invalid path for PermissionState: ${path}`)
return path
}
failure_message() {
return `The resource permissions for "${this._config.path}" on host "${this._host.name}" are invalid.`
}
check_message() {
return this.failure_message()
}
display() {
return `Set filesystem permissions of resource ${this._config.path}...`
}
}
module.exports = exports = PermissionState