~mclehman/guestctl

ref: df04e7d259d451c65a6d0716b82af0ada5ce249a guestctl/lib/App/Guestctl/Templates.pm6 -rw-r--r-- 1.6 KiB View raw
df04e7d2 — 0xFORDCOMMA Fix typo in manage-prompt comment. 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
unit module App::Guestctl::Templates;

use App::Guestctl::Utils;
use Template::Mustache;

our %builtin-templates =
    arch => {
        base_image => "base/archlinux",
        package_manager => "pacman",
        package_manager_update => "-Sy",
        package_manager_install => "-S",
        noconfirm => "--noconfirm",
        ssh_server => "openssh",
        aux_packages => "audit"
    },
    ubuntu => {
        base_image => "ubuntu",
        package_manager => "apt",
        package_manager_update => "update",
        package_manager_install => "install",
        noconfirm => "-y",
        ssh_server => "ssh",
        aux_dockerfile => "RUN mkdir -p /var/run/sshd"
    };

sub gen-template-vars(@prompt-entries, $guest) is export {
    my %responses = manage-prompt @prompt-entries;

    # Construct a template by merging a builtin template with the prompted
    # input as well as the new guest handle.
    my %template-vars = |%builtin-templates{%responses<base>},
                        |%responses,
                        :$guest;

    return %template-vars;
}

sub load-template-files($template-dir) is export {
    return ("Dockerfile"         => %?RESOURCES<templates/dockerfile.tmpl>.slurp,
            "docker-compose.yml" => %?RESOURCES<templates/docker-compose.tmpl>.slurp,
            "ssh/sshd_config"    => %?RESOURCES<templates/sshd_config.tmpl>.slurp);
}

sub instantiate-template-files(@template-files, %variables, $guest-dir) is export {
    for @template-files -> (:key($dest-file), :value($template)) {
        $guest-dir.add($dest-file).spurt: Template::Mustache.render($template, %variables);
    }
}