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.
65 lines
1.9 KiB
65 lines
1.9 KiB
const State = require('../State')
|
|
const UniversalPath = require('../../logical/UniversalPath')
|
|
|
|
class FileContentState extends State {
|
|
static get services() {
|
|
return [...super.services, 'hosts']
|
|
}
|
|
|
|
async apply() {
|
|
const target_path = await this._target()
|
|
if ( target_path.is_valid() ) {
|
|
|
|
}
|
|
|
|
if ( this._config.source ) {
|
|
const source_path = await this._source()
|
|
await source_path.copy_to(target_path)
|
|
} else {
|
|
await target_path.echo(this._config.contents)
|
|
}
|
|
}
|
|
|
|
async check() {
|
|
if ( this._config.source ) {
|
|
const source_path = await this._source()
|
|
const source_hash = await source_path.hash()
|
|
|
|
const target_path = await this._target()
|
|
if ( !target_path.is_file() ) return false
|
|
const target_hash = await target_path.hash()
|
|
|
|
return source_hash === target_hash
|
|
} else {
|
|
const localhost = this.hosts.get('localhost')
|
|
const temp_path = await localhost.get_temp_file()
|
|
await temp_path.echo(this._config.contents)
|
|
const source_hash = await temp_path.hash()
|
|
|
|
const target_path = await this._target()
|
|
if ( !target_path.is_file() ) return false
|
|
const target_hash = await target_path.hash()
|
|
|
|
return source_hash === target_hash
|
|
}
|
|
}
|
|
|
|
async reverse() {
|
|
await (await this._target()).echo('')
|
|
}
|
|
|
|
async _target() {
|
|
if ( typeof this._config.target === 'string' ) {
|
|
return UniversalPath.fromString(this._config.target)
|
|
} else return this._config.target
|
|
}
|
|
|
|
async _source() {
|
|
if ( typeof this._config.target === 'string' ) {
|
|
return UniversalPath.fromString(this._config.target)
|
|
} else return this._config.target
|
|
}
|
|
}
|
|
|
|
module.exports = exports = FileContentState
|