commandlinefu
Simple nodejs module to easily get commandlinefu.com snippets in JSON format.
Install
npm install commandlinefu
Use
With nodejs:
const clfu = require("commandlinefu");
clfu()
.then((res) => console.log(res))
.catch((e) => e);
node clfu.js
output:
{
"id": "5427",
"command": "curl ifconfig.me",
"summary": "Get your external IP address",
"votes": "262",
"url": "http://www.commandlinefu.com/commands/view/5427/get-your-external-ip-address"
}
This object will be referred as responseObject
from now on.
Features
By default (no argument) it will returns a random responseObject
.
You can pass a string as argument, in which case it will return an Array[] of responseObject{}.
Available arguments:
- "popular" returns an Array of popular
responseObject
(which tbh doesn't change much over time) - "search:$var" returns an Array of
responseObject
related to $var
Example of dynamic search:
const userInput = "ssh";
clfu(`search:${userInput}`)
.then((res) => console.log(res))
.catch((e) => e);
Discord bot
I originally wrote this for my discord bot. If you want to use this module for your own, feel free to check my (dirty) implementation :
- here is where I take user input & call my module to fetch stuff accordingly
- here are my format functions. Note that I divide the response into several chunks to get around Discord limitations (maximum 2000 characters for a message).
CLI
Nodejs make it easy to wrap functions into a custom CLI suiting your needs. In terminal :
mkdir myCli
cd ./myCli
npm init
npm i commandlinefu
touch index.js
This is the content of your index.js
:
const clfu = require("commandlinefu");
const commandlinefu = async () => {
if (!process.argv.slice(2)[0]) {
return await clfu()
.then(res => console.log(res.command))
.catch(e => console.error(e));
}
else if (process.argv.slice(2)[0]) {
return await clfu(process.argv.slice(2)[0])
.then(res => console.log(res))
.catch(e => console.error(e));
};
commandlinefu();
That's it! Now back into your terminal:
node index.js
ps auxw | awk '/(apache|httpd)/{print"strace -F -p " $2}' | sh
node index.js search:ssh
[
{
id: '24887',
command: 'ssh -L8888:localhost:80 -i nov15a.pem ubuntu@123.21.167.60',
summary: 'port forwarding',
votes: '1',
url: 'http://www.commandlinefu.com/commands/view/24887/port-forwarding'
},
{
id: '24879',
command: "rsync -e 'ssh -i /root/my.pem' -avz /mysql/db/data_summary.* ec2-1-2-4-9.compute-1.amazonaws.com:/mysql/test/",
summary: 'rsync using pem file',
votes: '0',
url: 'http://www.commandlinefu.com/commands/view/24879/rsync-using-pem-file'
},
{...more objects}
]
This is the basic principle. You can parse & do your stuff with those arrays in the else if
block of aforementioned index.js
file.
If you never used process.argv
before, this read should get you started
More
If you're looking for out-of-the-box CLI instead of a reusable function, it may be worth checking this repo.
At least it helped me decipher commandlinefu.com API endpoints, so thank you nire0510 :)
Todo