Class: Clag::Commands::Generate

Inherits:
Clag::Command
  • Object
show all
Defined in:
lib/clag/commands/generate.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.helpObject



31
32
33
# File 'lib/clag/commands/generate.rb', line 31

def self.help
  "Generate a command-line command and store it in the clipboard. \nUsage: {{command:#{Clag::TOOL_NAME} g \"the command you want to generate\"}}"
end

Instance Method Details

#call(args, _name) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/clag/commands/generate.rb', line 7

def call(args, _name)
  input = args.join(" ")

  if ENV['OPENAI_API_KEY'].nil?
    puts CLI::UI.fmt("{{red:OPENAI_API_KEY is not set. Please set it before continuing.}}")
    return
  end

  if input.nil?
    puts "Please provide input to generate options."
    return
  end

  results = Clag::CommandLineCommandGenerator.new(description: input).generate

  if results == 'unknown'
    puts CLI::UI.fmt("{{yellow:Unable to generate command. Please try again or provide more information.}}")
    return
  end

  Clipboard.copy(results)
  puts "\e[1;32m#{results}\e[0m\nCopied to clipboard."
end