Class: EasyAI::Command::Clean

Inherits:
Command
  • Object
show all
Defined in:
lib/easyai/command/clean.rb

Overview

清理 AI CLI 自身在本地产生的缓存与配置目录。

设计要点(详见 docs/设计文档.md §2.6):

  • 仅清理两家 AI CLI(claude / codex)的缓存路径
  • 不会触碰 ~/.easyai/config.json(重置 EasyAI 自身配置请用 easyai setup --reset)
  • --force 跳过确认;--dry-run 仅打印
  • 删除时遇到 Errno::EACCES 继续删余下项;最终如有失败项以退出码 1 退出

Constant Summary collapse

VALID_TOOLS =
%w[claude codex all].freeze
CACHE_PATHS =

AI CLI 自身的缓存路径表(不含 ~/.easyai/config.json)

{
  'claude' => %w[~/.claude ~/.claude.json ~/.config/claude],
  'codex'  => %w[~/.codex ~/.config/codex]
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Clean

Returns a new instance of Clean.



56
57
58
59
60
61
# File 'lib/easyai/command/clean.rb', line 56

def initialize(argv)
  @tool    = (argv.shift_argument || 'claude').to_s.downcase
  @force   = argv.flag?('force')
  @dry_run = argv.flag?('dry-run')
  super
end

Class Method Details

.optionsObject



49
50
51
52
53
54
# File 'lib/easyai/command/clean.rb', line 49

def self.options
  [
    ['--force',   '跳过确认提示,直接删除'],
    ['--dry-run', '仅打印将要删除的路径,不真正删除']
  ].concat(super)
end

Instance Method Details

#runObject



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
# File 'lib/easyai/command/clean.rb', line 70

def run
  targets  = collect_targets(@tool)
  existing = targets.select { |path| File.exist?(path) || File.symlink?(path) }

  if existing.empty?
    puts "未找到 #{display_label(@tool)} 的缓存路径,无需清理".cyan
    return
  end

  puts "将清理以下路径(#{display_label(@tool)}):".cyan
  existing.each { |path| puts "#{path}" }
  puts

  if @dry_run
    puts "已启用 --dry-run,未执行实际删除。".yellow
    return
  end

  unless @force
    print "确认继续删除以上路径?(y/N): "
    answer = ($stdin.gets || '').strip.downcase
    unless %w[y yes].include?(answer)
      puts "已取消".yellow
      return
    end
  end

  failures = perform_delete(existing)

  if failures.empty?
    puts "✓ 清理完成".green
  else
    puts "⚠ 清理完成,但有 #{failures.size} 项失败".yellow
    exit 1
  end
rescue Interrupt
  puts
  puts "已取消".yellow
  exit 130
end

#validate!Object



63
64
65
66
67
68
# File 'lib/easyai/command/clean.rb', line 63

def validate!
  super
  unless VALID_TOOLS.include?(@tool)
    help! "不支持的工具:#{@tool}。可选值:#{VALID_TOOLS.join(' / ')}"
  end
end