yaml_structure_checker 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c5b1ce6fcb49b5f7a073c2f0192e764bd31cd2548913fc142417f2177201049e
4
+ data.tar.gz: a21da4ba0ac21d7739ddaa274f7aad1a8fcf2a55dcf79376b64f4971e75a5bec
5
+ SHA512:
6
+ metadata.gz: 3253e65b6567752fec9a7bd2bfeb014a89f81c52e2790bfa9f0bafa50e3f3bca3dbcf4bc6eaae6bb1ec9b0a1a6cdfccbed01ea3c74a91f3510bb34a1968b906d
7
+ data.tar.gz: 0e945ec8fc8b7bd4a308355b8873a46ee751fa954363ca51b8d8bc51b4cf62e7718721986b11576fb9c6608e6f808215edd6521e2788ed0f403b90a905e0d4d0
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-04-06
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at TODO: Write your email address. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Dockerfile ADDED
@@ -0,0 +1,8 @@
1
+ FROM ruby:3.2.2
2
+
3
+ RUN apt-get update -qq && apt-get install -y \
4
+ postgresql-client
5
+
6
+ RUN gem update bundler
7
+
8
+ WORKDIR /app
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in yaml_structure_checker.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ yaml_structure_checker (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.5.0)
10
+ rake (13.0.6)
11
+ rspec (3.12.0)
12
+ rspec-core (~> 3.12.0)
13
+ rspec-expectations (~> 3.12.0)
14
+ rspec-mocks (~> 3.12.0)
15
+ rspec-core (3.12.1)
16
+ rspec-support (~> 3.12.0)
17
+ rspec-expectations (3.12.2)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.12.0)
20
+ rspec-mocks (3.12.5)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.12.0)
23
+ rspec-support (3.12.0)
24
+
25
+ PLATFORMS
26
+ x86_64-linux
27
+
28
+ DEPENDENCIES
29
+ rake (~> 13.0)
30
+ rspec (~> 3.0)
31
+ yaml_structure_checker!
32
+
33
+ BUNDLED WITH
34
+ 2.4.10
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 yosi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 TODO: Write your name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,138 @@
1
+ # Yaml structure checker
2
+
3
+ This Gem can detect that the keys in the yaml file do not match for each environment.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
16
+
17
+ ## Usage
18
+
19
+ ### Add configuration file
20
+
21
+ Add the configuration file `config/yaml_structure_checker.yml`.
22
+
23
+ Yaml structure checker loads `config/yaml_structure_checker.yml` by default.
24
+
25
+ An example follows
26
+
27
+ ```yaml
28
+ # config/yaml_structure_checker.yml
29
+ include_patterns:
30
+ - config/**/*.yml
31
+ exclude_patterns:
32
+ - config/locales/**/*.yml
33
+ envs:
34
+ - development
35
+ - test
36
+ - integration
37
+ - production
38
+ skip_paths:
39
+ - config/gcp.yml
40
+ ```
41
+
42
+ | | |
43
+ | ---------------- | ------------------------------------------------------------------------- |
44
+ | include_patterns | Specifies the pattern of files to inspect for keys. |
45
+ | exclude_patterns | Specifies patterns to exclude from include_patterns. |
46
+ | envs | Specifies the environment in which comparisons will be made by the check. |
47
+ | skip_paths | Specifies file paths to exclude from include_patterns. |
48
+ | | |
49
+
50
+ ### Check yaml files
51
+
52
+ Run the `yaml_structure_checker` command to check your Yaml files.
53
+
54
+ ```bash
55
+ $ bundle exec yaml_structure_checker
56
+
57
+ #################################
58
+ # Yaml Structure Check #
59
+ #################################
60
+
61
+ Exclude paths:
62
+ spec/fixtures/checker/target/exclude/example.yml
63
+ spec/fixtures/checker/target/failed/alias.yml
64
+ spec/fixtures/checker/target/failed/diff.yml
65
+
66
+ Skip paths:
67
+ spec/fixtures/checker/target/skip/example.yml
68
+
69
+
70
+ # spec/fixtures/checker/target/success/alias.yml
71
+ Result: OK
72
+
73
+
74
+ # spec/fixtures/checker/target/success/same.yml
75
+ Result: OK
76
+
77
+ #################################
78
+ # Yaml Structure Check Result #
79
+ #################################
80
+
81
+ NG paths:
82
+ nothing
83
+
84
+ Total count: 6
85
+ Exclude count: 3
86
+ Skip count: 1
87
+ OK count: 2
88
+ NG count: 0
89
+ ```
90
+
91
+ If you want to use a configuration file other than `config/yaml_structure_checker.yml`, do the following
92
+
93
+ ```bash
94
+ $ bundle exec yaml_structure_checker other_yaml_faile_path.yml
95
+ ```
96
+
97
+ ### Run on CI
98
+
99
+ It is recommended to run it through CI.
100
+
101
+ The following is an example of Github Actions.
102
+
103
+ ```yaml
104
+ name: CI
105
+
106
+ on: push
107
+
108
+ jobs:
109
+ ci:
110
+ name: CI
111
+ runs-on: ubuntu-latest
112
+ container:
113
+ image: ruby:3.2.2
114
+ steps:
115
+ - uses: actions/checkout@v2
116
+ - name: Bundle install
117
+ run: bundle install --path=vendor/bundle --jobs 4 --retry 3
118
+ - name: Yaml structure checker
119
+ run: bundle exec yaml_structure_checker
120
+ ```
121
+
122
+ ## Development
123
+
124
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
125
+
126
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
127
+
128
+ ## Contributing
129
+
130
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yaml_structure_checker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/yaml_structure_checker/blob/master/CODE_OF_CONDUCT.md).
131
+
132
+ ## License
133
+
134
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
135
+
136
+ ## Code of Conduct
137
+
138
+ Everyone interacting in the YamlStructureChecker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/yaml_structure_checker/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
@@ -0,0 +1,11 @@
1
+ version: "3"
2
+
3
+ services:
4
+ app:
5
+ build: .
6
+ volumes:
7
+ - ".:/app"
8
+ - bundle:/usr/local/bundle
9
+
10
+ volumes:
11
+ bundle:
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'yaml_structure_checker'
5
+
6
+ if !ARGV.empty?
7
+ settings_path = ARGV.first
8
+ end
9
+
10
+ YamlStructureChecker::Runner.invoke(settings_path)
@@ -0,0 +1,161 @@
1
+ module YamlStructureChecker
2
+ class Checker
3
+ attr_accessor :ok_count,
4
+ :ng_count,
5
+ :ng_paths
6
+
7
+ def initialize
8
+ self.ok_count = 0
9
+ self.ng_count = 0
10
+ self.ng_paths = []
11
+ end
12
+
13
+ def test_yamls(settings_path)
14
+ print_start_title
15
+
16
+ loader = Loader.new(settings_path)
17
+ print_exclude_paths(loader.exclude_paths)
18
+ print_skip_paths(loader.skip_paths)
19
+
20
+ results = loader.target_paths.map do |target_path|
21
+ test_yaml(target_path, loader.envs)
22
+ end
23
+
24
+ print_result_title
25
+
26
+ print_ng_paths(self.ng_paths)
27
+
28
+ puts "Total count: #{loader.total_count}"
29
+ puts "Exclude count: #{loader.exclude_paths.size}"
30
+ puts "Skip count: #{loader.skip_paths.size}"
31
+ puts "OK count: #{ok_count}"
32
+ puts "NG count: #{ng_count}"
33
+
34
+ raise Errors::StructureError if results.include?(false)
35
+ end
36
+
37
+ def test_yaml(path, envs)
38
+ puts "\n# #{path}"
39
+
40
+ yaml = Loader.yaml_load_file(path)
41
+
42
+ nested_keys = Converter.hash_to_nested_keys(yaml)
43
+
44
+ nested_keys_each_env =
45
+ Converter.nested_keys_to_nested_keys_each_env(
46
+ nested_keys,
47
+ envs
48
+ )
49
+
50
+ result = compare(nested_keys_each_env, envs)
51
+ if result[:success]
52
+ puts 'Result: OK'
53
+ self.ok_count += 1
54
+ else
55
+ puts 'Result: NG'
56
+ self.ng_count += 1
57
+ self.ng_paths.push(path)
58
+ end
59
+ puts result[:compare_message]
60
+
61
+ result[:success]
62
+ end
63
+
64
+ def compare(nested_keys_each_env, envs)
65
+ compare_message = ""
66
+
67
+ if !(envs - nested_keys_each_env.keys).empty?
68
+ compare_message += "There is no key:\n"
69
+ compare_message += " #{envs - nested_keys_each_env.keys}"
70
+ return {
71
+ success: false,
72
+ compare_message: compare_message,
73
+ }
74
+ end
75
+
76
+ first_env = envs.first
77
+ other_envs = envs.drop(1)
78
+
79
+ success = true
80
+ other_envs.each do |other_env|
81
+ if nested_keys_each_env[other_env] != nested_keys_each_env[first_env]
82
+ success = false
83
+ compare_message += "Diff:\n"
84
+
85
+ diff_nested_keys = nested_keys_each_env[other_env] - nested_keys_each_env[first_env]
86
+ if !diff_nested_keys.empty?
87
+ compare_message += " #{other_env} - #{first_env}:\n"
88
+ diff_nested_keys.each do |diff_nested_key|
89
+ compare_message += " #{diff_nested_key.join('.')}\n"
90
+ end
91
+ end
92
+
93
+ diff_nested_keys = nested_keys_each_env[first_env] - nested_keys_each_env[other_env]
94
+ if !diff_nested_keys.empty?
95
+ compare_message += " #{first_env} - #{other_env}:\n"
96
+ diff_nested_keys.each do |diff_nested_key|
97
+ compare_message += " #{diff_nested_key.join('.')}\n"
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ {
104
+ success: success,
105
+ compare_message: compare_message,
106
+ }
107
+ end
108
+
109
+ private
110
+
111
+ def print_start_title
112
+ puts "#################################"
113
+ puts "# Yaml Structure Check #"
114
+ puts "#################################"
115
+ puts "\n"
116
+ end
117
+
118
+ def print_result_title
119
+ puts "#################################"
120
+ puts "# Yaml Structure Check Result #"
121
+ puts "#################################"
122
+ puts "\n"
123
+ end
124
+
125
+ def print_exclude_paths(exclude_paths)
126
+ puts "Exclude paths:"
127
+ if(exclude_paths.size == 0)
128
+ puts " nothing"
129
+ end
130
+
131
+ exclude_paths.each do |exclude_path|
132
+ puts " #{exclude_path}"
133
+ end
134
+ puts "\n"
135
+ end
136
+
137
+ def print_skip_paths(skip_paths)
138
+ puts "Skip paths:"
139
+ if(skip_paths.size == 0)
140
+ puts " nothing"
141
+ end
142
+
143
+ skip_paths.each do |skip_path|
144
+ puts " #{skip_path}"
145
+ end
146
+ puts "\n"
147
+ end
148
+
149
+ def print_ng_paths(ng_paths)
150
+ puts "NG paths:"
151
+ if(ng_paths.size == 0)
152
+ puts " nothing"
153
+ end
154
+
155
+ ng_paths.each do |ng_path|
156
+ puts " #{ng_path}"
157
+ end
158
+ puts "\n"
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,79 @@
1
+ module YamlStructureChecker
2
+ class Converter
3
+ # [example]
4
+ # args:
5
+ # hash = {
6
+ # 'development' => { 'x' => 1, 'y' => 2, 'z' => 3 },
7
+ # 'test' => {
8
+ # 'p' => { 'q' => { 'r' => 4 } },
9
+ # 's' => { 't' => { 'u' => 5 } },
10
+ # },
11
+ # 'production' => 6,
12
+ # }
13
+ #
14
+ # return:
15
+ # [
16
+ # ['development', 'x'],
17
+ # ['development', 'y'],
18
+ # ['development', 'z'],
19
+ # ['test', 'p', 'q', 'r'],
20
+ # ['test', 's', 't', 'u'],
21
+ # ['production']
22
+ # ]
23
+ def self.hash_to_nested_keys(
24
+ hash,
25
+ nested_keys = [],
26
+ key_ancestors = []
27
+ )
28
+ keys = hash.keys
29
+ keys.each do |key|
30
+ value = hash[key]
31
+ child_key_ancestors = nested_key = key_ancestors.clone.push(key)
32
+ if value.class == Hash
33
+ hash_to_nested_keys(value, nested_keys, child_key_ancestors)
34
+ else
35
+ nested_keys.push(nested_key)
36
+ end
37
+ end
38
+
39
+ nested_keys
40
+ end
41
+
42
+ # [example]
43
+ # args:
44
+ # nested_keys = [
45
+ # ['development', 'x'],
46
+ # ['development', 'y'],
47
+ # ['development', 'z'],
48
+ # ['test', 'p', 'q', 'r'],
49
+ # ['test', 's', 't', 'u'],
50
+ # ['production']
51
+ # ]
52
+ # envs = ['development', 'test', 'production']
53
+ #
54
+ # return:
55
+ # {
56
+ # 'development' => [['x'], ['y'], ['z']],
57
+ # 'test' => [%w[p q r], %w[s t u]],
58
+ # 'production' => [[]],
59
+ # }
60
+ def self.nested_keys_to_nested_keys_each_env(nested_keys, envs)
61
+ nested_keys_each_env = {}
62
+ envs.each do |env|
63
+ nested_keys_each_env[env] = []
64
+ end
65
+
66
+ nested_keys.each do |nested_key|
67
+ nested_key = nested_key.clone
68
+ env = nested_key.first
69
+ nested_key_without_env = nested_key.drop(1)
70
+
71
+ if envs.include?(env)
72
+ nested_keys_each_env[env].push(nested_key_without_env)
73
+ end
74
+ end
75
+
76
+ nested_keys_each_env
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,6 @@
1
+ module YamlStructureChecker
2
+ module Errors
3
+ class LoaderError < StandardError;end
4
+ class StructureError < StandardError;end
5
+ end
6
+ end
@@ -0,0 +1,79 @@
1
+ require 'yaml'
2
+
3
+ module YamlStructureChecker
4
+ class Loader
5
+ attr_accessor :include_patterns,
6
+ :exclude_patterns,
7
+ :envs,
8
+ :skip_paths
9
+
10
+ def initialize(settings_path)
11
+ hash =
12
+ begin
13
+ Loader.yaml_safe_load_file(settings_path)
14
+ rescue => e
15
+ puts "Not found YamlStructureChecker's settings file."
16
+ raise e
17
+ end
18
+
19
+ self.include_patterns = hash['include_patterns'].freeze
20
+ self.exclude_patterns = (hash['exclude_patterns'] || []).freeze
21
+ self.envs = hash['envs'].freeze
22
+ self.skip_paths = (hash['skip_paths'] || []).freeze
23
+
24
+ exist_files?(self.skip_paths)
25
+ end
26
+
27
+ def self.yaml_load_file(path)
28
+ if RUBY_VERSION > '3.1.0'
29
+ YAML.load_file(path, aliases: true)
30
+ else
31
+ YAML.load_file(path)
32
+ end
33
+ end
34
+
35
+ def self.yaml_safe_load_file(path)
36
+ if RUBY_VERSION > '3.1.0'
37
+ YAML.safe_load_file(path)
38
+ else
39
+ YAML.load_file(path)
40
+ end
41
+ end
42
+
43
+ def target_paths
44
+ @target_paths ||=
45
+ begin
46
+ include_paths = file_paths(self.include_patterns)
47
+ (include_paths - exclude_paths - self.skip_paths).sort.freeze
48
+ end
49
+ end
50
+
51
+ def exclude_paths
52
+ @exclude_paths ||= file_paths(exclude_patterns).sort.freeze
53
+ end
54
+
55
+ def total_count
56
+ target_paths.size + exclude_paths.size + skip_paths.size
57
+ end
58
+
59
+ private
60
+
61
+ def file_paths(patterns)
62
+ paths = []
63
+ patterns.each do |pattern|
64
+ paths += Dir.glob(pattern)
65
+ end
66
+ paths.uniq.sort
67
+ end
68
+
69
+ def exist_files?(paths)
70
+ paths.each do |path|
71
+ unless File.exist?(path)
72
+ raise YamlStructureChecker::Errors::LoaderError, "Not found '#{path}'"
73
+ end
74
+ end
75
+
76
+ true
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,7 @@
1
+ module YamlStructureChecker
2
+ class Runner
3
+ def self.invoke(settings_path='config/yaml_structure_checker.yml')
4
+ Checker.new.test_yamls(settings_path)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module YamlStructureChecker
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'yaml_structure_checker/checker'
4
+ require_relative 'yaml_structure_checker/converter'
5
+ require_relative 'yaml_structure_checker/errors'
6
+ require_relative 'yaml_structure_checker/loader'
7
+ require_relative 'yaml_structure_checker/runner'
8
+ require_relative 'yaml_structure_checker/version'
@@ -0,0 +1,4 @@
1
+ module YamlStructureChecker
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: yaml_structure_checker
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yosipy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-09 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - yosi.contact@gmail.com
16
+ executables:
17
+ - yaml_structure_checker
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rspec"
22
+ - CHANGELOG.md
23
+ - CODE_OF_CONDUCT.md
24
+ - Dockerfile
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE
28
+ - LICENSE.txt
29
+ - README.md
30
+ - Rakefile
31
+ - docker-compose.yml
32
+ - exe/yaml_structure_checker
33
+ - lib/yaml_structure_checker.rb
34
+ - lib/yaml_structure_checker/checker.rb
35
+ - lib/yaml_structure_checker/converter.rb
36
+ - lib/yaml_structure_checker/errors.rb
37
+ - lib/yaml_structure_checker/loader.rb
38
+ - lib/yaml_structure_checker/runner.rb
39
+ - lib/yaml_structure_checker/version.rb
40
+ - sig/yaml_structure_checker.rbs
41
+ homepage: https://github.com/yosipy/yaml_structure_checker
42
+ licenses:
43
+ - MIT
44
+ metadata:
45
+ homepage_uri: https://github.com/yosipy/yaml_structure_checker
46
+ source_code_uri: https://github.com/yosipy/yaml_structure_checker
47
+ changelog_uri: https://github.com/yosipy/yaml_structure_checker/blob/main/CHANGELOG.md
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.6.0
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ requirements: []
63
+ rubygems_version: 3.2.32
64
+ signing_key:
65
+ specification_version: 4
66
+ summary: This Gem verifies that the structure of the Yaml file is consistent for each
67
+ environment.
68
+ test_files: []