Class: CommitCraft::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/commitcraft/cli.rb', line 10

def self.exit_on_failure?
  true
end

Instance Method Details

#configObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/commitcraft/cli.rb', line 117

def config
  config_file = File.join(Dir.home, ".commitcraft.yml")

  if options[:show]
    show_config(config_file)  
  elsif options[:jira_prefix]
    validate_jira_key!(options[:jira_prefix])
    set_config_value('jira_prefix', options[:jira_prefix])
    say pastel.green("✓ Default Jira prefix set to: #{options[:jira_prefix]}")
  end

  require "yaml"

  current_config = File.exist?(config_file) ? YAML.load_file(config_file) : {}

  current_config["api_key"] = options[:api_key] if options[:api_key]
  current_config["model"] = options[:model] if options[:model]
  current_config["style"] = options[:style] if options[:style]

  File.write(config_file, current_config.to_yaml)

  pastel = Pastel.new
  puts pastel.green("✓ Configuration saved to #{config_file}")
end

#generateObject



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
102
103
104
105
106
107
108
# File 'lib/commitcraft/cli.rb', line 24

def generate
  setup_style(options[:style]) if options[:style]

  generator = MessageGenerator.new
  prompt = TTY::Prompt.new
  pastel = Pastel.new

    if options[:jira]
      validate_jira_key!(options[:jira])
    end

  # Show test mode indicator
  if CommitCraft.test_mode?
    puts pastel.yellow("⚠️  TEST MODE - Using mock AI (no API calls)")
    puts ""
  end

  # Show spinner while generating
  spinner = TTY::Spinner.new("[:spinner] Analyzing changes and generating commit messages...", format: :dots)
  spinner.auto_spin

  begin
    result = generator.generate(
      all: options[:all],
      unstaged: options[:unstaged],
      no_context: options[:no_context],
      include_history: options[:include_history],
      jira: options[:jira]
    )
  rescue CommitCraft::Error => e
    spinner.error("(#{pastel.red("failed")})")
    puts pastel.red("Error: #{e.message}")
    exit 1
  end

  spinner.success("(#{pastel.green("done")})")
  puts

  # Show diff summary
  summary = result[:diff_summary]
  puts pastel.dim("Files changed: #{summary[:files_changed]}, ") +
       pastel.green("+#{summary[:additions]}") + pastel.dim(" / ") +
       pastel.red("-#{summary[:deletions]}")
  puts

  # Show generated messages
  messages = result[:messages]

  if options[:auto_commit]
    selected_message = messages.first
    puts pastel.cyan("Auto-committing with: ") + pastel.white(selected_message)
  else
    choices = messages.map.with_index do |msg, _idx|
      { name: msg, value: msg }
    end
    choices << { name: pastel.dim("✎ Write custom message"), value: :custom }
    choices << { name: pastel.dim("✗ Cancel"), value: :cancel }

    selected_message = prompt.select("Choose a commit message:", choices, per_page: 10)

    case selected_message
    when :custom
      selected_message = prompt.ask("Enter your commit message:") do |q|
        q.required true
        q.validate(/\S+/)
      end
    when :cancel
      puts pastel.yellow("Commit cancelled.")
      exit 0
    end
  end

  puts

  # Confirm and commit
  begin
    generator.commit_with_message(selected_message, amend: options[:amend])
    action = options[:amend] ? "amended" : "created"
    puts pastel.green("✓ Commit #{action} successfully!")
    puts pastel.dim("  Message: #{selected_message}")
  rescue CommitCraft::GitError => e
    puts pastel.red("Failed to commit: #{e.message}")
    exit 1
  end
end

#statusObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/commitcraft/cli.rb', line 143

def status
  git_client = GitClient.new
  pastel = Pastel.new

  begin
    puts pastel.bold("Git Status:")
    puts git_client.status
    puts

    staged_files = git_client.staged_files
    if staged_files.any?
      puts pastel.bold("Staged Files:")
      staged_files.each { |file| puts pastel.green("  + #{file}") }
      puts

      stats = git_client.file_stats
      total_add = stats.sum { |s| s[:additions] }
      total_del = stats.sum { |s| s[:deletions] }
      puts pastel.dim("Total changes: ") +
           pastel.green("+#{total_add}") + pastel.dim(" / ") +
           pastel.red("-#{total_del}")
    else
      puts pastel.yellow("No staged changes.")
    end
  rescue CommitCraft::GitError => e
    puts pastel.red("Error: #{e.message}")
    exit 1
  end
end

#versionObject



174
175
176
# File 'lib/commitcraft/cli.rb', line 174

def version
  puts "CommitCraft v#{CommitCraft::VERSION}"
end