Class: Dotcodegen::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/dotcodegen/cli.rb

Class Method Summary collapse

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(options)
  OptionParser.new do |opts|
    opts.banner = 'Usage: dotcodegen [options] file_path'
    opts.on('--openai_key KEY', 'OpenAI API Key') { |key| options.openai_key = key }
    opts.on('--openai_org_id Org_Id', 'OpenAI Organisation Id') { |key| options.openai_org_id = key }
    opts.on('--init', 'Initialize a .codegen configuration in the current directory') { options.init = true }
    opts.on('--version', 'Show version') { options.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, options)
  opt_parser.parse!(args)
  return if options.version || options.init

  validate_file_path(args, opt_parser)
  options.file_path = args.shift
  options.openai_key ||= ENV['OPENAI_KEY']
  options.openai_org_id ||= ENV['OPENAI_ORG_ID']

  return unless options.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)
  options = OpenStruct.new
  opt_parser = build_option_parser(options)
  handle_arguments(args, opt_parser, options)
  options
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
  options = parse(args)
  return version if options.version
  return Init.run if options.init

  matchers = load_matchers('.codegen/instructions')
  TestFileGenerator.new(file_path: options.file_path, matchers:, openai_key: options.openai_key, openai_org_id: options.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

.versionObject



31
32
33
34
# File 'lib/dotcodegen/cli.rb', line 31

def self.version
  puts "Dotcodegen version #{Dotcodegen::VERSION}"
  exit
end