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

Add Describe command for schema inspection. #13

Open
wants to merge 1 commit 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 @@ -88,6 +88,28 @@ jwalk obj{8} / $ ls
engine: 'node >= 0.8.x' }
```

### Describe

Show the schema of the given node.

```
jwalk obj{8} / $ ll
/
name string(5) "jwalk"
version string(5) "0.0.4"
description string(27) "command-line json inspector"
preferGlobal string(4) "true"
repositories object
bin object
dependencies object
engine string(13) "node >= 0.8.x"
jwalk obj{8} / $ ll repositories
/repositories/
type string(3) "git"
url string(31) "http://github.com/nkohari/jwalk"
```


### Change Directory

Allows navigation through the JSON tree. Note 'cd' does support autocomplete by pressing the tab key.
Expand Down
63 changes: 63 additions & 0 deletions src/commands/Describe.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
_ = require 'underscore'
Command = require './Command'

class Describe extends Command

help: ->
'examines the schema of an object node'

autocomplete: (context, str, callback) ->
return super unless _.isObject(context.pointer)
keys = _.keys(context.pointer)
matches = _.filter keys, (key) -> key.indexOf(str) == 0
results = if matches.length > 0 then matches else keys
callback null, [results, str]

run: (context, args, callback) ->

root = context.pointer
path = '/' + context.path.join('/')
try
subpath = if args? and args[0]? then args[0] else ''
if subpath.length > 0
for key in subpath.split('/')
root = root[key]
path += key + '/'
catch error
console.log "Not an object".red
console.log error
callback()
return

console.log path

unless _.isObject(root)
console.log "Not an object".red
callback()
return

typedesc = (key, obj) ->
rslt = ""
summary = ""
if _.isArray(obj)
rslt = "array(#{obj.length})"
else if _.isNumber(obj)
rslt = "number"
summary = obj.toString().red
else if _.isString(obj)
rslt = "string(#{obj.length})"
sz = 80
summary = if obj then '"' + obj.substring(0, sz) + '"' else ''
summary += '..' if obj.length > sz
else
rslt = typeof(obj)
rslt = rslt.cyan + ' ' + summary.green

for key in _.keys(root)
do (key) ->
value = root[key]
console.log ' ', key, typedesc(key, value)

callback()

module.exports = Describe
2 changes: 2 additions & 0 deletions src/commands/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ClearScreen = require './ClearScreen'
ExitProcess = require './ExitProcess'
Help = require './Help'
ShowKeys = require './ShowKeys'
Describe = require './Describe'
Inspect = require './Inspect'

commands =
Expand All @@ -11,6 +12,7 @@ commands =
cls: new ClearScreen()
exit: new ExitProcess()
keys: new ShowKeys()
ll: new Describe()
ls: new Inspect()
quit: new ExitProcess()

Expand Down