{"size": 3371, "ext": "js", "max_stars_repo_path": ".github/actions/husarnet-js/index.js", "max_stars_repo_name": "DominikN/esp32-ota-asyncdemo", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": ".github/actions/husarnet-js/index.js", "max_issues_repo_name": "DominikN/esp32-ota-asyncdemo", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": ".github/actions/husarnet-js/index.js", "max_forks_repo_name": "DominikN/esp32-ota-asyncdemo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.8595041322, "max_line_length": 127, "alphanum_fraction": 0.5974488282}
const core = require("@actions/core");
const github = require("@actions/github");
const exec = require("@actions/exec");
const cache = require("@actions/cache");
const io = require('@actions/io');



// most @actions toolkit packages have async methods
async function run() {
  try {

    const joincode = core.getInput("join-code");
    const hostname = core.getInput("hostname");
    const cachekey = "my-temp-cache-key"; //core.getInput("cache-key");

    let myOutput = "";
    let myError = "";

    // Recursive must be true for directories
    const options_io = { recursive: true, force: true }

    const options_exec = {};
    options_exec.listeners = {
      stdout: (data) => {
        myOutput = data.toString();
      },
      stderr: (data) => {
        myError = data.toString();
      },
    };

    const cache_paths = [
      // './husarnet_cache',
      '/var/lib/husarnet',
    ]

    // const paths = [
    //   // `${github.}`,
    //   "packages/*/node_modules/",
    // ];
    // const key = "npm-foobar-d5ea0750";
    // const cacheId = await cache.saveCache(paths, key);
    
    console.log("starting:" + cache_paths[0]);

    // await exec.exec(
    //   `/bin/bash -c "sudo chown $(whoami):$(whoami) /var/lib/husarnet"`,
    //   options_exec
    // );

    // // await io.mkdirP('./husarnet_cache');
    // let result = await cache.restoreCache(cache_paths, cachekey);
    // if ( result != undefined ) {
    //   await io.cp(cache_paths[0], '/var/lib/husarnet/', options_io);
    // }

    // https://github.com/actions/toolkit/issues/346
    await exec.exec(
      `/bin/bash -c "curl https://install.husarnet.com/install.sh | sudo bash"`
    );

    await exec.exec(
      `/bin/bash -c "sudo chown $(whoami):$(whoami) /var/lib/husarnet"`,
      options_exec
    );
    await cache.restoreCache(cache_paths, cachekey);

    // await io.mkdirP('./husarnet_cache');
    let result = await cache.restoreCache(cache_paths, cachekey);

    await exec.exec(
      `/bin/bash -c "sudo chown root:root /var/lib/husarnet"`,
      options_exec
    );
    // await io.cp('~/husarnet_cache', '/var/lib/husarnet/', options);

    await exec.exec(`/bin/bash -c "sudo systemctl restart husarnet"`);

    console.log("Waiting for Husarnet to be ready");
    await exec.exec(`/bin/bash -c "while [ $(sudo husarnet status | grep "ERROR" | wc -l) -gt 0 ]; do echo .; sleep 1; done"`);
    console.log("done");

    await exec.exec(
      `/bin/bash -c "sudo husarnet status"`,
      options_exec
    );
    console.log(myOutput.toString());

    await exec.exec(
      `/bin/bash -c "sudo husarnet join ${joincode} ${hostname}"`,
      options_exec
    );
    console.log("output is:" + myOutput.toString());

    await exec.exec(
      `/bin/bash -c "sudo chown $(whoami):$(whoami) /var/lib/husarnet"`,
      options_exec
    );
    // await exec.exec(
    //   `/bin/bash -c "sudo chown $(whoami):$(whoami) /var/lib/husarnet"`,
    //   options_exec
    // );
    // await io.cp('/var/lib/husarnet/', cache_paths[0], options_io);
    await cache.saveCache(cache_paths, cachekey);

    console.log(`JoinCode of this GA: ${joincode}`);

    const ipv6 = "fc94:2283:b56b:beeb:xxxx:xxxx:xxxx:xxxx";
    core.setOutput("ipv6", ipv6);

    console.log(JSON.stringify(github, null, "\t"));


  } catch (error) {
    core.setFailed(error.message);
  }
}

run();
