lib/polyn/cli/schema_loader.rb in polyn-cli-0.1.7 vs lib/polyn/cli/schema_loader.rb in polyn-cli-0.1.8

- old
+ new

@@ -16,11 +16,11 @@ new(cli).load_events end def initialize(thor, **opts) @thor = thor - @client = NATS.connect(Polyn::Cli.configuration.nats_servers).jetstream + @client = connect @store_name = opts.fetch(:store_name, STORE_NAME) @bucket = client.key_value(@store_name) @cloud_event_schema = Polyn::Cli::CloudEvent.to_h.freeze @events_dir = opts.fetch(:events_dir, File.join(Dir.pwd, "events")) @events = {} @@ -50,10 +50,24 @@ :cloud_event_schema, :events_dir, :store_name, :existing_events + def connect + opts = { + max_reconnect_attempts: 5, + reconnect_time_wait: 0.5, + servers: Polyn::Cli.configuration.nats_servers.split(","), + } + + if Polyn::Cli.configuration.nats_tls + opts.tls = { context: ::OpenSSL::SSL::SSLContext.new(:TLSv1_2) } + end + + NATS.connect(opts).jetstream + end + def read_events event_files = Dir.glob(File.join(events_dir, "/**/*.json")) validate_unique_event_types!(event_files) event_files.each do |event_file| @@ -68,19 +82,19 @@ end end def validate_unique_event_types!(event_files) duplicates = find_duplicates(event_files) - unless duplicates.empty? - messages = duplicates.reduce([]) do |memo, (event_type, files)| - memo << [event_type, *files].join("\n") - end - message = [ - "There can only be one of each event type. The following events were duplicated:", - *messages, - ].join("\n") - raise Polyn::Cli::ValidationError, message + return if duplicates.empty? + + messages = duplicates.reduce([]) do |memo, (event_type, files)| + memo << [event_type, *files].join("\n") end + message = [ + "There can only be one of each event type. The following events were duplicated:", + *messages, + ].join("\n") + raise Polyn::Cli::ValidationError, message end def find_duplicates(event_files) event_types = event_files.group_by do |event_file| File.basename(event_file, ".json")