Skip to content

Commit 8d22de3

Browse files
authored
Merge pull request #77 from github/kpaulisse-override-rename
Change FactOverride -> Override
2 parents 0cbf8d9 + 7ac614d commit 8d22de3

File tree

12 files changed

+450
-475
lines changed

12 files changed

+450
-475
lines changed

doc/dev/api/v1.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ The `OctocatalogDiff::API::V1::Diff` object represents a difference between two
3434

3535
[Read more about the `OctocatalogDiff::API::V1::Diff` object](/doc/dev/api/v1/objects/diff.md)
3636

37-
#### OctocatalogDiff::API::V1::FactOverride
37+
#### OctocatalogDiff::API::V1::Override
3838

39-
The `OctocatalogDiff::API::V1::FactOverride` object represents a user-supplied fact that will be used when compiling a catalog.
39+
The `OctocatalogDiff::API::V1::Override` object represents a user-supplied fact or ENC parameter that will be used when compiling a catalog.
4040

41-
[Read more about the `OctocatalogDiff::API::V1::FactOverride` object](/doc/dev/api/v1/objects/fact_override.md)
41+
[Read more about the `OctocatalogDiff::API::V1::Override` object](/doc/dev/api/v1/objects/override.md)

doc/dev/api/v1/objects/override.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# octocatalog-diff v1 API documentation: OctocatalogDiff::API::V1::Override
2+
3+
## Overview
4+
5+
`OctocatalogDiff::API::V1::Override` is an object that represents a user-supplied fact or ENC parameter that will be used when compiling a catalog.
6+
7+
## Constructor
8+
9+
#### `#new(<Hash> { key: <String>, value: <Object> })`
10+
11+
The hash must contain the following keys:
12+
13+
- `:key` (String) - The name of the fact or ENC parameter (e.g. `operatingsystem` or `parameters::fooclass::fooparam`)
14+
- `:value` (?) - The value of the fact or ENC parameter
15+
16+
See also: `#create_from_input`
17+
18+
## Methods
19+
20+
#### `#create_from_input(<String> key=value)` (OctocatalogDiff::API::V1::Override)
21+
22+
Parses the string (see [Overriding facts](/doc/advanced-override-facts.md) for the format to use). Returns a `OctocatalogDiff::API::V1::Override` object with key and value parsed from the string.
23+
24+
#### `#key` (String)
25+
26+
Returns the key as supplied in the constructor.
27+
28+
#### `#value` (?)
29+
30+
Returns the value as supplied in the constructor.

lib/octocatalog-diff/api/v1.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
require_relative 'v1/catalog-diff'
66
require_relative 'v1/config'
77
require_relative 'v1/diff'
8-
require_relative 'v1/fact_override'
8+
require_relative 'v1/override'
99

1010
module OctocatalogDiff
1111
module API

lib/octocatalog-diff/api/v1/fact_override.rb

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# frozen_string_literal: true
2+
3+
require 'json'
4+
5+
module OctocatalogDiff
6+
module API
7+
module V1
8+
# Sets up the override of a fact or ENC parameter during catalog compilation.
9+
class Override
10+
# Accessors
11+
attr_reader :key, :value
12+
13+
# Constructor: Accepts a key and value.
14+
# @param input [Hash] Must contain :key and :value
15+
def initialize(input)
16+
@key = input.fetch(:key)
17+
@value = parsed_value(input.fetch(:value))
18+
end
19+
20+
# Initialize from a parsed command line
21+
# @param input [String] Command line parameter
22+
# @return [OctocatalogDiff::API::V1::Override] Initialized object
23+
def self.create_from_input(input, key = nil)
24+
# Normally the input will be a string in the format key=(data type)value where the data
25+
# type is optional and the parentheses are literal. Example:
26+
# foo=1 (auto-determine data type - in this case it would be a fixnum)
27+
# foo=(fixnum)1 (will be a fixnum)
28+
# foo=(string)1 (will be '1' the string)
29+
# If input is not a string, we can still construct the object if the key is given.
30+
# That input would come directly from code and not from the command line, since inputs
31+
# from the command line are always strings.
32+
if key.nil? && input.is_a?(String)
33+
unless input.include?('=')
34+
raise ArgumentError, "Fact override '#{input}' is not in 'key=(data type)value' format"
35+
end
36+
k, v = input.strip.split('=', 2).map(&:strip)
37+
new(key: k, value: v)
38+
elsif key.nil?
39+
message = "Define a key when the input is not a string (#{input.class} => #{input.inspect})"
40+
raise ArgumentError, message
41+
else
42+
new(key: key, value: input)
43+
end
44+
end
45+
46+
private
47+
48+
# Guess the datatype from a particular input
49+
# @param input [String] Input in string format
50+
# @return [?] Output in appropriate format
51+
def parsed_value(input)
52+
# If data type is explicitly given
53+
if input =~ /^\((\w+)\)(.*)$/m
54+
datatype = Regexp.last_match(1)
55+
value = Regexp.last_match(2)
56+
return convert_to_data_type(datatype.downcase, value)
57+
end
58+
59+
# Guess data type
60+
return input.to_i if input =~ /^-?\d+$/
61+
return input.to_f if input =~ /^-?\d*\.\d+$/
62+
return true if input.casecmp('true').zero?
63+
return false if input.casecmp('false').zero?
64+
input
65+
end
66+
67+
# Handle data type that's explicitly given
68+
# @param datatype [String] Data type (as a string)
69+
# @param value [String] Value given
70+
# @return [?] Value converted to specified data type
71+
def convert_to_data_type(datatype, value)
72+
return value if datatype == 'string'
73+
return parse_json(value) if datatype == 'json'
74+
return nil if datatype == 'nil'
75+
if datatype == 'fixnum'
76+
return Regexp.last_match(1).to_i if value =~ /^(-?\d+)$/
77+
raise ArgumentError, "Illegal fixnum '#{value}'"
78+
end
79+
if datatype == 'float'
80+
return Regexp.last_match(1).to_f if value =~ /^(-?\d*\.\d+)$/
81+
return Regexp.last_match(1).to_f if value =~ /^(-?\d+)$/
82+
raise ArgumentError, "Illegal float '#{value}'"
83+
end
84+
if datatype == 'boolean'
85+
return true if value.casecmp('true').zero?
86+
return false if value.casecmp('false').zero?
87+
raise ArgumentError, "Illegal boolean '#{value}'"
88+
end
89+
raise ArgumentError, "Unknown data type '#{datatype}'"
90+
end
91+
92+
# Parse JSON value
93+
# @param input [String] Input, hopefully in JSON format
94+
# @return [?] Output data structure
95+
def parse_json(input)
96+
JSON.parse(input)
97+
rescue JSON::ParserError => exc
98+
raise JSON::ParserError, "Failed to parse JSON: input=#{input} error=#{exc}"
99+
end
100+
end
101+
end
102+
end
103+
end

lib/octocatalog-diff/cli.rb

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
require_relative 'api/v1'
44
require_relative 'catalog-util/cached_master_directory'
55
require_relative 'cli/diffs'
6-
require_relative 'cli/fact_override'
76
require_relative 'cli/options'
87
require_relative 'cli/printer'
98
require_relative 'errors'
@@ -82,7 +81,6 @@ def self.cli(argv = ARGV, logger = Logger.new(STDERR), opts = {})
8281
veto_with_none_options = %w(hiera_path hiera_path_strip)
8382
veto_with_none_options.each { |x| options.delete(x.to_sym) if options[x.to_sym] == :none }
8483

85-
# Fact overrides come in here - 'options' is modified
8684
setup_fact_overrides(options)
8785

8886
# Configure the logger and logger.debug initial information
@@ -129,15 +127,19 @@ def self.parse_opts(argv)
129127
Options.parse_options(argv, options)
130128
end
131129

130+
# Generic overrides
131+
def self.setup_overrides(key, options)
132+
o = options["#{key}_in".to_sym]
133+
return unless o.is_a?(Array)
134+
return unless o.any?
135+
options[key] ||= []
136+
options[key].concat o.map { |x| OctocatalogDiff::API::V1::Override.create_from_input(x) }
137+
end
138+
132139
# Fact overrides come in here
133140
def self.setup_fact_overrides(options)
134-
[:from_fact_override, :to_fact_override].each do |key|
135-
o = options["#{key}_in".to_sym]
136-
next unless o.is_a?(Array)
137-
next unless o.any?
138-
options[key] ||= []
139-
options[key].concat o.map { |x| OctocatalogDiff::Cli::FactOverride.fact_override(x) }
140-
end
141+
setup_overrides(:from_fact_override, options)
142+
setup_overrides(:to_fact_override, options)
141143
end
142144

143145
# Helper method: Configure and setup logger

lib/octocatalog-diff/cli/fact_override.rb

Lines changed: 0 additions & 93 deletions
This file was deleted.

spec/octocatalog-diff/tests/api/v1/fact_override_spec.rb

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)