Class: Workbush::CLI

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

Overview

Command-line interface for Workbush

Instance Method Summary collapse

Instance Method Details

#add(path, branch = nil) ⇒ Object



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
# File 'lib/workbush/cli.rb', line 38

def add(path, branch = nil)
  config = load_config

  # Create worktree
  worktree = Worktree.new(path: path, branch: branch)
  say "Creating worktree at #{path}...", :cyan
  worktree.create(force: options[:force])
  say "✓ Worktree created successfully", :green

  # Copy files if enabled
  if options[:copy] && has_copy_config?(config)
    copy_files(worktree, config)
  elsif options[:copy]
    say "No copy configuration found, skipping file copy", :yellow if options[:verbose]
  end

  # Run commands if enabled
  if options[:run_commands] && has_commands?(config)
    run_commands(worktree, config)
  elsif options[:run_commands]
    say "No post-creation commands configured", :yellow if options[:verbose]
  end

  say
  say "Worktree ready at: #{worktree.path}", :green
  say "To switch to it: cd #{path}", :cyan
rescue GitError => e
  say "Git error: #{e.message}", :red
  exit 1
rescue WorktreeError => e
  say "Worktree error: #{e.message}", :red
  exit 1
rescue StandardError => e
  say "Error: #{e.message}", :red
  say e.backtrace.join("\n"), :red if options[:verbose]
  exit 1
end

#initObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/workbush/cli.rb', line 91

def init
  config_path = File.join(Dir.pwd, Config::DEFAULT_CONFIG_NAME)

  if File.exist?(config_path) && !options[:force]
    say "Configuration file already exists: #{config_path}", :yellow
    return unless yes?("Overwrite?")
  end

  File.write(config_path, Config.default_template)
  say "Created configuration file: #{config_path}", :green
  say
  say "Edit this file to customize your worktree setup:", :cyan
  say "  #{config_path}"
rescue StandardError => e
  say "Error creating config: #{e.message}", :red
  exit 1
end

#listObject



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/workbush/cli.rb', line 118

def list
  worktrees = Worktree.list

  if worktrees.empty?
    say "No worktrees found", :yellow
    return
  end

  say "Worktrees:", :cyan
  worktrees.each do |wt|
    branch_info = wt[:branch] || wt[:head]
    status = []
    status << "bare" if wt[:bare]
    status << "detached" if wt[:detached]
    status << "locked" if wt[:locked]

    status_str = status.empty? ? "" : " (#{status.join(", ")})"
    say "  #{wt[:path]} [#{branch_info}]#{status_str}"
  end
rescue GitError => e
  say "Git error: #{e.message}", :red
  exit 1
end

#remove(path) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/workbush/cli.rb', line 154

def remove(path)
  expanded_path = File.expand_path(path)

  unless options[:force]
    return unless yes?("Remove worktree at #{expanded_path}?")
  end

  Worktree.remove(expanded_path, force: options[:force])
  say "✓ Worktree removed: #{expanded_path}", :green
rescue WorktreeError => e
  say "Error: #{e.message}", :red
  exit 1
end

#versionObject



169
170
171
# File 'lib/workbush/cli.rb', line 169

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