"use strict";
/*
 * Copyright 2020 Unbounded Systems, LLC
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const debug_1 = tslib_1.__importDefault(require("debug"));
const execa_1 = tslib_1.__importDefault(require("execa"));
const common_1 = require("../common");
const debug = debug_1.default("adapt:cloud:gcloud");
async function cloudRunDescribe(config) {
    try {
        const result = await execGCloud([
            "run",
            "services",
            "describe",
            "--platform=managed",
            "--format=json",
            `--region=${config.region}`,
            config.name
        ], config.globalOpts);
        return JSON.parse(result.stdout);
    }
    catch (e) {
        if (common_1.isExecaError(e) && e.exitCode !== 0 && e.stderr.match(/Cannot find service/)) {
            return undefined;
        }
        throw e;
    }
}
exports.cloudRunDescribe = cloudRunDescribe;
async function cloudRunDeploy(config) {
    const env = config.env;
    const args = config.args;
    // Need better escaping here.  See Issue #221
    const envString = Object.entries(env).map(([k, v]) => `${k}=${v}`).join(",");
    const argsString = args.join(",");
    const authArg = config.allowUnauthenticated
        ? "--allow-unauthenticated"
        : "--no-allow-unauthenticated";
    const gcargs = [
        "run",
        "deploy",
        config.name,
        "--platform=managed",
        "--format=json",
        authArg,
        `--memory=${config.memory}`,
        `--cpu=${config.cpu}`,
        `--image=${config.image}`,
        `--region=${config.region}`,
        `--port=${config.port}`,
        `--set-env-vars=${envString}`,
        `--args=${argsString}`
    ];
    await execGCloud(gcargs, config.globalOpts);
}
exports.cloudRunDeploy = cloudRunDeploy;
async function cloudRunUpdateTraffic(config) {
    const gcargs = [
        "run",
        "services",
        "update-traffic",
        config.name,
        "--platform=managed",
        "--format=json",
        `--region=${config.region}`,
        `--to-revisions`,
        `LATEST=${config.trafficPct}`
    ];
    await execGCloud(gcargs, config.globalOpts);
}
exports.cloudRunUpdateTraffic = cloudRunUpdateTraffic;
async function cloudRunDelete(config) {
    try {
        await execGCloud([
            "run",
            "services",
            "delete",
            config.name,
            "--platform=managed",
            "--format=json",
            `--region=${config.region}`
        ], config.globalOpts);
    }
    catch (e) {
        if (common_1.isExecaError(e) && e.exitCode !== 0 && e.stderr.match(/Cannot find service/)) {
            return undefined;
        }
        throw e;
    }
}
exports.cloudRunDelete = cloudRunDelete;
async function execGCloud(args, globalOpts, options = {}) {
    const execaOpts = Object.assign({ all: true }, options);
    const fullArgs = ["--quiet", ...args];
    if (globalOpts.configuration) {
        fullArgs.unshift(`--configuration=${globalOpts.configuration}`);
    }
    debug(`Running: gcloud ${fullArgs.join(" ")}`);
    try {
        const ret = execa_1.default("gcloud", fullArgs, execaOpts);
        return await ret;
    }
    catch (e) {
        if (common_1.isExecaError(e) && e.all)
            e.message = `${e.shortMessage}\n${e.all}`;
        debug(`Failed: gcloud ${fullArgs.join(" ")}: ${e.message}`);
        throw e;
    }
}
exports.execGCloud = execGCloud;
//# sourceMappingURL=commands.js.map