Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvemets for process json raw data and remote jsons #14

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ It can also handle gzipped files. If the extension is .gz, it will decompress th
jwalk somefile.json.gz
```

...and raw json data.

```
jwalk -j '{"some":"json"}'
```

...and remote json url.

```
jwalk -u http://some.domain.com/file.json
```

## Possible Commands

Given the following json file
Expand Down Expand Up @@ -130,6 +142,16 @@ jwalk obj{8} / $ keys
'engine' ]
```

## Flatten

Prints out JSON on a single line for easy copy

```
jwalk obj{8} / $ flatten

{"name":"jwalk","version":"0.0.4","description":"command-line json inspector","preferGlobal":"true","repositories":{"type":"git","url":"http://github.com/nkohari/jwalk"},"bin":{"jwalk":"bin/jwalk"},"dependencies":{"coffee-script":"1.4.0","colors":"0.6.0-1","filesize":"1.6.6","underscore":"1.4.2"},"engine":"node >= 0.8.x"}
```

### Quit

Exits the jwalk application
Expand Down
41 changes: 30 additions & 11 deletions index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,35 @@ if process.argv.length < 3
console.log "usage: jwalk <filename>"
process.exit(1)

filename = process.argv[2]
if !fs.existsSync(filename)
console.log "File not found: #{filename}"
process.exit(1)

if fs.existsSync("#{HOME}/.jwalk")
config = JSON.parse fs.readFileSync("#{HOME}/.jwalk")
config = JSON.parse fs.readFileSync("#{HOME}/.jwalk")

if process.argv[2] == "-j" || process.argv[2] == "--json"
jsonString = process.argv[3]

loader.parse jsonString, (err, tree) ->
if err?
console.log "Error parsing JSON: #{err}".red
process.exit(2)
prompt.run(tree, config)

else if process.argv[2] == "-u" || process.argv[2] == "--url"
url = process.argv[3]

loader.getURL url, (err, tree) ->
if err?
console.log "Error getting JSON from url: #{err}".red
process.exit(2)
prompt.run(tree, config)

else
filename = process.argv[2]
if !fs.existsSync(filename)
console.log "File not found: #{filename}"
process.exit(1)

loader.load filename, (err, tree) ->
if err?
console.log "Error loading file: #{err}".red
process.exit(2)
prompt.run(tree, config)
loader.load filename, (err, tree) ->
if err?
console.log "Error loading file: #{err}".red
process.exit(2)
prompt.run(tree, config)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"coffee-script": "1.4.0",
"colors": "0.6.0-1",
"filesize": "1.6.6",
"underscore": "1.4.2"
"underscore": "1.4.2",
"request": ">= 2.0.0"
},
"engine": "node >= 0.8.x"
}
20 changes: 20 additions & 0 deletions src/commands/Flatten.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
_ = require 'underscore'
Command = require './Command'

class Flatten extends Command

constructor: (@commands) ->

help: ->
'prints json in a one line'

run: (context, args, callback) ->
console.log()
if _.isObject(context.pointer)
console.log JSON.stringify(context.pointer)
else
console.log context.pointer
console.log()
callback()

module.exports = Flatten
16 changes: 9 additions & 7 deletions src/commands/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ ExitProcess = require './ExitProcess'
Help = require './Help'
ShowKeys = require './ShowKeys'
Inspect = require './Inspect'
Flatten = require './Flatten'

commands =
cd: new ChangePath()
clear: new ClearScreen()
cls: new ClearScreen()
exit: new ExitProcess()
keys: new ShowKeys()
ls: new Inspect()
quit: new ExitProcess()
cd: new ChangePath()
clear: new ClearScreen()
cls: new ClearScreen()
exit: new ExitProcess()
keys: new ShowKeys()
ls: new Inspect()
quit: new ExitProcess()
flatten: new Flatten()

commands.help = new Help(commands)

Expand Down
13 changes: 12 additions & 1 deletion src/loader.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
fs = require 'fs'
path = require 'path'
zlib = require 'zlib'
request = require 'request'

filesize = require 'filesize'

Expand All @@ -11,7 +12,7 @@ CLEAR = '\u001B[A\u001B[2K'
updateLoadingDisplay = (loaded, done) ->
console.log "#{CLEAR} #{if done then '├' else '└'} size in RAM: #{filesize(loaded)}"

parse = (json, callback) ->
exports.parse = parse = (json, callback) ->
console.log " └ parsing..."
start = Date.now()
tree = JSON.parse(json)
Expand Down Expand Up @@ -44,3 +45,13 @@ exports.load = (filename, callback) ->
readStream.on 'end', ->
updateLoadingDisplay(json.length, true)
parse(json, callback)

exports.getURL = (url, callback) ->

console.log "Getting from: " + url

request {"url": url,"headers": {"Accept": "application/json"}}, (err, res, body) ->
if (err)
callback(err)
else
parse(body, callback)