Class: Jeeves::CLI

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

Constant Summary collapse

CONFIG_DIR =
File.expand_path('~/.config/jeeves')
PROMPT_FILE =
File.join(CONFIG_DIR, 'prompt')

Instance Method Summary collapse

Constructor Details

#initializeCLI



14
15
16
17
18
19
20
21
# File 'lib/jeeves.rb', line 14

def initialize
  @options = {
    all: false,
    push: false,
    dry_run: false
  }
  setup_config_dir
end

Instance Method Details

#parse_optionsObject



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
# File 'lib/jeeves.rb', line 23

def parse_options
  require 'optparse'
  
  OptionParser.new do |opts|
    opts.banner = 'Usage: jeeves [options]'
    opts.version = VERSION

    opts.on('-a', '--all', 'Stage all changes before committing') do
      @options[:all] = true
    end

    opts.on('-p', '--push', 'Push changes after committing') do
      @options[:push] = true
    end

  opts.on('-d', '--dry-run', 'Generate commit message without committing') do
    @options[:dry_run] = true
  end

  opts.on('-h', '--help', 'Show this help message') do
    puts opts
    exit
  end
  end.parse!
end

#runObject



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
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/jeeves.rb', line 49

def run
  parse_options
  
  # Automatically detect stdin input
  has_stdin_input = !STDIN.tty? || STDIN.closed?
  
  # Handle stdin mode when auto-detected
  if has_stdin_input
    # Read diff from stdin
    diff = STDIN.read
    
    if diff.empty?
      puts "Error: No diff content provided via stdin."
      exit 1
    end
    
    # Generate commit message and output just the message
    commit_message = generate_commit_message(diff, suppress_output: true)
    puts commit_message
    return
  end
  
  # Normal mode: work with git staged changes
  if @options[:all]
    system('git add -A')
  end

  # Get git diff of staged changes
  diff = `git diff --staged`
  
  if diff.empty?
    puts "No changes staged for commit."
    exit 1
  end

  # Get AI-generated commit message
  commit_message = generate_commit_message(diff)
  
  # If dry-run mode, just output the message and exit
  if @options[:dry_run]
    puts "\nDry-run mode: Commit message would be:"
    puts "============================================"
    puts commit_message
    puts "============================================"
    puts "No commit was created (dry-run mode)"
    return
  end
  
  # Write commit message to temp file for git to use
  temp_file = File.join(Dir.tmpdir, 'jeeves_commit_message')
  File.write(temp_file, commit_message)
  
  # Commit with the generated message
  system("git commit -F #{temp_file}")
  
  # Clean up temp file
  File.unlink(temp_file) if File.exist?(temp_file)
  
  # Push if requested
  if @options[:push]
    puts "Pushing changes..."
    system('git push')
  end
end