Sha256: 8eb62275a4b4b7a23653496f1b46a42229c494fb079836bbe513cf922e403dff

Contents?: true

Size: 1.92 KB

Versions: 10

Compression:

Stored size: 1.92 KB

Contents

# description: Run a client.
require "optparse"
require "json"
require "discorb/utils/colored_puts"
require "io/console"

ARGV.delete_at 0
# @!visibility private
LOG_LEVELS = %w[none debug info warn error fatal]

opt = OptionParser.new <<~BANNER
                         This command will run a client.

                         Usage: discorb run [options] [script]

                                   script                     The script to run. Defaults to 'main.rb'.
                       BANNER
options = {
  deamon: false,
  log_level: nil,
  log_file: nil,
  log_color: nil,
  setup: nil,
  token: false,
}
opt.on("-d", "--deamon", "Run as a daemon.") { |v| options[:daemon] = v }
opt.on("-l", "--log-level LEVEL", "Log level.") do |v|
  unless LOG_LEVELS.include? v.downcase
    eputs "Invalid log level: \e[31m#{v}\e[91m"
    eputs "Valid log levels: \e[31m#{LOG_LEVELS.join("\e[91m, \e[31m")}\e[91m"
    exit 1
  end
  options[:log_level] = v.downcase
end
opt.on("-f", "--log-file FILE", "File to write log to.") { |v| options[:log_file] = v }
opt.on("-c", "--[no-]log-color", "Whether to colorize log output.") { |v| options[:log_color] = v }
opt.on("-s", "--setup", "Whether to setup application commands.") { |v| options[:setup] = v }
opt.on("-t", "--token [ENV]", "The name of the environment variable to use for token, or just `-t` or `--token` for intractive prompt.") { |v| options[:token] = v }
opt.parse!(ARGV)

script = ARGV[0]

script ||= "main.rb"

ENV["DISCORB_CLI_FLAG"] = "run"
ENV["DISCORB_CLI_OPTIONS"] = JSON.generate(options)

if options[:token]
  ENV["DISCORB_CLI_TOKEN"] = ENV[options[:token]]
  raise "#{options[:token]} is not set." if ENV["DISCORB_CLI_TOKEN"].nil?
elsif options[:token].nil? || options[:token] == "-"
  print "\e[90mPlease enter your token: \e[m"
  ENV["DISCORB_CLI_TOKEN"] = $stdin.noecho(&:gets).chomp
  puts ""
end

begin
  load script
rescue LoadError
  eputs "Could not load script: \e[31m#{script}\e[91m"
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
discorb-0.8.2 lib/discorb/exe/run.rb
discorb-0.8.1 lib/discorb/exe/run.rb
discorb-0.8.0 lib/discorb/exe/run.rb
discorb-0.7.6 lib/discorb/exe/run.rb
discorb-0.7.3 lib/discorb/exe/run.rb
discorb-0.7.2 lib/discorb/exe/run.rb
discorb-0.7.1 lib/discorb/exe/run.rb
discorb-0.7.0 lib/discorb/exe/run.rb
discorb-0.6.1 lib/discorb/exe/run.rb
discorb-0.6.0 lib/discorb/exe/run.rb