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 +4 -4
- data/AGENTS.md +1 -0
- data/README.md +14 -1
- data/Rakefile +3 -3
- data/lib/yaml_smith/cli.rb +9 -3
- data/lib/yaml_smith/delete_key.rb +21 -4
- data/lib/yaml_smith/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 03e70b6c6bd6522e6a35265fc2906e5fdc05e1e5a3bb497024e7805eb89cdc2e
|
|
4
|
+
data.tar.gz: a3b8572f3a9ead73fc96d1eb4795a995ac17a2ade613dc54334ee8c7b2e59843
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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
|
|
4
|
-
require
|
|
3
|
+
require 'bundler/gem_tasks'
|
|
4
|
+
require 'minitest/test_task'
|
|
5
5
|
|
|
6
6
|
Minitest::TestTask.create
|
|
7
7
|
|
|
8
|
-
require
|
|
8
|
+
require 'rubocop/rake_task'
|
|
9
9
|
|
|
10
10
|
RuboCop::RakeTask.new
|
|
11
11
|
|
data/lib/yaml_smith/cli.rb
CHANGED
|
@@ -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
|
|
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
|
-
|
|
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
|
data/lib/yaml_smith/version.rb
CHANGED