import * as async from "../async" ; import * as path from "path" ; import { app } from "electron" ; import { isObject } from "util" ; const ini = require ( 'ini' ) ; const slash = require ( 'slash' ) ; export interface WorkspacePuppetPaths { confdir : string ; ssldir : string ; vardir : string ; rundir : string ; logdir : string ; } export class WorkspaceSettings { public certname : string ; public server : string ; public version : string ; private readonly filePath : string ; private readonly codePath : string ; constructor ( filePath , codePath ) { this . filePath = filePath ; this . codePath = codePath ; } public static GetPaths ( ) { const home = app . getPath ( "home" ) ; const hieraHome = path . join ( home , ".hieraeditor" ) ; return { confdir : slash ( path . join ( hieraHome , "etc" , "puppet" ) ) , ssldir : slash ( path . join ( hieraHome , "etc" , "puppet" , "ssl" ) ) , vardir : slash ( path . join ( hieraHome , "opt" , "puppet" ) ) , rundir : slash ( path . join ( hieraHome , "var" , "run" ) ) , logdir : slash ( path . join ( hieraHome , "var" , "log" ) ) } ; } public async read ( ) { if ( ! await async . isFile ( this . filePath ) ) return false ; let raw ; try { raw = await async . readFile ( this . filePath ) ; } catch ( e ) { return false ; } let data ; try { data = ini . parse ( raw ) ; } catch ( e ) { return false ; } if ( ! isObject ( data ) ) { return true ; } return this . load ( data ) ; } public async isValid ( offline ) { if ( this . certname == null || this . certname == "" ) return false ; if ( this . server == null || this . server == "" ) return false ; if ( ! offline ) { const { ssldir } = WorkspaceSettings . GetPaths ( ) ; if ( ! await async . isFile ( path . join ( ssldir , "certs" , "ca.pem" ) ) ) return false ; if ( ! await async . isFile ( path . join ( ssldir , "certs" , this . certname + ".pem" ) ) ) return false ; if ( ! await async . isFile ( path . join ( ssldir , "private_keys" , this . certname + ".pem" ) ) ) return false ; } return true ; } public reset ( ) { this . certname = null ; this . server = null ; this . version = null ; } private load ( data ) { const main = data [ "main" ] || { } ; this . certname = main [ "certname" ] ; this . server = main [ "server" ] ; this . version = data [ "version" ] ; return true ; } public async write ( ) { const data = this . dump ( ) ; const raw = ini . stringify ( data ) ; await async . writeFile ( this . filePath , raw ) ; } private dump ( ) { const res = { "main" : { "environment" : "production" , "codedir" : this . codePath , "certname" : this . certname , "server" : this . server } } ; if ( this . version ) { res [ "version" ] = this . version ; } return res ; } }