Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow editing toml in place #171

Merged
merged 2 commits into from
Sep 1, 2023
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
9 changes: 8 additions & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def test_roundtrip_yaml(self):
with io.open(cfn_filename) as fh:
self.assertEqual(self.run_yq("", ["-Y", ".", cfn_filename]), fh.read())

def test_in_place(self):
def test_in_place_yaml(self):
with tempfile.NamedTemporaryFile() as tf, tempfile.NamedTemporaryFile() as tf2:
tf.write(b"- foo\n- bar\n")
tf.seek(0)
Expand All @@ -202,6 +202,13 @@ def test_in_place(self):
self.assertEqual(tf.read(), b"foo\n...\n")
self.assertEqual(tf2.read(), b"foo\n...\n")

def test_in_place_toml(self):
with tempfile.NamedTemporaryFile() as tf:
tf.write(b'[GLOBAL]\nversion="1.0.0"\n')
tf.seek(0)
self.run_yq("", ["-i", "-t", '.GLOBAL.version="1.0.1"', tf.name], input_format="toml")
self.assertEqual(tf.read(), b'[GLOBAL]\nversion = "1.0.1"\n')

def test_explicit_doc_markers(self):
test_doc = os.path.join(os.path.dirname(__file__), "doc.yml")
self.assertTrue(self.run_yq("", ["-y", ".", test_doc]).startswith("yaml_struct"))
Expand Down
4 changes: 2 additions & 2 deletions yq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ def cli(args=None, input_format="yaml", program_name="yq"):

yq_args = dict(input_format=input_format, program_name=program_name, jq_args=jq_args, **vars(args))
if in_place:
if args.output_format not in {"yaml", "annotated_yaml"}:
sys.exit("{}: -i/--in-place can only be used with -y/-Y".format(program_name))
if args.output_format not in {"yaml", "annotated_yaml", "toml"}:
sys.exit("{}: -i/--in-place can only be used with -y/-Y/-t".format(program_name))
input_streams = yq_args.pop("input_streams")
if len(input_streams) == 1 and input_streams[0].name == "<stdin>":
msg = "{}: -i/--in-place can only be used with filename arguments, not on standard input"
Expand Down