2020-03-03 22:47:51 +00:00
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 ( ) {
const path = await this . _path ( )
const permissions = ` ${ await path . permissions ( ) } ` . trim ( )
const target = ` ${ this . _config . level } ` . trim ( )
return permissions === target
}
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
}
2020-04-15 14:11:10 +00:00
failure _message ( ) {
return ` The resource permissions for " ${ this . _config . path } " on host " ${ this . _host . name } " are invalid. `
}
check _message ( ) {
return this . failure _message ( )
}
2020-03-03 22:47:51 +00:00
}
module . exports = exports = PermissionState