Class: Dotcodegen::CLI
- Inherits:
-
Object
- Object
- Dotcodegen::CLI
- Defined in:
- lib/dotcodegen/cli.rb
Class Method Summary collapse
-
.build_option_parser(options) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
.handle_arguments(args, opt_parser, options) ⇒ Object
rubocop:disable Metrics/AbcSize.
- .load_matchers(instructions_path) ⇒ Object
- .parse(args) ⇒ Object
- .run(args) ⇒ Object
-
.validate_file_path(args, opt_parser) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize.
- .version ⇒ Object
Class Method Details
.build_option_parser(options) ⇒ Object
rubocop:disable Metrics/MethodLength
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/dotcodegen/cli.rb', line 44 def self.build_option_parser() OptionParser.new do |opts| opts. = 'Usage: dotcodegen [options] file_path' opts.on('--openai_key KEY', 'OpenAI API Key') { |key| .openai_key = key } opts.on('--openai_org_id Org_Id', 'OpenAI Organisation Id') { |key| .openai_org_id = key } opts.on('--init', 'Initialize a .codegen configuration in the current directory') { .init = true } opts.on('--version', 'Show version') { .version = true } opts.on_tail('-h', '--help', 'Show this message') do puts opts exit end end end |
.handle_arguments(args, opt_parser, options) ⇒ Object
rubocop:disable Metrics/AbcSize
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/dotcodegen/cli.rb', line 59 def self.handle_arguments(args, opt_parser, ) opt_parser.parse!(args) return if .version || .init validate_file_path(args, opt_parser) .file_path = args.shift .openai_key ||= ENV['OPENAI_KEY'] .openai_org_id ||= ENV['OPENAI_ORG_ID'] return unless .openai_key.to_s.strip.empty? puts 'Error: Missing --openai_key flag or OPENAI_KEY environment variable.' puts opt_parser exit 1 end |
.load_matchers(instructions_path) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/dotcodegen/cli.rb', line 36 def self.load_matchers(instructions_path) Dir.glob("#{instructions_path}/*.md").map do |file| parsed = FrontMatterParser::Parser.parse_file(file) parsed.front_matter.merge({ content: parsed.content }) end end |
.parse(args) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/dotcodegen/cli.rb', line 14 def self.parse(args) = OpenStruct.new opt_parser = build_option_parser() handle_arguments(args, opt_parser, ) end |
.run(args) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/dotcodegen/cli.rb', line 21 def self.run(args) Dotenv.load unless defined?($running_tests) && $running_tests = parse(args) return version if .version return Init.run if .init matchers = load_matchers('.codegen/instructions') TestFileGenerator.new(file_path: .file_path, matchers:, openai_key: .openai_key, openai_org_id: .openai_org_id).run end |
.validate_file_path(args, opt_parser) ⇒ Object
rubocop:enable Metrics/MethodLength rubocop:enable Metrics/AbcSize
78 79 80 81 82 83 84 |
# File 'lib/dotcodegen/cli.rb', line 78 def self.validate_file_path(args, opt_parser) return unless args.empty? puts 'Error: Missing file path.' puts opt_parser exit 1 end |
.version ⇒ Object
31 32 33 34 |
# File 'lib/dotcodegen/cli.rb', line 31 def self.version puts "Dotcodegen version #{Dotcodegen::VERSION}" exit end |