import click
import os

from ..cli import cli_command
from ..utils import load_file, template_string


@cli_command('paas-manifest', max_apps=1)
@click.option('--template', '-t', default='paas/manifest.j2',
              type=click.Path(exists=True),
              help="Manifest Jinja2 template file")
@click.option('--out-file', '-o',
              help="Output file, if empty the template content is printed to the stdout")
def paas_manifest(ctx, template, out_file):
    """Generate a PaaS manifest file from a Jinja2 template"""
    app = ctx.apps[0]

    if app not in ctx.variables:
        raise ValueError('Application configuration not found')

    templace_content = load_file(template)
    variables = {
        'environment': ctx.environment,
        'app': app.replace('_', '-')
    }
    variables.update(ctx.variables[app])

    manifest_content = template_string(templace_content, variables)

    if out_file is not None:
        with open(out_file, 'w') as f:
            f.write(manifest_content)
        os.chmod(out_file, 0o600)
    else:
        print(manifest_content)

import click
import os

from ..cli import cli_command
from ..utils import load_file, template_string, merge_dicts


@cli_command('paas-manifest', max_apps=1)
@click.option('--out-file', '-o',
              help="Output file, if empty the template content is printed to the stdout")
def paas_manifest(ctx, out_file):
    """Generate a PaaS manifest file from a Jinja2 template"""
    app = ctx.apps[0]

    if app not in ctx.variables:
        raise ValueError('Application configuration not found')

    variables = {
        'environment': ctx.environment,
        'app': app.replace('_', '-')
    }

    template_content = load_file('paas/{}.j2'.format(variables['app']))

    variables = merge_dicts(variables, ctx.variables)
    variables = merge_dicts(variables, ctx.variables[app])

    manifest_content = template_string(template_content, variables, templates_path='paas/')

    if out_file is not None:
        with open(out_file, 'w') as f:
            f.write(manifest_content)
        os.chmod(out_file, 0o600)
    else:
        print(manifest_content)

Update paas-manifest command to load per-app manifests
Update paas-manifest command to load per-app manifests

Removes template file option in favour of the app-specific manifests.

Changes the way variables are set for the manifest template. Once
the relevant variable files are loaded and merged the command will
update the top-level namespace with the values from the application.

This allows us to use the same base manifest template referencing
generic top-level variables (eg `subdomain`, `instances`, `path`)
that are overridden by application-specific values. Previously
this was accomplished by using `stacks.yml` as the middle layer.

The template variable change means that we can run into issues if
we use clashing variable names accidentally, but at the same time
it allows us to set common values for all applications. Eg:

```
instances: 3

api:
  instances: 5
```

sets instance counts to 3 for all applications, but since the context
will be updated with the `api` values the api manifest will only see
`instances: 5` value.
