How to take output of MySQL SHELL utility in file - javascript

I am running util.checkForServerUpgrade() function in JS mode in MySQL Shell utility. I need output of mentioned function in file instead of command-line.
Here is the exact syntax which I am using:
util.checkForServerUpgrade('root#localhost:3306', {"password":"abc", "targetVersion":"8.0.11", "outputFormat":"JSON", "configPath":"C:\ProgramData\MySQL\MySQL Server 5.7\my.ini"})
I have tried appending > output.json and many more but not working for me.

Execute the util.checkForServerUpgrade call from the command line when mysqlsh ist started:
mysqlsh root:****#localhost:3306 -e "util.checkForServerUpgrade('root#localhost:3306', {\"password\":\"abc\", \"targetVersion\":\"8.0.11\", \"outputFormat\":\"JSON\", \"configPath\":\"C:\ProgramData\MySQL\MySQL Server 5.7\my.ini\"})" > err.log

Related

Output gulp-qunit console output to file

I am running unit tests using QUnit and trying to integrate QUnit into our build automation and Continuous Integration process. For Atlassian Bamboo to parse the test output it requires the test output to be in an xml file. I can generate a console log that is in the required xml format by using the qunit-reporter-junit plugin. When Gulp-QUnit runs our test-runner.html file it outputs the console.log to the screen. My problem is that I cannot find a way to pipe this console.log output into a file.
I have tried the following approaches:
Using gulp-log-capture plugin (does nothing):
gulp.task('qunit', function() {
return gulp.src('./qunit/test-runner.html')
.pipe(logCapture.start(console,'log'))
.pipe(qunit())
.pipe(logCapture.stop('build.xml'));
});
Piping the output into a write stream (which throws an error):
gulp.task('qunit', function() {
return gulp.src('./qunit/test-runner.html')
.pipe(qunit())
.pipe(fs.createWriteStream('build.xml));
});
Using gulp-out plugin (which simply pipes the input html into the new file):
gulp.task('qunit', function() {
return gulp.src('./qunit/test-runner.html')
.pipe(qunit())
.pipe(out('build.xml');
});
The XML is right there on the screen I just need to get it into a file somehow.
It turns out that phantom js takes node-like scripts that will run on execution. I basically took the run-qunit script from the examples directory of phantom-js and adjusted it to pipe console output into a build.xml file. Example script can be found here: https://github.com/ariya/phantomjs/blob/master/examples/run-qunit.js
I simply adjusted the onConsoleMessage listener (ln48) like so:
page.onConsoleMessage = function(msg) {
if( msg.search(/xml/) > -1 ) {
fs.write('build.xml',msg);
}
console.log(msg);
};
To make this run as part of an automated build process, in Gulp I run the following task using the exec plugin.
exec = require('child_process').exec;
.
.
.
gulp.task('phantom',function() {
exec('phantomjs ./qunit/run-qunit.js ./qunit/test-runner.html');
});
Both of these adjustments successfully create a build.xml file that Atlassian Bamboo can read as part of its process.

How to run complex command in node js spawn?

I am developing a lib for docker command line in nodejs, I am still in starting face, I just tried basic docker run command using spawn in node js - everything works fine but it's not working for complex cases like the one below.
I want to run docker run --rm -it julia:0.3.6 julia -E "[x^2 for x in 1:100]" in nodejs, but I am gettting below error -
the input device is not a TTY
Docker Shell existed with status = 1
Below Code -
const
spawn = require('child_process').spawn,
dockerDeamon = spawn("docker", ["run","--rm", "-it", "julia:0.3.6", "-E", "\" [x^2 for x in 1:100]\""] );
dockerDeamon.stdout.on('data', data => {
console.log(`${data}`);
});
dockerDeamon.stderr.on('data', data => {
console.log(`${data}`);
});
dockerDeamon.on('close', code => {
console.log(`Docker Shell existed with status = ${code}`);
});
Is there any better way to execute the above script ?
You're passing the -t (--tty) flag to Docker, which tells it that it should expect the input and output to be attached to a terminal (TTY). However, when you're using spawn, you're instead attaching it to a Node.js stream in your program. Docker notices this and therefore gives the error Input device is not a TTY. Therefore, you shouldn't be using the -t flag in this case.
Also, note that you don't need nested quotes in your last argument, "\" [x^2 for x in 1:100]\"". The purpose of the quotes is to preserve the spaces and other special characters in the argument when running in a shell, but when you use spawn you're not using a shell.
So your statement should be something like:
dockerDeamon = spawn("docker", ["run","--rm", "-i", "julia:0.3.6", "julia", "-E", "[x^2 for x in 1:100]"] );

How to check whether chrome browser is installed or not using java script?

My requirement is I need to check whether Chrome browser is insatlled on the client machine or not using Javascript. I have searched on the net not able to find the way out.
Please help in getting this done.
You can't do that with JavaScript, and even if you could, you shouldn't.
JavaScript on the client doesn't have access to the user's system, for very good reasons. (Think, servers with bad intentions.)
You can check if the browser is Chrome with the next code
if(!window.chrome){
//Chrome code
}else{
// Chrome block
}
You can't. Not with JavaScript. However, you can check whether the browser that is currently being used to view your webpage is Google Chrome or not.
<script type="text/javascript">
if(window.chrome){
document.write("Browser is Chrome");
}
else{
document.write("Please download Chrome");
}
</script>
You can't get that kind of information directly from javascript.
What you can do is use that PowerShell command in a script and save the result in a file that you'll read later using javascript.
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, InstallLocation, Publisher, InstallDate | Format-Table -AutoSize
This will get you all the installed programs on the machine from the HKEY_LOCAL_MACHINE registry folder.
The exact path to the folder from wich the informations are retrieved is : HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\
The given command will display you the application name followed by it's version, it's install location, publisher name and installation date in a PowerShell terminal.
If you want to output that list in a file simply add >FileName.txt after the command before pressing enter.
Note that by default the file will be created in the C:\Users\YourUserName\ folder so if you want the file to be created in a specific location you'll have to use the CD command to get to that specific location before executing the Get-Item-Property command.
This will get you done for the get installed programs on a machine part.
Now we can get into the check if app x is installed on the machine part.
First load the previously generated file in your js application you will use it's content to determine if an application is installed on the computer.
The faster way to get if 'chrome' is installed will be to load the file as a string and then do that basic stuff :
if (string.includes('chrome') == true) {
// chrome is installed on the machine
// you can do some more stuff
// like extracting it's path from the file content
} else {
console.log('error: chrome is not installed on this computer');
}
Needless to say that this will only work if used on the same computer from which you want to check the installed applications.
Edit: If you want a more practical file to use in javascript you can replace
Format-Table -AutoSize >FileName.txt
with :
Export-Csv -path .\FileName.txt -NoTypeInformation
this way you can split your file lines using the string.split(',') method and don't have to do some extra stuff to deal with the spaces between data.
Edit 2:
Here's a full working implementation that will let you retrieve informations from a PowerShell script directly from your javascript using NodeJs.
get_programs.ps1 (PowerShell script file) :
chcp 65001 # sets the encoding for displaying chars correctly
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, InstallLocation | ConvertTo-Csv -NoTypeInformation
chcp 850 # restores the default encoding set this will avoid police changes due to the terminal modifications
Notice the change at the end of the command which is now:
| ConvertTo-Csv -NoTypeInformation
this allows to log data in the PowerShell terminal in the csv format which will simplify it's parsing as a string.
If you don't want to use another file to hold those few PowerShell
commands you can use this
child = spawn("powershell.exe",[`chcp 65001
Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, InstallLocation | ConvertTo-Csv -NoTypeInformation
chcp 850`]);
as a replacement for
child = spawn("powershell.exe",["./get_programs.ps1"]);
If you choose to do this don't forget to escape the \ chars else it will not work.
app.js :
var spawn = require("child_process").spawn,child;
child = spawn("powershell.exe",["./get_programs.ps1"]); // here we start our PowerShell script "./" means that it's in the same directory as the .js file
let chromeDetails;
child.stdout.on("data", (data) => { // data event
// here we receive each outputed line in the PowerShell terminal as an Uint8Array
if (data.includes('Chrome')) { // check for the 'Chrome' string in data
chromeDetails = data.toString(); // adds data converted as string
}
});
child.stderr.on("data", (data) => { // logs errors
console.log(`Powershell Errors: ${data}`);
});
child.on("exit", () => { // exit event
console.log("Powershell Script finished");
if (chromeDetails != undefined) {
console.log(`> chrome has been detected on this computer
available informations (appName, version, installPath):
${chromeDetails}`);
} else
console.log('> chrome has not been detected on this computer');
});
child.stdin.end(); // we end the child
Expected output :
Powershell Script finished
> chrome has been detected on this computer
    available informations (appName, version, installPath):
    "Google Chrome","103.0.5060.114","C:\Program Files\Google\Chrome\Application"
If you are not on Windows you may want to take a look at Spawning .bat and .cmd files on Windows from the NodeJs documentation to get hints on how to adapt the above app.js code to work on your system.

nodejs 'exec' command doesn't execute 'sed' command properly

I have a file where I want to scan it, find the character '{' and create a new line, then add an IP on the new line and add a semi-cologne to the end of the line, then write to a config file.
I can accomplish this with the following sed command when running from shell:
sed -i 's/{/&\n1.1.1.1;/g' /tmp/test.conf
inside test.conf:
acl testACL{
};
the output from the command in shell shows:
acl testACL{
1.1.1.1;
};
works perfect! Now the problem is when I get nodejs to execute it:
var sys = require('sys');
var exec = require('child_process').exec;
function puts(error, stdout, stderr) { sys.puts(stdout) };
exec("sed -i 's/{/&\n1.1.1.1;/g' /tmp/test.conf",puts);
return 0;
when I run the command in console: 'nodejs test.js'
I get blank output and when I check the file, the file 'test.conf' has not been altered! why?!
Also if you're thinking 'its a permissions issue!' I've had nodejs exec command write basic echos to the test config file and it worked fine.
I have also tried the 'shelljs' module with no luck there. I have tried countless combinations for hours now with no prevail! I'm puzzled.
I think you need to escape your \n as \\n. Right now, it's getting parsed as a literal newline in the command, which is messing it up.

Parsing of strings containing forward slash in node.js command line

Hi I am writing a node js app where I need to pass the path to some file via command line. I need to do this for configuration purposes. I understand that I can put all config details in a json file and then load it inside the app. But this is a specific requirement.
Here is my code
app.js
----
-----
console.log(process.argv);
---
--
// Start server
Now when I run this file in node as:
node app.js hii
Output
'hii'
But if I do
node app.js '/samplePath'
I get this output in DOS:
'\'/samplePath\''
I get this output in Git Bash:
'C:/Program Files/Git/samplePath'
How will I get just '/samplePath' as output? What am I doing wrong? Any help would be appreciated.
In order to get the last item, try
process.argv[2].split(/\//).pop();
You will have to parse the command line options your self. However you can look into this node module that will make this easy for you.
npm install commander
Here is an example from their Github page
var program = require('commander');
program
.version('0.0.1')
.option('-p, --peppers', 'Add peppers')
.option('-P, --pineapple', 'Add pineapple')
.option('-b, --bbq', 'Add bbq sauce')
.option('-c, --cheese [type]', 'Add the specified type of cheese [marble]', 'marble')
.parse(process.argv);

Categories

Resources