Class: Kscript::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details



14
15
16
# File 'lib/kscript/cli.rb', line 14

def self.banner(command, _namespace = nil, _subcommand = false)
  "kscript #{command.usage}" if command.respond_to?(:usage)
end

.exit_on_failure?Boolean

Thor help 美化

Returns:

  • (Boolean)


145
146
147
# File 'lib/kscript/cli.rb', line 145

def self.exit_on_failure?
  true
end

Instance Method Details

#envObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/kscript/cli.rb', line 107

def env
  puts Kscript::BANNER
  puts color('─' * 80, 90)
  keys = ENV.keys.grep(/^(KSCRIPT_|AWS_|LOG$|SHELL$|HOME$)/)
  if keys.empty?
    puts gray('No relevant environment variables found.')
  else
    puts bold('Loaded environment variables:')
    keys.sort.each do |k|
      v = ENV.fetch(k, nil)
      puts green(k.ljust(28)) + gray('=') + cyan(v)
    end
  end
end

#help(*_args) ⇒ Object



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/kscript/cli.rb', line 149

def help(*_args)
  puts Kscript::BANNER
  puts color('─' * 80, 90)
  # 只展示主命令(version、help、list)
  Kscript::PluginLoader.plugin_infos.select do |info|
    info[:name].to_s.sub(/^kk_/, '')
    false # 不输出插件命令
  end
  # 只输出主命令
  puts bold('Available commands:')
  puts green('  version'.ljust(16)) + gray('Show kscript version')
  puts green('  help'.ljust(16)) + gray('Describe available commands or one specific command')
  puts green('  list'.ljust(16)) + gray('List all available commands')
  puts
  puts bold('Options:')
  puts gray('  --log-level, [--log-level=LOG_LEVEL]            # Set log level (debug, info, warn, error, fatal)')
  puts gray('               [--log], [--no-log], [--skip-log]  # Enable structured log output')
  puts gray('  --log                        # Enable  structured log output')
  puts gray('                                                  # Default: false')
  puts
  puts gray("Use 'kscript list' to see all business subcommands.")
  nil
end

#listObject



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

def list
  puts Kscript::BANNER
  puts color('─' * 80, 90)
  plugin_infos = Kscript::PluginLoader.plugin_infos
  grouped = plugin_infos.group_by { |info| info[:group] || 'other' }
  group_colors = %w[36 32 35 34 33 31 90 37]
  group_names = grouped.keys.sort
  group_names.each_with_index do |group, idx|
    color_code = group_colors[idx % group_colors.size]
    group_label = color("[#{group.capitalize}]", color_code)
    puts group_label
    puts color('─' * 80, 90)
    grouped[group].each do |info|
      command = info[:name].to_s.sub(/^kk_/, '')
      desc = info[:description] || ''
      usage = info[:class].respond_to?(:usage) ? info[:class].usage : nil
      arguments = info[:class].respond_to?(:arguments) ? info[:class].arguments : nil
      author = info[:class].respond_to?(:author) ? info[:class].author : nil
      print "  #{green(command.ljust(16))}"
      print gray(desc)
      puts
      if usage && !usage.to_s.strip.empty?
        usage.to_s.split("\n").each_with_index do |line, idx|
          prefix = idx.zero? ? gray('    usage:') : gray('          ')
          puts "#{prefix} #{cyan(line.strip)}"
        end
      end
      puts gray("    args:  #{arguments}") if arguments && !arguments.to_s.strip.empty?
      puts gray("    by:    #{author}") if author && !author.to_s.strip.empty?
      puts gray("    #{'-' * 60}")
    end
    puts
  end
end

#versionObject



20
21
22
23
24
25
# File 'lib/kscript/cli.rb', line 20

def version
  puts Kscript::BANNER
  puts color('─' * 80, 90)
  require 'kscript/version'
  puts "kscript version: #{Kscript::VERSION}"
end