Class: Schoolkeep::CLI

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

Instance Method Summary collapse

Instance Method Details

#download_fixturesObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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_fixturesObject



97
98
99
100
101
102
103
104
105
106
107
108
# 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

#guiObject



130
131
132
133
# File 'lib/schoolkeep/cli.rb', line 130

def gui
  require "schoolkeep/gui_client"
  Schoolkeep::GuiClient.new
end

#reset(template_path = nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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

#serverObject



83
84
85
86
87
88
89
90
91
92
93
# 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) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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