Initial Commit

This commit is contained in:
garrettmills
2020-02-21 00:36:55 -06:00
commit e2016069f2
40 changed files with 4902 additions and 0 deletions

View File

@@ -0,0 +1,29 @@
const State = require('../State')
class DirectoryState extends State {
async apply() {
if ( !(await this.check()) ) {
const path = await this._path()
await path.touch(true)
}
}
async check() {
const path = await this._path()
await path.classify()
return path.is_directory()
}
async reverse() {
if ( await this.check() ) {
const path = await this._path()
await path.unlink()
}
}
async _path() {
return this._host.get_path(this._config.path)
}
}
module.exports = exports = DirectoryState

View File

@@ -0,0 +1,64 @@
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

View File

@@ -0,0 +1,29 @@
const State = require('../State')
class FileState extends State {
async apply() {
if ( !(await this.check()) ) {
const path = await this._path()
await path.touch()
}
}
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() {
return this._host.get_path(this._config.path)
}
}
module.exports = exports = FileState