Start adding state classes; update dependencies
This commit is contained in:
@@ -5,6 +5,8 @@ class DirectoryState extends State {
|
||||
if ( !(await this.check()) ) {
|
||||
const path = await this._path()
|
||||
await path.touch(true)
|
||||
|
||||
// TODO directories
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
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
|
||||
@@ -5,6 +5,12 @@ class FileState extends State {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user