Skip to content
Merged
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
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2024 Michał Adamczyk
Copyright (c) 2025 Michał Adamczyk

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
<a href="https://opensource.org/licenses/MIT" rel="nofollow">
<img alt="MIT license" src="https://img.shields.io/github/license/mdm-code/tq">
</a>
<a href="https://goreportcard.com/report/github.com/mdm-code/tq">
<img alt="Go report card" src="https://goreportcard.com/badge/github.com/mdm-code/tq">
<a href="https://goreportcard.com/report/github.com/mdm-code/tq/v2">
<img alt="Go report card" src="https://goreportcard.com/badge/github.com/mdm-code/tq/v2">
</a>
<a href="https://pkg.go.dev/github.com/mdm-code/tq/v2">
<img alt="Go package docs" src="https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white">
Expand Down Expand Up @@ -137,13 +137,13 @@ to an iterator with `[]`, and then (5) query each element of the iterator for


```sh
tq -q '
<< EOF tq -q '
.runners[]
.kubernetes
.volumes
.host_path[]
."host path"
' << EOF
'
[session_server]
session_timeout = 1800

Expand Down Expand Up @@ -190,10 +190,12 @@ tq -q '
pod_annotations_overwrite_allowed = ""
[runners.kubernetes.volumes]
EOF
```

```txt
Output:

'/home/core/data/gitlab-runner/data'
/home/core/data/gitlab-runner/data
```


Expand All @@ -205,7 +207,7 @@ with `[]`, and then (3) the IP address is recovered from each of the objects
with the quoted key `"ip"`.

```sh
tq -q '.servers[]."ip"' <<EOF
<<EOF tq -q '.servers[]."ip"'
[servers]

[servers.prod]
Expand All @@ -216,11 +218,13 @@ role = "backend"
ip = "10.0.0.2"
role = "backend"
EOF
```

```txt
Output:

'10.0.0.1'
'10.0.0.2'
10.0.0.1
10.0.0.2
```


Expand All @@ -231,10 +235,12 @@ all ports aside from the first one assigned to the first database record on the
list.

```sh
tq -q '.["databases"][0]["ports"][1:][]' <<EOF
<<EOF tq -q '.["databases"][0]["ports"][1:][]'
databases = [ {enabled = true, ports = [ 5432, 5433, 5434 ]} ]
EOF
```

```txt
Output:

5433
Expand All @@ -248,7 +254,7 @@ If you don't feel like installing `tq` with `go install`, you can test `tq` out
running inside of a container with this command:

```sh
docker run -i ghcr.io/mdm-code/tq:latest tq -q ".dependencies.ignore" <<EOF
<<EOF docker run -i ghcr.io/mdm-code/tq:latest tq -q ".dependencies.ignore"
[dependencies]
anyhow = "1.0.75"
bstr = "1.7.0"
Expand All @@ -262,6 +268,13 @@ textwrap = { version = "0.16.0", default-features = false }
EOF
```

```txt
Output:

path = 'crates/ignore'
version = '0.4.22'
```


## Development

Expand Down
6 changes: 3 additions & 3 deletions cmd/tq/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Options:

Example:

tq -q '.servers[].ip' <<EOF
<<EOF tq -q .servers[].ip
[servers]

[servers.prod]
Expand All @@ -30,8 +30,8 @@ Example:

Output:

'10.0.0.1'
'10.0.0.2'
10.0.0.1
10.0.0.2

Tq is a tool for querying TOML configuration files with a sequence of intuitive
filters. It works as a regular Unix filter program reading input data from the
Expand Down
4 changes: 2 additions & 2 deletions cmd/tq/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestMain(t *testing.T) {
if err != nil {
t.Errorf("should not return an error: %s", err)
}
want := `'red'
'green'
want := `red
green
`
if have := output.String(); have != want {
t.Errorf("have: %s\nwant: %s", have, want)
Expand Down
6 changes: 3 additions & 3 deletions cmd/tq/usage.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Options:

Example:

tq -q '.servers[].ip' <<EOF
<<EOF tq -q .servers[].ip
[servers]

[servers.prod]
Expand All @@ -29,8 +29,8 @@ Example:

Output:

'10.0.0.1'
'10.0.0.2'
10.0.0.1
10.0.0.2

Tq is a tool for querying TOML configuration files with a sequence of intuitive
filters. It works as a regular Unix filter program reading input data from the
Expand Down
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ role = "backend"
_ = tq.Run(input, &output, query)
fmt.Println(output.String())
// Output:
// '10.0.0.1'
// 10.0.0.1
}

// ExampleTq_Validate shows how to use the Tq struct to validate whether a
Expand Down
14 changes: 5 additions & 9 deletions internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package interpreter

import (
"fmt"

"github.com/mdm-code/tq/v2/internal/ast"
)

Expand Down Expand Up @@ -110,7 +108,7 @@ func (i *Interpreter) VisitSpan(e ast.Expr) {
default:
err = &Error{
data: d,
filter: fmt.Sprintf("%s", span),
filter: span.String(),
err: ErrTOMLDataType,
}
}
Expand All @@ -136,13 +134,11 @@ func (i *Interpreter) VisitIterator(e ast.Expr) {
result = append(result, val)
}
case []any:
for _, val := range v {
result = append(result, val)
}
result = append(result, v...)
default:
err = &Error{
data: d,
filter: fmt.Sprintf("%s", iter),
filter: iter.String(),
err: ErrTOMLDataType,
}
}
Expand Down Expand Up @@ -172,7 +168,7 @@ func (i *Interpreter) VisitString(e ast.Expr) {
default:
err = &Error{
data: d,
filter: fmt.Sprintf("%s", str),
filter: str.String(),
err: ErrTOMLDataType,
}
}
Expand Down Expand Up @@ -201,7 +197,7 @@ func (i *Interpreter) VisitInteger(e ast.Expr) {
default:
err = &Error{
data: d,
filter: fmt.Sprintf("%s", integer),
filter: integer.String(),
err: ErrTOMLDataType,
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
// lexerOffsetStart declares the initial Lexer offset.
lexerOffsetStart = 0

// lexerLineOffsetStart declares the inital Lexer line offset.
// lexerLineOffsetStart declares the initial Lexer line offset.
lexerLineOffsetStart = 0
)

Expand Down
19 changes: 12 additions & 7 deletions tq.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,19 @@
return err
}
for _, d := range filteredData {
bytes, err := t.adapter.Marshal(d)
if err != nil {
return err
var bytes []byte
var err error
switch v := d.(type) {
// NOTE: Single strings get quoted when marshalled, and it is misleading.
case string:
bytes = []byte(v)
default:
bytes, err = t.adapter.Marshal(v)
if err != nil {
return err
}

Check warning on line 99 in tq.go

View check run for this annotation

Codecov / codecov/patch

tq.go#L95-L99

Added lines #L95 - L99 were not covered by tests
}
if len(bytes) == 0 {
continue
}
fmt.Fprintln(output, string(bytes))
fmt.Fprintf(output, "%s\n", strings.TrimSpace(string(bytes)))
}
return nil
}
Loading