Class: SRS::CLI::GetField

Inherits:
Object
  • Object
show all
Defined in:
lib/srs/cli/get-field.rb

Instance Method Summary collapse

Instance Method Details

#help ⇒ Object



46
47
48
49
50
51
52
# File 'lib/srs/cli/get-field.rb', line 46

def help()
	puts <<-EOF
srs get-field <field-name> <content-id>

Returns the value of the field <field-name> from content <content-id>
		EOF
end

#run!(arguments) ⇒ Object



4
5
6
7
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
35
36
37
38
39
40
41
42
43
44
# File 'lib/srs/cli/get-field.rb', line 4

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

	field = arguments.shift
	id = arguments.shift

	is_schedule = (id =~ /\d{14}\.\d{3}/)

	filename = ""
	if is_schedule then
		filename = "schedule/#{id}"
		filename = "schedule/pending/#{id}" if not File.exists?(filename)
	else
		filename = "exercises/#{id}"
	end

	if not File.exists?(filename) then
		puts "No content with that ID exists"
		return 4
	end

	File.open(filename, "r") do |file|
		while( line = file.gets() ) do
			if line.strip.empty? then
				break
			end

			key, *val = line.split(':').map{|e| e.strip}
			if key.casecmp(field) == 0 then
				puts val.join(':')
				return 0
			end
		end
	end

	puts "Content #{id} does not contain field \"#{field}\"."
	return 4
end