Skip to content
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
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ go install -v github.com/noperator/jqfmt/cmd/jqfmt@latest
𝄢 jqfmt -h
Usage of jqfmt:
-ar
arrays
break array literals, one element per line
-f string
file
-o one line
read jq from this file instead of stdin
-fn string
comma-separated function names to break the line before
-if
break if/elif/else/end onto their own lines
-o collapse to a single canonical line
-ob
objects
break object literals, one key per line
-op string
operators
-v verbose
comma-separated operators to break on (pipe,comma,add,...)
```

Let's take this line of jq…
Expand Down Expand Up @@ -111,6 +114,19 @@ It'll read easier if we also break on pipes.
}
```

Word operators (`and`/`or`) break _before_ the operator, the way you'd write a boolean chain by hand — while pipes and commas still trail.

```
𝄢 echo '{name: .n, ok: select(.active and .verified and .paid)}' |
jqfmt -ob -op pipe,and
{
name: .n,
ok: select(.active
and .verified
and .paid)
}
```

<details><summary>Full list of valid operators</summary>
<p>

Expand Down
94 changes: 42 additions & 52 deletions cmd/jqfmt/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Command jqfmt formats a jq program. It reads from a file (-f) or stdin and
// writes the formatted result to stdout.
package main

import (
Expand All @@ -8,68 +10,56 @@ import (
"strings"

"github.com/noperator/jqfmt"
log "github.com/sirupsen/logrus"
)

func assertErrorToNilf(message string, err error) {
if err != nil {
log.Fatalf(message, err)
}
}

func main() {
// funcsStr := flag.String("fn", "", "functions")
opsStr := flag.String("op", "", "operators")
obj := flag.Bool("ob", false, "objects")
arr := flag.Bool("ar", false, "arrays")
oneLn := flag.Bool("o", false, "one line")
file := flag.String("f", "", "file")
verbose := flag.Bool("v", false, "verbose")
// noHang := flag.Bool("nh", false, "no hanging indent")
ops := flag.String("op", "", "comma-separated operators to break on (pipe,comma,add,...)")
funcs := flag.String("fn", "", "comma-separated function names to break the line before")
obj := flag.Bool("ob", false, "break object literals, one key per line")
arr := flag.Bool("ar", false, "break array literals, one element per line")
ifBrk := flag.Bool("if", false, "break if/elif/else/end onto their own lines")
oneLn := flag.Bool("o", false, "collapse to a single canonical line")
file := flag.String("f", "", "read jq from this file instead of stdin")
flag.Parse()
var from_stdin bool = false

if *verbose {
log.SetLevel(log.DebugLevel)
}
cfg, err := jqfmt.ValidateConfig(jqfmt.Config{
Ops: splitList(*ops),
Funcs: splitList(*funcs),
Obj: *obj,
Arr: *arr,
If: *ifBrk,
OneLn: *oneLn,
})
fail("invalid config", err)

if *file == "" {
from_stdin = true
}
src, err := read(*file)
fail("could not read input", err)

// var funcs []string
// if *funcsStr == "" {
// funcs = []string{}
// } else {
// funcs = strings.Split(*funcsStr, ",")
// }
out, err := jqfmt.Format(src, cfg)
fail("could not format jq", err)

var ops []string
if *opsStr == "" {
ops = []string{}
} else {
ops = strings.Split(*opsStr, ",")
fmt.Print(out)
}

func splitList(s string) []string {
if s == "" {
return nil
}
return strings.Split(s, ",")
}

cliCfg, err := jqfmt.ValidateConfig(jqfmt.JqFmtCfg{
Arr: *arr,
// Funcs: funcs,
Obj: *obj,
OneLn: *oneLn,
Ops: ops,
// Hang: !(*noHang),
})
assertErrorToNilf("invalid config: %v", err)
func read(file string) (string, error) {
if file == "" {
b, err := io.ReadAll(os.Stdin)
return string(b), err
}
b, err := os.ReadFile(file)
return string(b), err
}

var jqBytes []byte
if from_stdin {
jqBytes, err = io.ReadAll(os.Stdin)
} else {
jqBytes, err = os.ReadFile(*file)
func fail(msg string, err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", msg, err)
os.Exit(1)
}
assertErrorToNilf("could not read file: %v", err)
jqStr := string(jqBytes)
jqStrFmt, err := jqfmt.DoThing(jqStr, cliCfg)
assertErrorToNilf("could not format jq: %v", err)
fmt.Print(jqStrFmt)
}
28 changes: 0 additions & 28 deletions export.go

This file was deleted.

Loading