Class: LlmTranslate::CLI

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

Instance Method Summary collapse

Instance Method Details

#initObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/llm_translate/cli.rb', line 112

def init
  config_path = options[:output]

  return if File.exist?(config_path) && !yes?('Configuration file already exists. Overwrite? (y/N)')

  # Copy the sample configuration
  sample_config = File.join(__dir__, '../../content/llm_translate.yml')
  if File.exist?(sample_config)
    FileUtils.cp(sample_config, config_path)
    say "Configuration file created: #{config_path}", :green
    say 'Please edit the file to configure your API keys and preferences', :yellow
  else
    # Create a minimal config if sample doesn't exist
    create_minimal_config(config_path)
  end
end

#translateObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/llm_translate/cli.rb', line 22

def translate
  config_path = options[:config]

  unless File.exist?(config_path)
    say "Configuration file not found: #{config_path}", :red
    say 'Please create a configuration file or specify a valid path with --config', :yellow
    exit 1
  end

  begin
    config = Config.load(config_path, options)
    logger = Logger.new(config)

    logger.info 'Starting translation process...'
    if config.single_file_mode?
      logger.info "Input file: #{config.input_file}"
      logger.info "Output file: #{config.output_file}"
    else
      logger.info "Input directory: #{config.input_directory}"
      logger.info "Output directory: #{config.output_directory}"
    end

    logger.info 'DRY RUN MODE - No files will be translated' if options[:dry_run]

    # Initialize components
    ai_client = AiClient.new(config, logger)
    translator_engine = TranslatorEngine.new(config, logger, ai_client)

    # Determine files to translate
    if config.single_file_mode?
      # Single file mode
      unless File.exist?(config.input_file)
        logger.error "Input file not found: #{config.input_file}"
        return
      end
      files = [config.input_file]
      logger.info "Single file mode: translating #{config.input_file}"
    else
      # Directory mode
      file_finder = FileFinder.new(config, logger)
      files = file_finder.find_markdown_files

      if files.empty?
        logger.warn "No markdown files found in #{config.input_directory}"
        return
      end

      logger.info "Found #{files.length} markdown files to translate"
    end

    # Translate files
    if options[:dry_run]
      logger.info "DRY RUN: Would translate #{files.length} files with #{config.concurrent_files} concurrent threads"
      success_count = files.length
      error_count = 0
    else
      logger.info "Starting translation with #{config.concurrent_files} concurrent files"

      results = translator_engine.translate_files_concurrently(files)
      success_count = results[:success].length
      error_count = results[:error].length

      # Check if we should stop on too many errors
      logger.error "Stopping due to too many errors (#{error_count})" if config.should_stop_on_error?(error_count)
    end

    # Summary
    logger.info 'Translation completed!'
    logger.info "Success: #{success_count}, Errors: #{error_count}"

    generate_report(config, success_count, error_count, files) if config.generate_report?
  rescue ConfigurationError => e
    say "Configuration error: #{e.message}", :red
    exit 1
  rescue StandardError => e
    say "Unexpected error: #{e.message}", :red
    say e.backtrace.join("\n") if options[:verbose]
    exit 1
  end
end

#versionObject



104
105
106
# File 'lib/llm_translate/cli.rb', line 104

def version
  say "LlmTranslate #{LlmTranslate::VERSION}"
end