Class: Kscript::KkShellHelperUtils

Inherits:
Base
  • Object
show all
Defined in:
lib/kscript/plugins/kk_shell_helper_utils.rb

Constant Summary collapse

CHT_SH_URL =
'https://cht.sh'

Instance Attribute Summary collapse

Attributes inherited from Base

#logger

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#human_output?, inherited, #with_error_handling

Constructor Details

#initialize(*args, **opts) ⇒ KkShellHelperUtils

Initialize with shell command to look up

Parameters:

  • command (String)

    command to get help for



18
19
20
21
# File 'lib/kscript/plugins/kk_shell_helper_utils.rb', line 18

def initialize(*args, **opts)
  super
  @command = args.join(' ').strip
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



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

def command
  @command
end

Class Method Details

.argumentsObject



52
53
54
# File 'lib/kscript/plugins/kk_shell_helper_utils.rb', line 52

def self.arguments
  '[subcommand] [args...]'
end

.authorObject



64
65
66
# File 'lib/kscript/plugins/kk_shell_helper_utils.rb', line 64

def self.author
  'kk'
end

.descriptionObject



68
69
70
# File 'lib/kscript/plugins/kk_shell_helper_utils.rb', line 68

def self.description
  'Query shell command usage and cheatsheets.'
end

.groupObject



60
61
62
# File 'lib/kscript/plugins/kk_shell_helper_utils.rb', line 60

def self.group
  'system'
end

.usageObject



56
57
58
# File 'lib/kscript/plugins/kk_shell_helper_utils.rb', line 56

def self.usage
  "kscript shell_helper 'ls'\nkscript shell_helper 'echo hello'"
end

Instance Method Details

#fetch_help(command) ⇒ Object

Fetch and display command documentation



44
45
46
47
48
49
50
# File 'lib/kscript/plugins/kk_shell_helper_utils.rb', line 44

def fetch_help(command)
  response = make_request(command)
  response = response.first if response.is_a?(Array)
  logger.kinfo(response.body)
rescue StandardError => e
  display_error(e)
end

#helpObject



34
35
36
37
38
39
40
41
# File 'lib/kscript/plugins/kk_shell_helper_utils.rb', line 34

def help
  if command
    fetch_help
  else
    logger.kinfo("Usage: #{$PROGRAM_NAME} <command>")
    exit 1
  end
end

#run(*args, **_opts) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/kscript/plugins/kk_shell_helper_utils.rb', line 23

def run(*args, **_opts)
  command = args.join(' ').strip
  with_error_handling do
    if command.nil? || command.strip.empty?
      logger.kwarn("Usage: #{self.class.usage}")
    else
      fetch_help(command)
    end
  end
end