yaml_smith 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d7ff73d942d1f809bbac011b168ef43cd3ce7b8d193292c661e6d2d65a358498
4
- data.tar.gz: 4e6fe2d3391a53d6fd9d364d0b87061342409033ba9be4e29520eb8ab75b4e20
3
+ metadata.gz: 03e70b6c6bd6522e6a35265fc2906e5fdc05e1e5a3bb497024e7805eb89cdc2e
4
+ data.tar.gz: a3b8572f3a9ead73fc96d1eb4795a995ac17a2ade613dc54334ee8c7b2e59843
5
5
  SHA512:
6
- metadata.gz: 0da49b2b19add33c7a5137624bb6347c39c87ea6bc203c7ad416a77e026bf3c387b3ca5f0fbb10e550716f50c6729592c4ffa8b1204ef255d808e5c15782baf4
7
- data.tar.gz: 241861d560bdce07bdfe246c11618f83750a00f31d338a140c820147faf9482df223ff115df8fcf0fe6b29db93f4d1544cb82efd19be7422ba304a8b59662609
6
+ metadata.gz: 49168721a967966498b840fc55af20beaad3be787c756cfb28aa4d08c55011f1d8cfbef4f0af5216278aceee1efce14c2f89b9cb39ff24d6bc0379e57afe09cb
7
+ data.tar.gz: b7b6467e1f366f016ca038e5a7ecd2e54b55f72b9e6b071bd53ffaf2edf0726f46faa3b635d9dbb5e55d13b6e4e93ee943f52fec3bc95493c2404330b0bec03c
data/AGENTS.md CHANGED
@@ -20,3 +20,4 @@
20
20
  - Load existing files and values with `comments: true`.
21
21
  - Values passed on the command line are YAML and should retain their YAML types.
22
22
  - File edits are written through a same-directory temporary file and rename; preserve this atomic-write behavior.
23
+ - In tests, use squiggly heredocs (`<<~YAML`) for multiline YAML fixtures and expected YAML output so the structure remains readable.
data/README.md CHANGED
@@ -34,12 +34,25 @@ Set or replace a top-level key:
34
34
  yaml-smith set config.yml timeout 60
35
35
  ```
36
36
 
37
- Delete a top-level key:
37
+ Delete matching keys recursively:
38
38
 
39
39
  ```bash
40
40
  yaml-smith delete config.yml timeout
41
41
  ```
42
42
 
43
+ Delete searches mappings nested inside arrays and other mappings. For example,
44
+ this removes the `WEB_CONCURRENCY` entry from a nested `envVars` list:
45
+
46
+ ```bash
47
+ yaml-smith delete render.yaml WEB_CONCURRENCY
48
+ ```
49
+
50
+ Use `--top-level-only` to restrict deletion to the document's top-level keys:
51
+
52
+ ```bash
53
+ yaml-smith delete config.yml timeout --top-level-only
54
+ ```
55
+
43
56
  ## Development
44
57
 
45
58
  After checking out the repo, run `bin/setup` to install dependencies. Then,
data/Rakefile CHANGED
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "bundler/gem_tasks"
4
- require "minitest/test_task"
3
+ require 'bundler/gem_tasks'
4
+ require 'minitest/test_task'
5
5
 
6
6
  Minitest::TestTask.create
7
7
 
8
- require "rubocop/rake_task"
8
+ require 'rubocop/rake_task'
9
9
 
10
10
  RuboCop::RakeTask.new
11
11
 
@@ -10,7 +10,7 @@ module YamlSmith
10
10
  'set' => SetKey,
11
11
  'delete' => DeleteKey
12
12
  }.freeze
13
- USAGE = 'Usage: yaml-smith COMMAND FILE KEY [VALUE]'
13
+ USAGE = 'Usage: yaml-smith COMMAND FILE KEY [VALUE] [--top-level-only]'
14
14
 
15
15
  def self.call(arguments)
16
16
  new(arguments).call
@@ -24,7 +24,7 @@ module YamlSmith
24
24
  command = COMMANDS[@arguments.first]
25
25
  return usage unless valid_arguments?(command)
26
26
 
27
- command.call(*command_arguments)
27
+ command.call(*command_arguments, **command_options)
28
28
  rescue Error => e
29
29
  warn e.message
30
30
  1
@@ -43,7 +43,13 @@ module YamlSmith
43
43
  end
44
44
 
45
45
  def command_arguments
46
- @arguments.drop(1)
46
+ @arguments.drop(1).reject { |argument| argument == '--top-level-only' }
47
+ end
48
+
49
+ def command_options
50
+ return {} unless delete_command?
51
+
52
+ { top_level_only: @arguments.include?('--top-level-only') }
47
53
  end
48
54
 
49
55
  def usage
@@ -1,16 +1,33 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YamlSmith
4
- # Deletes a top-level key from a YAML file.
4
+ # Deletes matching keys from a YAML file.
5
5
  class DeleteKey < FileCommand
6
- def call
6
+ def call(top_level_only: false)
7
7
  document = load_file(@path)
8
8
 
9
9
  ensure_mapping(document)
10
- raise Error, "key does not exist: #{@key}" unless document.key?(@key)
10
+ deleted = top_level_only ? !document.delete(@key).nil? : delete_key(document)
11
+ raise Error, "key does not exist: #{@key}" unless deleted
11
12
 
12
- document.delete(@key)
13
13
  write(Psych::Pure.dump(document))
14
14
  end
15
+
16
+ private
17
+
18
+ def delete_key(value)
19
+ return delete_from_array(value) if value.instance_of?(Psych::Pure::LoadedArray)
20
+ return false unless value.instance_of?(Psych::Pure::LoadedHash)
21
+
22
+ deleted = value.key?(@key)
23
+ value.delete(@key)
24
+ value.each_value { |child| deleted = delete_key(child) || deleted }
25
+ deleted
26
+ end
27
+
28
+ def delete_from_array(value)
29
+ deleted = value.delete_if { |child| child.instance_of?(Psych::Pure::LoadedHash) && child.key?(@key) }.any?
30
+ value.any? { |child| delete_key(child) } || deleted
31
+ end
15
32
  end
16
33
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module YamlSmith
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yaml_smith
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Waite