Skip to content
This repository has been archived by the owner on Mar 3, 2022. It is now read-only.

Commit

Permalink
second commit
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Jun 23, 2018
1 parent 66257f5 commit 686487d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/out/
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: go
go:
- "1.10"
install:
- make install
script:
- "echo '{\"key\":\"value\"}' | go run main.go"
- "echo '{\"foo\": [{\"bar\": \"value\"}]}' | go run main.go 'this.foo[0].bar'"
- "echo '{\"foo\": [{\"bar\": \"value\"}]}' | go run main.go 'this.foo' 'this[0]' 'this.bar'"
- "echo '[]' | go run main.go 'void 0'"
- "echo '{\"foo\": 1, \"bar\": 2}' | go run main.go 'Object.keys(this)'"
- "echo '{\"foo\": 1, \"bar\": 2}' | go run main.go ?"
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.PHONY: install build

install:
go get github.com/robertkrimen/otto
go get github.com/hokaccha/go-prettyjson

build:
gox -output "out/{{.Dir}}_{{.OS}}_{{.Arch}}"
29 changes: 24 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# XX
<img src="https://user-images.githubusercontent.com/141232/41759457-dbfb230a-7618-11e8-9159-03aa62e57152.png" height="100" alt="fx">

# [![Build Status](https://travis-ci.org/antonmedv/xx.svg?branch=master)](https://travis-ci.org/antonmedv/xx)

[fx](https://github.com/antonmedv/fx)-like command-line JSON processing tool

Expand All @@ -7,7 +9,12 @@
* Don't need to learn new syntax
* Plain JavaScript
* Formatting and highlighting
* Small binary size

## Differences

* Only ES5 (no arrow functions, no spread)
* Small binary size (xx ~5mb vs fx ~30mb)
* Can't use npm packages

## Install

Expand Down Expand Up @@ -54,15 +61,27 @@ $ echo '{"foo": [{"bar": "value"}]}' | xx 'this.foo' 'this[0]' 'this.bar'
If you need something different then JSON (for example arguments for xargs) do not return anything from reducer.
`undefined` value printed into stderr by default.
```
echo '[]' | xx 'void 0'
$ echo '[]' | xx 'void 0'
undefined
```

```
echo '[1,2,3]' | xx 'this.forEach(function (x) {console.log(x)})' 2>/dev/null | xargs echo
$ echo '[1,2,3]' | xx 'this.forEach(function (x) { console.log(x) })' 2>/dev/null | xargs echo
1 2 3
```

### Modifying

To modify object use command separated by comma `,` and return `this` as the end.

```
$ echo '{"a": 2}' | xx 'this["b"] = Math.pow(this.a, 10), this'
{
"a": 2,
"b": 1024
}
```

### Object keys

Get all object keys:
Expand All @@ -87,4 +106,4 @@ $ echo '{"foo": 1, "bar": 2}' | xx ?

## License

MIT
MIT
25 changes: 15 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ var vm = otto.New()
func main() {
fi, err := os.Stdin.Stat()
if err != nil {
panic(err)
fatal(err)
}
if fi.Size() == 0 {
if fi.Mode()&os.ModeNamedPipe == 0 {
usage()
os.Exit(1)
}

buf, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
fatal(err)
}
var input interface{}
if err := json.Unmarshal(buf, &input); err != nil {
panic(err)
fatal(err)
}

if err := vm.Set("json", input); err != nil {
panic(err)
fatal(err)
}
value, err := vm.Get("json")
if err != nil {
panic(err)
fatal(err)
}
for _, code := range os.Args[1:] {
value, err = reduce(value, code)
if err != nil {
panic(err)
fatal(err)
}
}

Expand All @@ -51,18 +51,18 @@ func main() {

i, err := value.Export()
if err != nil {
panic(err)
fatal(err)
}
s, err := prettyjson.Marshal(i)
if err != nil {
panic(err)
fatal(err)
}
fmt.Println(string(s))
}

func reduce(value otto.Value, code string) (otto.Value, error) {
if err := vm.Set("json", value); err != nil {
panic(err)
fatal(err)
}
switch code {
case "?":
Expand Down Expand Up @@ -94,3 +94,8 @@ func usage() {
`)
}

func fatal(err error) {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
10 changes: 0 additions & 10 deletions test.sh

This file was deleted.

0 comments on commit 686487d

Please sign in to comment.