text: "use strict";
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
      return new (P || (P = Promise))(function (resolve, reject) {
          function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
          function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
          function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
          step((generator = generator.apply(thisArg, _arguments || [])).next());
      });
  };
  var __importStar = (this && this.__importStar) || function (mod) {
      if (mod && mod.__esModule) return mod;
      var result = {};
      if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
      result["default"] = mod;
      return result;
  };
  Object.defineProperty(exports, "__esModule", { value: true });
  const core = __importStar(require("@actions/core"));
  const exec_1 = require("@actions/exec");
  const toolCache = __importStar(require("@actions/tool-cache"));
  const fs = __importStar(require("fs-extra"));
  const os = __importStar(require("os"));
  const path = __importStar(require("path"));
  const semver = __importStar(require("semver"));
  class Utils {
      static downloadCli() {
          return __awaiter(this, void 0, void 0, function* () {
              let version = core.getInput(Utils.CLI_VERSION_ARG);
              if (semver.lt(version, this.MIN_CLI_VERSION)) {
                  throw new Error('Requested to download JFrog CLI version ' + version + ' but must be at least ' + this.MIN_CLI_VERSION);
              }
              let fileName = Utils.getCliExecutableName();
              let cliDir = toolCache.find(fileName, version);
              if (cliDir) {
                  return path.join(cliDir, fileName);
              }
              let url = Utils.getCliUrl(version, fileName);
              let downloadDir = yield toolCache.downloadTool(url);
              cliDir = yield toolCache.cacheFile(downloadDir, fileName, fileName, version);
              let cliPath = path.join(cliDir, fileName);
              if (!Utils.isWindows()) {
                  fs.chmodSync(cliPath, 0o555);
              }
              core.addPath(cliDir);
              return cliPath;
          });
      }
      static getCliUrl(version, fileName) {
          let bintrayPackage = 'jfrog-cli-' + Utils.getArchitecture();
          return ('https://api.bintray.com/content/jfrog/jfrog-cli-go/' + version + '/' + bintrayPackage + '/' + fileName + '?bt_package=' + bintrayPackage);
      }
      static getServerTokens() {
          return Object.keys(process.env)
              .filter(env => env.match(Utils.SERVER_TOKEN_PREFIX))
              .map(envKey => process.env[envKey] || '');
      }
      static setCliEnv() {
          core.exportVariable('JFROG_CLI_ENV_EXCLUDE', '*password*;*secret*;*key*;*token*;JF_ARTIFACTORY_*');
          core.exportVariable('JFROG_CLI_OFFER_CONFIG', 'false');
          let buildNameEnv = process.env.GITHUB_WORKFLOW;
          if (buildNameEnv) {
              core.exportVariable('JFROG_CLI_BUILD_NAME', buildNameEnv);
          }
          let buildNumberEnv = process.env.GITHUB_SHA;
          if (buildNumberEnv) {
              core.exportVariable('JFROG_CLI_BUILD_NUMBER', buildNumberEnv);
          }
          core.exportVariable('JFROG_CLI_BUILD_URL', 'https://github.com/' + process.env.GITHUB_REPOSITORY + '/commit/' + buildNumberEnv + '/checks');
      }
      static configArtifactoryServers(cliPath) {
          return __awaiter(this, void 0, void 0, function* () {
              for (let serverToken of Utils.getServerTokens()) {
                  yield Utils.runCli(cliPath, ['rt', 'c', 'import', serverToken]);
              }
          });
      }
      static getArchitecture() {
          if (Utils.isWindows()) {
              return 'windows-amd64';
          }
          if (os.platform().includes('darwin')) {
              return 'mac-386';
          }
          if (os.arch().includes('arm')) {
              return os.arch().includes('64') ? 'linux-arm64' : 'linux-arm';
          }
          return os.arch().includes('64') ? 'linux-amd64' : 'linux-386';
      }
      static getCliExecutableName() {
          return Utils.isWindows() ? 'jfrog.exe' : 'jfrog';
      }
      static isWindows() {
          return os.platform().startsWith('win');
      }
      static runCli(cliPath, args) {
          return __awaiter(this, void 0, void 0, function* () {
              let res = yield exec_1.exec(cliPath, args);
              if (res !== core.ExitCode.Success) {
                  throw new Error('JFrog CLI exited with exit code ' + res);
              }
          });
      }
  }
  Utils.SERVER_TOKEN_PREFIX = /^JF_ARTIFACTORY_.*$/;
  Utils.CLI_VERSION_ARG = 'version';
  Utils.MIN_CLI_VERSION = '1.29.0';
  exports.Utils = Utils;
  