Class: SRS::CLI::Cat

Inherits:
Object
  • Object
show all
Defined in:
lib/srs/cli/cat.rb

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#help ⇒ Object



29
30
31
32
33
34
35
# File 'lib/srs/cli/cat.rb', line 29

def help()
	puts <<-EOF
srs cat <id>

Outputs the content matching <id>
		EOF
end

#run!(arguments) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/srs/cli/cat.rb', line 5

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

	sha1 = arguments.shift
	sha1_start = sha1[0..1]
	sha1_rest = sha1[2..-1]

	VALID_SECTIONS.each do |section|
		datafile = "#{section}/#{sha1_start}/#{sha1_rest}"
		if File.exists?(datafile) then
			contents = File.open(datafile, "r"){ |file| file.read }
			puts contents

			return 0
		end
	end

	puts "No content with that ID exists"
	return 4
end