Class: SRS::CLI::InsertInto

Inherits:
Object
  • Object
show all
Defined in:
lib/srs/cli/insert-into.rb

Constant Summary collapse

VALID_SECTIONS =
["data", "exercises"].freeze

Instance Method Summary collapse

Instance Method Details

#help ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/srs/cli/insert-into.rb', line 36

def help()
	puts <<-EOF
srs insert-into <section>

Reads the contents from stdin and inserts it into the appropriate section in the
workspace.  <section> can be one of "data", "exercise", or "schedule".  Returns
the id used to access that exercise.
	EOF
end

#run!(arguments) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/srs/cli/insert-into.rb', line 8

def run!(arguments)
	if not SRS::Workspace.initialised? then
		puts "Current directory is not an SRS Workspace"
		return 3
	end

	section = arguments.shift()
	if section == nil or !VALID_SECTIONS.include?(section) then
		help()
		return 4
	end

	data = STDIN.read()
	sha1 = Digest::SHA1.hexdigest data
	sha1_start = sha1[0..1]
	sha1_rest = sha1[2..-1]
	datafile = "#{section}/#{sha1_start}/#{sha1_rest}"

	if not File.exists?(datafile) then
		FileUtils::mkdir_p("#{section}/#{sha1_start}")
		File.open(datafile, 'w') {|f| f.write(data)}
	end

	puts sha1

	return 0
end