]> git.wincent.com - wincent.git/blobdiff - src/Fig/operations/template.ts
chore(fig): turn on more TypeScript checks
[wincent.git] / src / Fig / operations / template.ts
index 04c81d544706852f8aae0ac3f0a645c2004ee565..44334db6dfd64b7dd77687b4ea4a8987da811588 100644 (file)
@@ -1,65 +1,65 @@
-import * as fs from 'fs';
-
 import ErrorWithMetadata from '../../ErrorWithMetadata';
+import chown from '../../chown';
 import {log} from '../../console';
 import expand from '../../expand';
-import run from '../../run';
 import tempfile from '../../tempfile';
-import {compile, fill} from '../../template';
 import Context from '../Context';
 import compare from '../compare';
 
 export default async function template({
-  force,
-  group,
-  mode,
-  owner,
-  path,
-  src,
-  variables = {},
-}: {
-  force?: boolean;
-  group?: string;
-  path: string;
-  mode?: Mode;
-  owner?: string;
-  src: string;
-  variables: Variables;
-}): Promise<void> {
-  const target = expand(path);
-
-  const contents = (await Context.compile(src)).fill({variables});
-
-  const diff = await compare({
-    contents,
     force,
     group,
     mode,
     owner,
-    path: target,
-    state: 'file',
-  });
+    path,
+    src,
+    variables = {},
+}: {
+    force?: boolean;
+    group?: string;
+    path: string;
+    mode?: Mode;
+    owner?: string;
+    src: string;
+    variables: Variables;
+}): Promise<void> {
+    const target = expand(path);
 
-  if (owner && owner !== Context.attributes.username) {
-    log.notice(`needs sudo: ${Context.attributes.username} -> ${owner}`);
-    const passphrase = await Context.sudoPassphrase;
-    const result = await run('ls', ['-l', '/var/audit'], {passphrase});
+    const contents = (await Context.compile(src)).fill({variables});
 
-    if (result.status !== 0) {
-      throw new ErrorWithMetadata(`Failed command`, {
-        ...result,
-        error: result.error?.toString() ?? null,
-      });
-    }
-  } else {
-    if (diff.contents) {
-      log.info('change!');
-      const temp = await tempfile(contents);
+    const diff = await compare({
+        contents,
+        force,
+        group,
+        mode,
+        owner,
+        path: target,
+        state: 'file',
+    });
 
-      // TODO: cp from temp to target
-      // TODO: deal with group/owner/mode etc
+    if (diff.state === 'file') {
+        // TODO: file does not exist — have to create it
+    } else if (diff.owner || diff.group) {
+        const result = await chown(target, {group, owner, sudo: true});
+
+        if (result instanceof Error) {
+            throw new ErrorWithMetadata(`Failed command`, {
+                error: result.toString(),
+                group: group ?? null,
+                owner: owner ?? null,
+                target,
+            });
+        }
     } else {
-      Context.informOk(`template ${path}`);
+        if (diff.contents) {
+            // log.info('change!');
+            const temp = await tempfile(contents);
+            log.debug(`Wrote to temporary file: ${temp}`);
+
+            // TODO: cp from temp to target
+            // TODO: deal with group/owner/mode etc
+        } else {
+            Context.informOk(`template ${path}`);
+        }
     }
-  }
 }