forked from puppetlabs/puppet-strings
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Add Hiera defaults support for module documentation #27
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
Merged
bastelfreak
merged 10 commits into
voxpupuli:main
from
slauger:feature/hiera-defaults-support
Feb 19, 2026
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7ef7a3e
Add Hiera defaults support for module documentation
slauger cc5f17a
Fix empty hash/array formatting to match code defaults
slauger a417868
Fix array formatting to match code defaults output
slauger 81dbab3
Add tests for array formatting without spaces
slauger 7877167
Fix RuboCop violations
slauger 878eff2
fix: update lib/openvox-strings/markdown/base.rb
slauger 86e6eae
Improve Hiera hierarchy detection and fix indentation
slauger d99d0d0
Refactor load_common_data into smaller testable methods
slauger 61a1d32
Fix RuboCop violations for Hiera interpolation detection
slauger e215273
Fix paths array handling and simplify load_hiera_config
slauger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'yaml' | ||
|
|
||
| module OpenvoxStrings | ||
| # Parser for Hiera configuration and data | ||
| class Hiera | ||
| attr_reader :hiera_config, :common_data | ||
|
|
||
| # Initializes a Hiera parser for a given module path | ||
| # @param [String] module_path The path to the Puppet module root directory | ||
| def initialize(module_path) | ||
| @module_path = module_path | ||
| @hiera_config = load_hiera_config | ||
| @common_data = load_common_data | ||
| end | ||
|
|
||
| # Checks if Hiera is configured for this module | ||
| # @return [Boolean] true if hiera.yaml exists and is valid | ||
| def hiera_enabled? | ||
| !@hiera_config.nil? | ||
| end | ||
|
|
||
| # Gets the default value for a parameter from Hiera data | ||
| # @param [String] class_name The fully qualified class name (e.g., 'github_actions_runner') | ||
| # @param [String] param_name The parameter name | ||
| # @return [String, nil] The default value as a string, or nil if not found | ||
| def lookup_default(class_name, param_name) | ||
| return nil unless hiera_enabled? | ||
| return nil if @common_data.nil? | ||
|
|
||
| # Try to lookup with class prefix: modulename::parametername | ||
| key = "#{class_name}::#{param_name}" | ||
| return nil unless @common_data.key?(key) | ||
|
|
||
| value = @common_data[key] | ||
|
|
||
| # Convert value to Puppet-compatible string representation | ||
| value_to_puppet_string(value) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Loads and parses hiera.yaml from the module root | ||
| # @return [Hash, nil] The parsed hiera configuration, or nil if not found/invalid | ||
| def load_hiera_config | ||
| hiera_file = File.join(@module_path, 'hiera.yaml') | ||
| return nil unless File.exist?(hiera_file) | ||
|
|
||
| begin | ||
| YAML.load_file(hiera_file) | ||
| rescue StandardError => e | ||
| YARD::Logger.instance.warn "Failed to parse hiera.yaml: #{e.message}" | ||
| nil | ||
| end | ||
| end | ||
|
|
||
| # Loads and parses the first static hierarchy layer (without interpolations) | ||
slauger marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # @return [Hash, nil] The parsed data, or nil if not found/invalid | ||
| def load_common_data | ||
| return nil unless hiera_enabled? | ||
|
|
||
| # Get datadir from hiera config (defaults to 'data') | ||
| datadir = @hiera_config.dig('defaults', 'datadir') || 'data' | ||
|
|
||
| # Find first hierarchy entry without interpolations | ||
| hierarchy = @hiera_config['hierarchy'] | ||
| return nil unless hierarchy | ||
|
|
||
| first_static = hierarchy.find do |entry| | ||
| path = entry['path'] || entry['paths']&.first | ||
| next false unless path | ||
|
|
||
| # Check if path contains interpolations like %{...} | ||
| !path.match?(/%\{[^}]+\}/) | ||
| end | ||
|
|
||
| return nil unless first_static | ||
|
|
||
| # Get the path from the hierarchy entry | ||
| data_file_path = first_static['path'] || first_static['paths']&.first | ||
| return nil unless data_file_path | ||
|
|
||
| # Build full path | ||
| data_file = File.join(@module_path, datadir, data_file_path) | ||
| return nil unless File.exist?(data_file) | ||
|
|
||
| begin | ||
| YAML.load_file(data_file) | ||
| rescue StandardError => e | ||
| YARD::Logger.instance.warn "Failed to parse #{data_file_path}: #{e.message}" | ||
| nil | ||
| end | ||
| end | ||
|
|
||
| # Converts a Ruby value to a Puppet-compatible string representation | ||
| # @param [Object] value The value to convert | ||
| # @return [String] The Puppet-compatible string representation | ||
| def value_to_puppet_string(value) | ||
| case value | ||
| when String | ||
| # Empty strings from YAML nil (~) should be undef | ||
| return 'undef' if value.empty? | ||
|
|
||
| # Strings should be quoted | ||
| "'#{value}'" | ||
| when Integer, Float, TrueClass, FalseClass | ||
| # Numbers and booleans are unquoted/lowercase | ||
| value.to_s | ||
| when NilClass, :undef | ||
| # Puppet undef | ||
| 'undef' | ||
| when Hash | ||
| # Convert hash to Puppet hash syntax (no spaces to match code defaults format) | ||
| return '{}' if value.empty? | ||
|
|
||
| pairs = value.map { |k, v| "'#{k}' => #{value_to_puppet_string(v)}" } | ||
| "{ #{pairs.join(', ')} }" | ||
| when Array | ||
| # Convert array to Puppet array syntax (no spaces to match code defaults format) | ||
| return '[]' if value.empty? | ||
|
|
||
| elements = value.map { |v| value_to_puppet_string(v) } | ||
| "[#{elements.join(', ')}]" | ||
| else | ||
| # Fallback: convert to string and quote | ||
| "'#{value}'" | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.