Module: Commands

Defined in:
lib/oliver/commands.rb

Class Method Summary collapse

Class Method Details

.add(args) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/oliver/commands.rb', line 62

def add(args)
	if !Helpers.oliver_exists?
		puts Helpers.log('.oliver does not exist', :error)
	else
		content = Helpers.file

		args.each do |arg|
			profile = arg.split('/')
			username = profile[0]
			repo = profile[1]

			begin
				content[username].push(repo)
			rescue
				content[username] = []
				content[username].push(repo)
			end

			File.open('.oliver', 'w') do |f|
				f.write(JSON.pretty_generate(content))
				puts Helpers.log("#{repo} added", :success)
			end
		end

	end
end

.helpObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/oliver/commands.rb', line 4

def help
	commands = {
		init: 'initializes the main directory by creating a base dotfile',
		update: 'clones/removes repos if they\'re listed',
		list: 'list user repos',
		pull: 'pull updates from each tracked repo',
		version: 'return the current version',
		help: 'return this help menu',
	}
	text = <<-EOS
Usage:\n
\t#{'oliver'.colorize(:blue)} <command> [<args>]
Commands:\n
	EOS
	commands.map { |key, value| text += ("\t#{key}\t#{value}\n") }
	puts text
end

.init(args) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/oliver/commands.rb', line 22

def init(args)
	template = {}
	unless args.empty?
		args.each do |pair|
			# syntax: oliver init trmml=oliver,site,etc
			buffer = pair.split('=')
			username = buffer[0]
			repos = buffer[1].split(',')
			template[username] = repos
		end
	end
	if Helpers.oliver_exists?
		puts Helpers.log('.oliver exists', :warning)
	else
		File.open('.oliver', 'w') do |f|
			f.write(JSON.pretty_generate(template))
			puts Helpers.log('.oliver created.', :success)
		end
	end
end

.list(args) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/oliver/commands.rb', line 117

def list(args)
	# check if listed and local (good)
	# star if listed but not local
	# x if local but not listed
	Helpers.log('.oliver does not exist', :error) unless Helpers.oliver_exists?
	Helpers.log('.oliver is empty', :error) if Helpers.file.empty?

	Helpers.file.map do |user, repos|
		repos.each do |repo|
			# repo is definitely listed at this point
			if Helpers.local_repos.include? repo
				emblem = ''.colorize(:green)
			elsif !Helpers.local_repos.include? repo
				emblem = '*'.colorize(:blue)
			end

			puts "#{emblem} #{user}/#{repo}"
		end
	end

	Helpers.local_repos.each do |repo|
		# check if local repos are listed
		# check remotes for fetch and push URL to add user in the future
		if !Helpers.tracked_repos.include? repo
			emblem = ''.colorize(:red)
			puts "#{emblem} #{repo}"
		end
	end
end

.pull(args) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/oliver/commands.rb', line 43

def pull(args)
	# array with all folders in directory
	dirs = Dir.glob('*').select { |f| File.directory? f }
	dirs.each do |dir|
		begin
			g = Git.open(dir)
			g.pull
			puts Helpers.log("Pulled #{dir} successfully")
		rescue
			puts Helpers.log("Failed to pull #{dir}", :error)
		end
	end
end

.remove(args) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/oliver/commands.rb', line 89

def remove(args)
	if !Helpers.oliver_exists?
		puts Helpers.log('.oliver dose not exist', :error)
	else
		content = Helpers.file

		args.each do |arg|
			profile = arg.split('/')
			username = profile[0]
			repo = profile[1]

			if content[username].include?(repo)
				# good
				index = content[username].index(repo)
				content[username].delete_at(index)

				File.open('.oliver', 'w') do |f|
					f.write(JSON.pretty_generate(content))
					puts Helpers.log("#{repo} removed", :success)
				end
			else
				puts Helpers.log("#{repo} is not listed")
			end
		end

	end
end

.update(args) ⇒ Object



57
58
59
60
# File 'lib/oliver/commands.rb', line 57

def update(args)
	Helpers.remove
	Helpers.install
end