class Schoolkeep::CLI

Public Instance Methods

download_fixtures() click to toggle source
# File lib/schoolkeep/cli.rb, line 112
def download_fixtures
  unless ENV["SK_API_KEY"]
    abort set_color("Set the env variable SK_API_KEY in order to download the fixtures", :red)
  end

  client = Client.new(ENV["SK_API_KEY"])

  response = client.get_fixtures
  if response.success?
    FileUtils.mkdir_p(File.join(options[:dir], "config"))
    File.write(File.join(options[:dir], "config/fixtures.yml"), response.body)
    true
  else
    abort set_color("download failed: Server responded with #{response.status} #{response.body}", :red)
  end
end
generate_fixtures() click to toggle source
# File lib/schoolkeep/cli.rb, line 97
def generate_fixtures
  require "schoolkeep/fixture"
  FileUtils.mkdir_p(File.join(options[:dir], "config"))
  fixture_path = File.join(options[:dir], "/config/fixtures.yml")
  if File.exists?(fixture_path)
    unless yes? set_color("#{fixture_path} already exists, are you sure you want to overwrite? [y/n]", :yellow)
      abort set_color("aborting fixture generation", :red)
    end
  end
  FileUtils.copy_file(Fixture::GEM_FIXTURE_PATH, fixture_path)
  say "#{fixture_path} has been generated", :green
end
gui() click to toggle source
# File lib/schoolkeep/cli.rb, line 130
def gui
  require "schoolkeep/gui_client"
  Schoolkeep::GuiClient.new
end
reset(template_path = nil) click to toggle source
# File lib/schoolkeep/cli.rb, line 54
def reset(template_path = nil)
  unless ENV["SK_API_KEY"]
    abort set_color("Set the env variable SK_API_KEY in order to upload files", :red)
  end

  client = Client.new(ENV["SK_API_KEY"])

  if options[:all]
    template_names = TEMPLATE_NAMES
  else
    template_names = [File.basename(template_path, ".sktl")]
  end

  invalid_templates = template_names - TEMPLATE_NAMES
  if invalid_templates.size > 0
    abort set_color("invalid template names: #{invalid_templates.join(", ")}", :yellow)
  end

  template_names.each do |name|
    client.delete(name)
    say("reset #{name}", :green) unless options[:quiet]
  end
end
server() click to toggle source
# File lib/schoolkeep/cli.rb, line 83
def server
  require "schoolkeep/server"
  server = Server.new(
    dir: options[:dir],
    port: options[:port],
    quiet: options[:quiet],
    asset_host: options[:asset_host]
  )
  trap "INT" do server.shutdown end
  server.start
end
upload(path = nil) click to toggle source
# File lib/schoolkeep/cli.rb, line 11
def upload(path = nil)
  unless ENV["SK_API_KEY"]
    abort set_color("Set the env variable SK_API_KEY in order to upload files", :red)
  end
  unless %w(sktl liquid).include?(options[:engine])
    abort set_color("Invalid engine flag. Allowed values: sktl, liquid", :red)
  end

  client = Client.new(ENV["SK_API_KEY"])
  engine = options[:engine]

  if options[:all] || options[:dir]
    templates = Dir.glob(File.join(options[:dir] || "templates", "*.#{engine}"))
  elsif path
    templates = Dir.glob(path)
  else
    abort set_color("missing template path as argument", :red)
  end

  if templates.count == 0
    abort set_color("No sktl templates found, no files will be uploaded", :yellow)
  end

  template_names = templates.map { |t| File.basename(t, ".#{engine}") }
  invalid_templates = template_names - TEMPLATE_NAMES
  if invalid_templates.size > 0
    abort set_color("invalid templates: #{invalid_templates.join(", ")}", :yellow)
  end

  templates.each do |template_path|
    name = File.basename(template_path, ".#{engine}")
    response = client.upload(name, template_path, engine)
    if response.success?
      say("uploaded #{name}", :green) unless options[:quiet]
    else
      abort set_color("upload failed: Server responded with #{response.status} #{response.body}", :red)
    end
  end
end