Skip to content

Commit c34785e

Browse files
authored
support inplace editing from stdin (#34)
Signed-off-by: suryapandian <[email protected]>
1 parent 60b0523 commit c34785e

File tree

3 files changed

+17
-2
lines changed

3 files changed

+17
-2
lines changed

Diff for: test/secret.yaml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
apiVersion: v1
2+
data:
3+
color: aHR0cHM6Ly9zdXJ5YXBhbmRpYW4ud29yZHByZXNzLmNvbS8=
4+
kind: Secret
5+
metadata:
6+
creationTimestamp: "2042-11-04T10:11:22Z"
7+
name: test-secret
8+
namespace: test-namespace
9+
type: Opaque

Diff for: test/yq.test.bats

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838
rm test/output.json
3939
}
4040

41+
@test "yq_in_place_edit" {
42+
cat test/secret.yaml | yq -i '.metadata.name="updated-name"' > test/output.yaml
43+
cat test/output.yaml | yq '.metadata.name' | grep 'updated-name'
44+
rm test/output.yaml
45+
}
46+
4147
@test "exit_codes" {
4248
run yq -h
4349
[ "$status" -eq 0 ]

Diff for: yq.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct Args {
6565
toml_output: bool,
6666

6767
/// Edit the input file in place
68-
#[arg(short, long, default_value = "false", requires = "file")]
68+
#[arg(short, long, default_value = "false")]
6969
in_place: bool,
7070

7171
/// Query to be sent to jq (see https://jqlang.github.io/jq/manual/)
@@ -292,7 +292,7 @@ fn main() -> Result<()> {
292292
let input = args.read_input()?;
293293
let stdout = args.shellout(input)?;
294294
let output = args.output(stdout)?;
295-
if args.in_place {
295+
if args.in_place && args.file.is_some() {
296296
let f = args.file.unwrap(); // required
297297
std::fs::write(f, output + "\n")?;
298298
} else {

0 commit comments

Comments
 (0)