polyn-cli 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: a16936f9e691ef9d0d7564c2c4c67a00afda5bf7498d731d3f04701063f5414b
4
+ data.tar.gz: 0535b13a5c60a3a2c241fca848b599f1fdad362896d6ecae147dd7fccaedebfb
5
+ SHA512:
6
+ metadata.gz: 4f200ef0759192c9c0196b67338450e3e3bf2fbb6a7ca1df0047d7d4903986ee8a68b6a8f31999ee31bfa61ed799033ab358f9e9548f7af6884a72993a71ce1f
7
+ data.tar.gz: bf6a66f6919ddd30f033ad35a53458bffffbdb1305d58a9f718ef5303aa0fe8b589451d88928d6c8218666b9ac1e608775dd0eedb7ac251c2be0582daa6e0198
@@ -0,0 +1,37 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Ruby
9
+
10
+ on:
11
+ push:
12
+ branches: [ main ]
13
+ pull_request:
14
+ branches: [ main ]
15
+
16
+ jobs:
17
+ test:
18
+
19
+ runs-on: ubuntu-latest
20
+ strategy:
21
+ matrix:
22
+ ruby-version: ['2.6', '2.7', '3.0']
23
+
24
+ steps:
25
+ - uses: actions/checkout@v2
26
+ - name: Start NATS Jetstream
27
+ run: docker run --rm -d --network host nats:latest -js
28
+ - name: Set up Ruby
29
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
30
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
31
+ # uses: ruby/setup-ruby@v1
32
+ uses: ruby/setup-ruby@473e4d8fe5dd94ee328fdfca9f8c9c7afc9dae5e
33
+ with:
34
+ ruby-version: ${{ matrix.ruby-version }}
35
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
36
+ - name: Run tests
37
+ run: bundle exec rspec spec
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .idea/**
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,44 @@
1
+ Layout/ExtraSpacing:
2
+ ForceEqualSignAlignment: true
3
+
4
+ Layout/ArgumentAlignment:
5
+ EnforcedStyle: with_fixed_indentation
6
+
7
+ Layout/IndentationWidth:
8
+ IgnoredPatterns: []
9
+
10
+ Layout/HashAlignment:
11
+ EnforcedColonStyle: table
12
+ EnforcedHashRocketStyle: table
13
+
14
+ Layout/LineLength:
15
+ Max: 100
16
+ IgnoreCopDirectives: true
17
+
18
+ Layout/EmptyLineAfterMagicComment:
19
+ Enabled: true
20
+
21
+ Metrics/MethodLength:
22
+ Max: 15
23
+
24
+ Metrics/BlockLength:
25
+ Exclude: ["spec/**/*_spec.rb"]
26
+
27
+ Style/ModuleFunction:
28
+ Enabled: false
29
+
30
+ Style/TrailingCommaInArguments:
31
+ EnforcedStyleForMultiline: comma
32
+
33
+ Style/TrailingCommaInHashLiteral:
34
+ EnforcedStyleForMultiline: comma
35
+
36
+ Style/TrailingCommaInArrayLiteral:
37
+ EnforcedStyleForMultiline: comma
38
+
39
+ Style/FrozenStringLiteralComment:
40
+ Enabled: true
41
+ SafeAutoCorrect: true
42
+
43
+ Style/StringLiterals:
44
+ EnforcedStyle: "double_quotes"
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 3.0.4
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 3.0.1
6
+ before_install: gem install bundler -v 2.2.15
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-05-23
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 therealfugu@gmail.com. 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/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in polyn-cli.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "rspec", "~> 3.0"
10
+ gem "rubocop", "~> 1.7"
11
+ gem "json_schemer"
12
+ # EventMachine nats repo doesn't support jetstream, only nats-pure
13
+ gem "nats-pure", "~> 2.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ polyn-cli (0.1.0)
5
+ dotenv (~> 2.7.6)
6
+ json_schemer (~> 0.2)
7
+ nats-pure (~> 2.0.0)
8
+ thor (~> 1.2.0)
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ ast (2.4.2)
14
+ diff-lcs (1.5.0)
15
+ dotenv (2.7.6)
16
+ ecma-re-validator (0.4.0)
17
+ regexp_parser (~> 2.2)
18
+ hana (1.3.7)
19
+ json_schemer (0.2.21)
20
+ ecma-re-validator (~> 0.3)
21
+ hana (~> 1.3)
22
+ regexp_parser (~> 2.0)
23
+ uri_template (~> 0.7)
24
+ nats-pure (2.0.0)
25
+ parallel (1.22.1)
26
+ parser (3.1.2.0)
27
+ ast (~> 2.4.1)
28
+ rainbow (3.1.1)
29
+ rake (13.0.6)
30
+ regexp_parser (2.4.0)
31
+ rexml (3.2.5)
32
+ rspec (3.11.0)
33
+ rspec-core (~> 3.11.0)
34
+ rspec-expectations (~> 3.11.0)
35
+ rspec-mocks (~> 3.11.0)
36
+ rspec-core (3.11.0)
37
+ rspec-support (~> 3.11.0)
38
+ rspec-expectations (3.11.0)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.11.0)
41
+ rspec-mocks (3.11.1)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.11.0)
44
+ rspec-support (3.11.0)
45
+ rubocop (1.29.1)
46
+ parallel (~> 1.10)
47
+ parser (>= 3.1.0.0)
48
+ rainbow (>= 2.2.2, < 4.0)
49
+ regexp_parser (>= 1.8, < 3.0)
50
+ rexml (>= 3.2.5, < 4.0)
51
+ rubocop-ast (>= 1.17.0, < 2.0)
52
+ ruby-progressbar (~> 1.7)
53
+ unicode-display_width (>= 1.4.0, < 3.0)
54
+ rubocop-ast (1.18.0)
55
+ parser (>= 3.1.1.0)
56
+ ruby-progressbar (1.11.0)
57
+ thor (1.2.1)
58
+ unicode-display_width (2.1.0)
59
+ uri_template (0.7.0)
60
+
61
+ PLATFORMS
62
+ arm64-darwin-20
63
+ x86_64-darwin-21
64
+ x86_64-linux
65
+
66
+ DEPENDENCIES
67
+ json_schemer
68
+ nats-pure (~> 2.0)
69
+ polyn-cli!
70
+ rake (~> 13.0)
71
+ rspec (~> 3.0)
72
+ rubocop (~> 1.7)
73
+
74
+ BUNDLED WITH
75
+ 2.2.33
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Jarod
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,54 @@
1
+ # Polyn::Cli
2
+
3
+ Polyn CLI is a tool for managing and configuring a NATS server for organizations using the Polyn protocol
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ gem install 'polyn-cli'
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ### Create New Events Codebase
14
+
15
+ Run `polyn init` inside a directory to create a new `events` respository for managing your event schemas and NATS server configuration
16
+
17
+ ### Stream Generator
18
+
19
+ Run `polyn gen:stream <stream_name>` to generate a new configuration file for a stream
20
+
21
+ ### Schema Generator
22
+
23
+ Run `polyn gen:schema <event_type>` to generate a new JSON Schema for an event
24
+
25
+ ### Consumer Generator
26
+
27
+ Run `polyn gen:consumer <stream_name> <destination_name> <event_type>` to generate new configuration for a consumer of a stream. It will be included in the same file as the stream configuration.
28
+
29
+ ### Updating NATS Configuration and Schemas
30
+
31
+ Run `polyn up` to update your NATS server with the latest configuration in your `./tf` directory. It will also update your Schema Repository with the latest schemas.
32
+
33
+ ## Environment Variables
34
+
35
+ * `NATS_SERVERS` - locations of your servers (defaults to localhost)
36
+ * `POLYN_ENV` - type of environment (defaults to "development")
37
+
38
+ ## Development
39
+
40
+ 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.
41
+
42
+ 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).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/polyn-cli. 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]/polyn-cli/blob/master/CODE_OF_CONDUCT.md).
47
+
48
+ ## License
49
+
50
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
51
+
52
+ ## Code of Conduct
53
+
54
+ Everyone interacting in the Polyn::Cli project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/polyn-cli/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
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
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "polyn/cli"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/polyn ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ # Get all the `lib` files loadable
5
+ $LOAD_PATH << File.join(File.expand_path("..", __dir__), "lib")
6
+ require "polyn/cli"
7
+
8
+ Polyn::Cli.start(ARGV)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polyn
4
+ class Cli
5
+ ##
6
+ # Access cloud event information
7
+ class CloudEvent
8
+ def self.to_h
9
+ path = File.expand_path(File.join(File.dirname(__FILE__), "../cloud-event-schema.json"))
10
+ file = File.open(path)
11
+ JSON.parse(file.read)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polyn
4
+ class Cli
5
+ ##
6
+ # Configuration data for Polyn::Cli
7
+ class Configuration
8
+ attr_reader :polyn_env, :nats_servers
9
+
10
+ def initialize
11
+ @polyn_env = ENV["POLYN_ENV"] || "development"
12
+ @nats_servers = ENV["NATS_SERVERS"] || "localhost:4222"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polyn
4
+ class Cli
5
+ ##
6
+ # Generates a new NATS Consumer configuration for a stream
7
+ class ConsumerGenerator < Thor::Group
8
+ include Thor::Actions
9
+
10
+ desc "Generates a new NATS Consumer configuration for a stream"
11
+
12
+ argument :stream_name, required: true, desc: "The name of the stream to consume events from"
13
+ argument :destination_name, required: true,
14
+ desc: "The name of the application, service, or component consuming the event"
15
+ argument :event_type, required: true, desc: "The type of event being consumed"
16
+ class_option :dir, default: Dir.getwd
17
+
18
+ source_root File.join(File.expand_path(__dir__), "../templates")
19
+
20
+ def check_names
21
+ Polyn::Cli::Naming.validate_stream_name!(stream_name)
22
+ Polyn::Cli::Naming.validate_destination_name!(destination_name)
23
+ Polyn::Cli::Naming.validate_event_type!(event_type)
24
+ end
25
+
26
+ def check_stream_existance
27
+ unless File.exist?(file_path)
28
+ raise Polyn::Cli::Error,
29
+ "You must first create a stream configuration with "\
30
+ "`polyn gen:stream #{format_stream_name}`"
31
+ end
32
+ end
33
+
34
+ def check_event_type_schema
35
+ unless File.exist?(File.join(options.dir, "events", "#{event_type}.json"))
36
+ raise Polyn::Cli::Error,
37
+ "You must first create a schema with `polyn gen:schema #{event_type}`"
38
+ end
39
+ end
40
+
41
+ def format_stream_name
42
+ @stream_name = stream_name.upcase
43
+ end
44
+
45
+ def consumer_name
46
+ dest = Polyn::Cli::Naming.colon_to_underscore(destination_name)
47
+ dest = Polyn::Cli::Naming.dot_to_underscore(dest)
48
+ type = Polyn::Cli::Naming.dot_to_underscore(event_type)
49
+ "#{dest}_#{type}"
50
+ end
51
+
52
+ def file_name
53
+ @file_name ||= "tf/#{stream_name.downcase}.tf"
54
+ end
55
+
56
+ def file_path
57
+ File.join(options.dir, file_name)
58
+ end
59
+
60
+ def create
61
+ say "Creating new consumer config #{consumer_name} for stream #{stream_name}"
62
+ consumer_config = <<~TF
63
+
64
+ resource "jetstream_consumer" "#{consumer_name}" {
65
+ stream_id = jetstream_stream.#{stream_name}.id
66
+ durable_name = "#{consumer_name}"
67
+ deliver_all = true
68
+ filter_subject = "#{event_type}"
69
+ sample_freq = 100
70
+ }
71
+ TF
72
+ append_to_file(file_path, consumer_config)
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polyn
4
+ class Cli
5
+ class Naming
6
+ ##
7
+ # Convert a dot separated name into a colon separated name
8
+ def self.dot_to_colon(str)
9
+ str.gsub(".", ":")
10
+ end
11
+
12
+ def self.validate_stream_name!(name)
13
+ unless name.match(/^[a-zA-Z0-9_]+$/)
14
+ raise Polyn::Cli::Error,
15
+ "Stream name must be all alphanumeric, uppercase, and underscore separated. Got #{name}"
16
+ end
17
+ end
18
+
19
+ def self.format_stream_name(name)
20
+ name.upcase
21
+ end
22
+
23
+ def self.validate_destination_name!(name)
24
+ unless name.is_a?(String) && name.match?(/\A[a-z0-9]+(?:(?:\.|:)[a-z0-9]+)*\z/)
25
+ raise Polyn::Cli::Error,
26
+ "Event destination must be lowercase, alphanumeric and dot/colon separated, got #{name}"
27
+ end
28
+ end
29
+
30
+ def self.validate_event_type!(name)
31
+ unless name.is_a?(String) && name.match?(/\A[a-z0-9]+(?:\.[a-z0-9]+)*\z/)
32
+ raise Polyn::Cli::Error,
33
+ "Event types must be lowercase, alphanumeric and dot separated"
34
+ end
35
+ end
36
+
37
+ def self.dot_to_underscore(name)
38
+ name.gsub(".", "_")
39
+ end
40
+
41
+ def self.colon_to_underscore(name)
42
+ name.gsub(":", "_")
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polyn
4
+ class Cli
5
+ ##
6
+ # Generates a new JSON Schema file for an event
7
+ class SchemaGenerator < Thor::Group
8
+ include Thor::Actions
9
+
10
+ desc "Generates a new JSON Schema file for an event"
11
+
12
+ argument :event_type, required: true
13
+ class_option :dir, default: Dir.getwd
14
+
15
+ source_root File.join(File.expand_path(__dir__), "../templates")
16
+
17
+ def check_name
18
+ Polyn::Cli::Naming.validate_event_type!(event_type)
19
+ end
20
+
21
+ def file_name
22
+ @file_name ||= "#{event_type}.json"
23
+ end
24
+
25
+ def schema_id
26
+ Polyn::Cli::Naming.dot_to_colon(event_type)
27
+ end
28
+
29
+ def create
30
+ say "Creating new schema for #{event_type}"
31
+ template "generators/schema.json", File.join(options.dir, "events/#{event_type}.json")
32
+ end
33
+ end
34
+ end
35
+ end