Skip to content

Commit 99342d8

Browse files
committed
first commit
0 parents  commit 99342d8

29 files changed

+1455
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: README.md

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Note Box
2+
Note box is a sqlite3 based database application which allows people create / search notes
3+
under command line. It has following features:
4+
5+
* Any data acceptable including binary or files
6+
* Password protection: AES256 encryption
7+
* No overhead. Just drop in notes
8+
* pipe in and pipe out
9+
* Note search / full text search / tag based search
10+
11+
## Installation
12+
```
13+
npm install -g notebox
14+
```
15+
16+
# CheatSheet (A way to notebox ninja)
17+
## New note
18+
19+
```
20+
nb n "this is my note"
21+
```
22+
23+
Input multiple line
24+
25+
```
26+
nb n
27+
```
28+
29+
Input from a file
30+
31+
```
32+
nb n < myfile
33+
34+
```
35+
36+
Set tags (separate by comma)
37+
38+
```
39+
nb n -t tag1,tag2,tag3 "Hell world"
40+
```
41+
42+
Store binary file with a title
43+
44+
```
45+
nb n --title mypdf.pdf <mypdf.pdf
46+
```
47+
Use password to protect data
48+
49+
```
50+
nb -n -p pwd "my data here"
51+
```
52+
or input password silently
53+
54+
```
55+
nb -n "my data " -p
56+
```
57+
58+
## Search note
59+
60+
```
61+
nb s "word or phrase"
62+
```
63+
or by tags
64+
65+
```
66+
nb s -t tag1,tag2
67+
```
68+
69+
or by id
70+
71+
```
72+
nb s --id <id>
73+
```
74+
75+
remove all found items (find and remove)
76+
77+
```
78+
nb s --delete
79+
```
80+
81+
extract stored file
82+
83+
```
84+
nb s --title mypdf.pdf -o raw > mypdf.pdf
85+
```
86+
87+
extract password protected file
88+
89+
```
90+
nb s --title mypic.png -p -o raw >mypic.png
91+
```
92+
93+
## Others
94+
95+
Shrink DB file
96+
97+
```
98+
nb sql "vacuum;"
99+
```

Diff for: cli/comm.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports={
2+
initDb:initDb
3+
}
4+
function initDb(path,cb){
5+
var db = require("../db").get(path);
6+
require("../db").init(db,cb);
7+
}

Diff for: cli/editor.js

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module.exports={
2+
edit:edit
3+
}
4+
5+
6+
var fs = require("fs");
7+
var env = require("../env");
8+
var log = require("../log");
9+
var temp=require("temp");
10+
var spawn=require("child_process").spawn;
11+
var tty=require("tty");
12+
temp.track();
13+
function edit(data,cb){
14+
temp.open("update-note",function(err,info){
15+
if (err){
16+
cb(err);
17+
}else{
18+
log.debug(info);
19+
var fd=info.fd;
20+
var path=info.path;
21+
fs.writeSync(fd,data);
22+
runEditor(path,function(){
23+
var newData=fs.readFileSync(path,"utf8");
24+
cb(null,newData);
25+
});
26+
}
27+
});
28+
}
29+
function runEditor(path,cb){
30+
var editor=getEditor();
31+
var c=spawn(editor,[path],{
32+
stdio:[process.stdin,process.stdout,process.stderr]
33+
});
34+
c.on("close",function(){
35+
process.stdin.setRawMode(false);
36+
cb();
37+
});
38+
process.stdin.setRawMode(true);
39+
}
40+
function getEditor() {
41+
var cmd = env.get("EDITOR");
42+
if (fs.existsSync(cmd)){
43+
return cmd;
44+
}else{
45+
throw(new Error("Cannot find a valid editor. Please setup $EDITOR environtment variable."));
46+
}
47+
}

Diff for: cli/nb-n.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
var cli = require("commander");
2+
var async = require("async");
3+
var inputData = "";
4+
var note = require("../core/note");
5+
var db = require("../db");
6+
var log = require("../log");
7+
cli
8+
.option("-d, --db [dbFile]", "The path to a database file. If omitted, it will use $HOME/.sn.")
9+
.option("-t, --tags <tags...>", "Tags associated with the data. Seperated by comma (,)")
10+
.option("-b, --binary", "Flag. Use it if input data is binary.")
11+
.option("-p, --password [password]", "The note will be protected by a password")
12+
.option("--title <title>", "The title of the note.")
13+
.parse(process.argv);
14+
15+
var d = db.get(cli.db);
16+
var rl = require("readline-sync");
17+
var params = {};
18+
var pwd = "";
19+
var W=require("stream").Writable;
20+
log.debug(cli.args, process.stdin.isTTY);
21+
async.series([
22+
function(scb){
23+
log.debug("init db");
24+
require("./comm").initDb(cli.db,scb);
25+
},
26+
function(scb) {
27+
if (cli.args.length > 0) {
28+
inputData = cli.args[0];
29+
scb();
30+
} else {
31+
log.debug("PIPE mode for stdio.");
32+
var bufferArr = [];
33+
process.stdin.resume();
34+
process.stdin.on("data", function(b,e) {
35+
if (cli.binary) {
36+
bufferArr.push(b);
37+
} else {
38+
bufferArr.push(b.toString());
39+
}
40+
});
41+
process.stdin.on("end", function() {
42+
if (cli.binary) {
43+
inputData = Buffer.concat(bufferArr);
44+
} else {
45+
inputData = bufferArr.join("");
46+
}
47+
scb();
48+
});
49+
process.stdin.on("error", function(e) {
50+
scb(e);
51+
});
52+
}
53+
},
54+
function(scb) { //check pwd
55+
if (cli.password) {
56+
if (cli.password === true) {
57+
pwd=rl.question("Please enter password: ",{hideEchoBack:true});
58+
scb();
59+
} else {
60+
pwd = cli.password;
61+
scb();
62+
}
63+
}else{
64+
scb();
65+
}
66+
67+
},
68+
function(scb){
69+
if (pwd){
70+
var aes=require("../encryption/aes");
71+
inputData=aes.encrypt(inputData,pwd);
72+
params.ispassword=1;
73+
}
74+
scb();
75+
},
76+
function(scb) {
77+
params.data= inputData
78+
if (cli.binary) {
79+
params.isblob = 1;
80+
}
81+
if (cli.title) {
82+
params.title = cli.title
83+
}
84+
var tags = cli.tags;
85+
async.waterfall([
86+
function(sscb) {
87+
log.debug(params);
88+
note.new(d, params, sscb);
89+
},
90+
function(id, sscb) {
91+
console.log("ID: ", id);
92+
if (tags) {
93+
note.addTags(d, id, tags.split(","), sscb);
94+
} else {
95+
sscb();
96+
}
97+
}
98+
], scb);
99+
}
100+
], function(err) {
101+
if (err) {
102+
console.log(err);
103+
}
104+
});

Diff for: cli/nb-s.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
var cli = require("commander");
2+
var async = require("async");
3+
var inputData = "";
4+
var note = require("../core/note");
5+
var db = require("../db");
6+
var log = require("../log");
7+
cli
8+
.option("-d, --db [dbFile]", "The path to a database file. If omitted, it will use $HOME/.sn.")
9+
.option("--id <id>", "Search by note id")
10+
.option("-t, --tags <tags>", "Results should belong to the tags. Seperate by comma(,)")
11+
.option("--delete", "Remove all found notes.")
12+
.option("-o, --output <outputType>", "Output format. It could be: table(default), raw, csv. ")
13+
.option("-p, --password [password]", "Attempt to decrypt all data with password")
14+
.parse(process.argv);
15+
16+
17+
var rl = require("readline-sync");
18+
require("./comm").initDb(cli.db, function() {
19+
var d = db.get(cli.db);
20+
var output = "table";
21+
if (cli.output) {
22+
output = cli.output;
23+
}
24+
if (cli.delete) {
25+
output = "delete";
26+
}
27+
var pwd = "";
28+
if (cli.password) {
29+
if (cli.password === true) {
30+
pwd = rl.question("Please enter password: ", {
31+
hideEchoBack: true
32+
});
33+
} else {
34+
pwd = cli.password;
35+
}
36+
}
37+
try {
38+
var opFunc = require("./output-" + output);
39+
if (cli.id) { //search by id
40+
note.loadById(d, cli.id, function(err, res) {
41+
if (err) {
42+
log.error(err);
43+
} else {
44+
opFunc(prepareOutput([res]), cli);
45+
}
46+
});
47+
} else { //search by text
48+
var txt = cli.args[0];
49+
log.debug("Search text:" + txt, "tags: ", cli.tags);
50+
note.searchText(d, txt, cli.tags ? cli.tags.split(",") : [], function(err, res) {
51+
if (err) {
52+
log.error(err);
53+
} else {
54+
opFunc(prepareOutput(res), cli);
55+
}
56+
})
57+
}
58+
} catch (e) {
59+
log.error(e);
60+
log.error("Failed initialise output printer.");
61+
}
62+
63+
var aes = require("../encryption/aes");
64+
65+
function prepareOutput(arr) {
66+
var rtn = [];
67+
arr.forEach(function(item) {
68+
if (item.ispassword) {
69+
try {
70+
item.data = aes.decrypt(item.data, pwd);
71+
item.ispassword = false;
72+
} catch (e) {
73+
log.warn(e);
74+
}
75+
}
76+
rtn.push(item);
77+
});
78+
return rtn;
79+
}
80+
});

0 commit comments

Comments
 (0)