maestro/app/classes/state/fs/FileState.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2020-02-21 06:36:55 +00:00
const State = require('../State')
class FileState extends State {
async apply() {
if ( !(await this.check()) ) {
const path = await this._path()
await path.touch()
if ( this._config.contents ) {
await path.echo(this._config.contents)
} else if ( this._config.template ) {
await path.copy_from(this._config.template)
}
2020-02-21 06:36:55 +00:00
}
}
async check() {
const path = await this._path()
await path.classify()
return path.is_file()
}
async reverse() {
if ( await this.check() ) {
const path = await this._path()
await path.unlink()
}
}
async _path() {
2020-03-03 22:10:10 +00:00
if ( !this._config.path ) throw new Error('Missing path config for FileState.')
2020-02-21 06:36:55 +00:00
return this._host.get_path(this._config.path)
}
failure_message() {
return `The file "${this._config.path}" does not exist on the host "${this._host.name}."`
}
check_message() {
return this.failure_message()
}
2020-02-21 06:36:55 +00:00
}
module.exports = exports = FileState