parent
2725f9eac2
commit
6f1de65602
@ -0,0 +1,44 @@
|
||||
const State = require('../State')
|
||||
|
||||
class OwnerState extends State {
|
||||
static get services() {
|
||||
return [...super.services, 'output']
|
||||
}
|
||||
|
||||
async apply() {
|
||||
if ( !(await this.check()) ) {
|
||||
const path = await this._path()
|
||||
await path.ownership(this._config.owners, !!this._config.recursive)
|
||||
}
|
||||
}
|
||||
|
||||
async check() {
|
||||
const path = await this._path()
|
||||
const owners = await path.ownership()
|
||||
|
||||
if ( this._config.owners.user && owners.user !== this._config.owners.user ) return false
|
||||
else if ( this._config.owners.group && owners.group !== this._config.owners.group ) return false
|
||||
return true
|
||||
}
|
||||
|
||||
async reverse() {
|
||||
if ( await this.check() ) {
|
||||
if (this._config.revert_to) {
|
||||
const path = await this._path()
|
||||
await path.ownership(this._config.revert_to, !!this._config.recursive)
|
||||
} else {
|
||||
this.output.warn(`Owner state does not support automatic reversal. Specify the revert_to config key 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 OwnerState: ${path}`)
|
||||
return path
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = exports = OwnerState
|
@ -0,0 +1,39 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = exports = PermissionState
|
Loading…
Reference in new issue