-
-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mega-linter-runner --codetotal (#2877)
* runner v0 * Allow to run CodeTotal with a single command npx mega-linter-runner@beta --codetotal * Manage browser open * Add codetotal-url option * Add colors in messages * changelog * changelog * cspell ! * [MegaLinter] Apply linters fixes --------- Co-authored-by: nvuillam <nicolas.vuillamy@ox.security> Co-authored-by: nvuillam <nvuillam@users.noreply.github.com>
- Loading branch information
1 parent
8d5dd3e
commit 832236e
Showing
7 changed files
with
171 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -587,6 +587,7 @@ | |
"codebases", | ||
"codeclimate", | ||
"codecov", | ||
"codetotal", | ||
"codenarcargs", | ||
"codeql", | ||
"codestyle", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#! /usr/bin/env node | ||
"use strict"; | ||
const { spawnSync, spawn } = require("child_process"); | ||
const c = require("chalk"); | ||
const fs = require("fs-extra"); | ||
const https = require('https'); | ||
const open = require("open"); | ||
const path = require("path"); | ||
const which = require("which"); | ||
const { asciiArtCodeTotal } = require("./ascii"); | ||
|
||
class CodeTotalRunner { | ||
constructor(options = {}) { | ||
this.options = options; | ||
} | ||
|
||
async run() { | ||
console.log(asciiArtCodeTotal()); | ||
|
||
// Retrieve docker-compose | ||
if (!fs.existsSync(path.join(process.cwd(), 'docker-compose.yml'))) { | ||
const dockerComposeUrl = "https://raw.githubusercontent.com/oxsecurity/codetotal/main/docker-compose.yml"; | ||
console.info(c.cyan(`Downloading latest docker-compose.yml from ${c.bold(dockerComposeUrl)} ...`)); | ||
https.get(dockerComposeUrl, resp => resp.pipe(fs.createWriteStream(path.join(process.cwd(), 'docker-compose.yml')))); | ||
await new Promise(r => setTimeout(r, 2000)); | ||
} | ||
|
||
// Check for docker installation | ||
const whichPromise = which("docker"); | ||
whichPromise.catch(() => { | ||
console.error(c.red(` | ||
ERROR: Docker engine has not been found on your system. | ||
- to run CodeTotal locally, please install docker desktop: https://www.docker.com/products/docker-desktop`)); | ||
}); | ||
|
||
// Get platform to use with docker pull & run | ||
const imagesPlatform = this.options.platform || "linux/amd64"; | ||
const platformVars = { | ||
"DOCKER_DEFAULT_PLATFORM": imagesPlatform | ||
}; | ||
|
||
// Pull docker image | ||
if (this.options.nodockerpull !== true) { | ||
console.info(c.cyan(`Pulling docker-compose.yml images...`)); | ||
console.info(c.grey( | ||
"INFO: this operation can be long during the first use of CodeTotal" | ||
)); | ||
console.info(c.grey( | ||
"The next runs, it will be immediate (thanks to docker cache !)" | ||
)); | ||
console.log("Running command: " + c.whiteBright(c.bgGray("docker-compose " + ["-f", "docker-compose.yml", "pull"].join(" ")))); | ||
const spawnResPull = spawnSync( | ||
"docker-compose", | ||
["-f", "docker-compose.yml", "pull"], | ||
{ | ||
detached: false, | ||
stdio: "inherit", | ||
windowsHide: true, | ||
windowsVerbatimArguments: true, | ||
env: { ...process.env, ...platformVars } | ||
} | ||
); | ||
// Manage case when unable to pull docker image | ||
if (spawnResPull.status !== 0) { | ||
return { | ||
status: 2, | ||
errorMsg: `Unable to pull docker images: \n${JSON.stringify( | ||
spawnResPull, | ||
null, | ||
2 | ||
)}`, | ||
}; | ||
} | ||
} else { | ||
console.log(`Skipped pull of docker images (--nodockerpull used)`); | ||
} | ||
|
||
// Prepare docker-compose command | ||
const commandArgs = ["-f", "docker-compose.yml", "up"]; | ||
|
||
// Prepare interval to check localhost is open | ||
let isOpen = false; | ||
const uiUrl = this.options["codetotal-url"] || "http://localhost:8081/"; | ||
let interval = setInterval(async () => { | ||
let response; | ||
try { | ||
response = await fetch(uiUrl); | ||
} catch (e) { | ||
// URL not available yet | ||
return; | ||
} | ||
const statusCode = response.status; | ||
if (statusCode >= 200 && statusCode <= 400 && isOpen === false) { | ||
clearInterval(interval); | ||
isOpen = true; | ||
console.log(c.green("CodeTotal is started: opening " + uiUrl + " ...")); | ||
open(uiUrl); | ||
console.log(c.yellow("Hit CTRL+C to terminate")); | ||
} | ||
}, 2000); | ||
|
||
// Call docker run | ||
console.log("Running command: " + c.whiteBright(c.bgGray("docker-compose " + commandArgs.join(" ")))); | ||
const spawnOptions = { | ||
stdio: "inherit", | ||
windowsHide: true, | ||
env: { ...process.env, ...platformVars } | ||
}; | ||
const spawnRes = spawn("docker-compose", commandArgs, spawnOptions); | ||
return spawnRes; | ||
} | ||
} | ||
|
||
module.exports = { CodeTotalRunner }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters