Skip to content

Commit 765597f

Browse files
authored
Merge pull request #41 from mdm-code/remove-trailing-newline
Remove quotes around single output strings; remove trailing newline character
2 parents 6d3e0c9 + 21f7822 commit 765597f

File tree

9 files changed

+51
-37
lines changed

9 files changed

+51
-37
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 Michał Adamczyk
3+
Copyright (c) 2025 Michał Adamczyk
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<a href="https://opensource.org/licenses/MIT" rel="nofollow">
2323
<img alt="MIT license" src="https://img.shields.io/github/license/mdm-code/tq">
2424
</a>
25-
<a href="https://goreportcard.com/report/github.com/mdm-code/tq">
26-
<img alt="Go report card" src="https://goreportcard.com/badge/github.com/mdm-code/tq">
25+
<a href="https://goreportcard.com/report/github.com/mdm-code/tq/v2">
26+
<img alt="Go report card" src="https://goreportcard.com/badge/github.com/mdm-code/tq/v2">
2727
</a>
2828
<a href="https://pkg.go.dev/github.com/mdm-code/tq/v2">
2929
<img alt="Go package docs" src="https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white">
@@ -137,13 +137,13 @@ to an iterator with `[]`, and then (5) query each element of the iterator for
137137

138138

139139
```sh
140-
tq -q '
140+
<< EOF tq -q '
141141
.runners[]
142142
.kubernetes
143143
.volumes
144144
.host_path[]
145145
."host path"
146-
' << EOF
146+
'
147147
[session_server]
148148
session_timeout = 1800
149149
@@ -190,10 +190,12 @@ tq -q '
190190
pod_annotations_overwrite_allowed = ""
191191
[runners.kubernetes.volumes]
192192
EOF
193+
```
193194

195+
```txt
194196
Output:
195197
196-
'/home/core/data/gitlab-runner/data'
198+
/home/core/data/gitlab-runner/data
197199
```
198200

199201

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

207209
```sh
208-
tq -q '.servers[]."ip"' <<EOF
210+
<<EOF tq -q '.servers[]."ip"'
209211
[servers]
210212
211213
[servers.prod]
@@ -216,11 +218,13 @@ role = "backend"
216218
ip = "10.0.0.2"
217219
role = "backend"
218220
EOF
221+
```
219222

223+
```txt
220224
Output:
221225
222-
'10.0.0.1'
223-
'10.0.0.2'
226+
10.0.0.1
227+
10.0.0.2
224228
```
225229

226230

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

233237
```sh
234-
tq -q '.["databases"][0]["ports"][1:][]' <<EOF
238+
<<EOF tq -q '.["databases"][0]["ports"][1:][]'
235239
databases = [ {enabled = true, ports = [ 5432, 5433, 5434 ]} ]
236240
EOF
241+
```
237242

243+
```txt
238244
Output:
239245
240246
5433
@@ -248,7 +254,7 @@ If you don't feel like installing `tq` with `go install`, you can test `tq` out
248254
running inside of a container with this command:
249255

250256
```sh
251-
docker run -i ghcr.io/mdm-code/tq:latest tq -q ".dependencies.ignore" <<EOF
257+
<<EOF docker run -i ghcr.io/mdm-code/tq:latest tq -q ".dependencies.ignore"
252258
[dependencies]
253259
anyhow = "1.0.75"
254260
bstr = "1.7.0"
@@ -262,6 +268,13 @@ textwrap = { version = "0.16.0", default-features = false }
262268
EOF
263269
```
264270

271+
```txt
272+
Output:
273+
274+
path = 'crates/ignore'
275+
version = '0.4.22'
276+
```
277+
265278

266279
## Development
267280

cmd/tq/doc.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Options:
1616
1717
Example:
1818
19-
tq -q '.servers[].ip' <<EOF
19+
<<EOF tq -q .servers[].ip
2020
[servers]
2121
2222
[servers.prod]
@@ -30,8 +30,8 @@ Example:
3030
3131
Output:
3232
33-
'10.0.0.1'
34-
'10.0.0.2'
33+
10.0.0.1
34+
10.0.0.2
3535
3636
Tq is a tool for querying TOML configuration files with a sequence of intuitive
3737
filters. It works as a regular Unix filter program reading input data from the

cmd/tq/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ func TestMain(t *testing.T) {
4444
if err != nil {
4545
t.Errorf("should not return an error: %s", err)
4646
}
47-
want := `'red'
48-
'green'
47+
want := `red
48+
green
4949
`
5050
if have := output.String(); have != want {
5151
t.Errorf("have: %s\nwant: %s", have, want)

cmd/tq/usage.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Options:
1515

1616
Example:
1717

18-
tq -q '.servers[].ip' <<EOF
18+
<<EOF tq -q .servers[].ip
1919
[servers]
2020

2121
[servers.prod]
@@ -29,8 +29,8 @@ Example:
2929

3030
Output:
3131

32-
'10.0.0.1'
33-
'10.0.0.2'
32+
10.0.0.1
33+
10.0.0.2
3434

3535
Tq is a tool for querying TOML configuration files with a sequence of intuitive
3636
filters. It works as a regular Unix filter program reading input data from the

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ role = "backend"
3838
_ = tq.Run(input, &output, query)
3939
fmt.Println(output.String())
4040
// Output:
41-
// '10.0.0.1'
41+
// 10.0.0.1
4242
}
4343

4444
// ExampleTq_Validate shows how to use the Tq struct to validate whether a

internal/interpreter/interpreter.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package interpreter
22

33
import (
4-
"fmt"
5-
64
"github.com/mdm-code/tq/v2/internal/ast"
75
)
86

@@ -110,7 +108,7 @@ func (i *Interpreter) VisitSpan(e ast.Expr) {
110108
default:
111109
err = &Error{
112110
data: d,
113-
filter: fmt.Sprintf("%s", span),
111+
filter: span.String(),
114112
err: ErrTOMLDataType,
115113
}
116114
}
@@ -136,13 +134,11 @@ func (i *Interpreter) VisitIterator(e ast.Expr) {
136134
result = append(result, val)
137135
}
138136
case []any:
139-
for _, val := range v {
140-
result = append(result, val)
141-
}
137+
result = append(result, v...)
142138
default:
143139
err = &Error{
144140
data: d,
145-
filter: fmt.Sprintf("%s", iter),
141+
filter: iter.String(),
146142
err: ErrTOMLDataType,
147143
}
148144
}
@@ -172,7 +168,7 @@ func (i *Interpreter) VisitString(e ast.Expr) {
172168
default:
173169
err = &Error{
174170
data: d,
175-
filter: fmt.Sprintf("%s", str),
171+
filter: str.String(),
176172
err: ErrTOMLDataType,
177173
}
178174
}
@@ -201,7 +197,7 @@ func (i *Interpreter) VisitInteger(e ast.Expr) {
201197
default:
202198
err = &Error{
203199
data: d,
204-
filter: fmt.Sprintf("%s", integer),
200+
filter: integer.String(),
205201
err: ErrTOMLDataType,
206202
}
207203
}

internal/lexer/lexer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const (
1212
// lexerOffsetStart declares the initial Lexer offset.
1313
lexerOffsetStart = 0
1414

15-
// lexerLineOffsetStart declares the inital Lexer line offset.
15+
// lexerLineOffsetStart declares the initial Lexer line offset.
1616
lexerLineOffsetStart = 0
1717
)
1818

tq.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,19 @@ func (t *Tq) Run(input io.Reader, output io.Writer, query string) error {
8686
return err
8787
}
8888
for _, d := range filteredData {
89-
bytes, err := t.adapter.Marshal(d)
90-
if err != nil {
91-
return err
89+
var bytes []byte
90+
var err error
91+
switch v := d.(type) {
92+
// NOTE: Single strings get quoted when marshalled, and it is misleading.
93+
case string:
94+
bytes = []byte(v)
95+
default:
96+
bytes, err = t.adapter.Marshal(v)
97+
if err != nil {
98+
return err
99+
}
92100
}
93-
if len(bytes) == 0 {
94-
continue
95-
}
96-
fmt.Fprintln(output, string(bytes))
101+
fmt.Fprintf(output, "%s\n", strings.TrimSpace(string(bytes)))
97102
}
98103
return nil
99104
}

0 commit comments

Comments
 (0)