Module: Actions
- Included in:
- Pullall
- Defined in:
- lib/pullall/actions.rb
Constant Summary collapse
- STORAGE =
"#{ENV['HOME']}/.pullall"
Instance Method Summary collapse
-
#colorize(text) ⇒ Object
Output in bold red.
-
#get_real_paths(paths) ⇒ Object
Use real path to allow the use of .(current directory) as an argument.
-
#list_groups ⇒ Object
Output all the groups and associated paths.
- #load_all ⇒ Object
-
#load_paths(group) ⇒ Object
Get the paths that belong to a given group.
- #pluralize(n, text) ⇒ Object
- #pull(group) ⇒ Object
- #remove_group(group) ⇒ Object
- #remove_paths(*paths, group) ⇒ Object
- #save_all(groups) ⇒ Object
-
#save_paths(*paths, group) ⇒ Object
Only allow saving a path or a group of paths if all of them don’t belong to the group.
Instance Method Details
#colorize(text) ⇒ Object
Output in bold red
8 9 10 11 |
# File 'lib/pullall/actions.rb', line 8 def colorize(text) red_text = "\e[#{31}m#{text}\e[0m" "\e[#{1}m#{red_text}\e[0m" end |
#get_real_paths(paths) ⇒ Object
Use real path to allow the use of .(current directory) as an argument
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/pullall/actions.rb', line 18 def get_real_paths(paths) paths.collect! do |path| if File.exists?(path) File.realpath(path) else Trollop::die "The path #{path} doesn't exist" end end paths end |
#list_groups ⇒ Object
Output all the groups and associated paths
30 31 32 33 34 35 36 37 38 |
# File 'lib/pullall/actions.rb', line 30 def list_groups groups = load_all puts "\n" groups.each do |group, paths| puts "#{colorize(group)}:" puts " #{paths.join("\n ")}" puts "\n" end end |
#load_all ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/pullall/actions.rb', line 74 def load_all begin if not File.exists?(STORAGE) # Initiate file with valid json File.new(STORAGE, "w") Oj.to_file(STORAGE, Oj.dump({})) end json = Oj.load_file(STORAGE) Oj.load(json) rescue Trollop::die "~/.pullall has invalid JSON. If you changed that file then fix it so that it has valid json. If this problem persists remove the file (rm ~/.pullall)." end end |
#load_paths(group) ⇒ Object
Get the paths that belong to a given group
65 66 67 68 69 70 71 72 |
# File 'lib/pullall/actions.rb', line 65 def load_paths(group) groups = load_all if groups.has_key?(group) groups[group] else Trollop::die "Group #{group} doesn't exist" end end |
#pluralize(n, text) ⇒ Object
13 14 15 |
# File 'lib/pullall/actions.rb', line 13 def pluralize(n, text) (n == 1) ? text : "#{text}s" end |
#pull(group) ⇒ Object
119 120 121 122 123 124 125 126 |
# File 'lib/pullall/actions.rb', line 119 def pull(group) paths = load_paths(group) paths.each do |path| puts "Pulling from #{colorize(path)}:" %x((cd #{path} && git pull origin master)) puts "\n" end end |
#remove_group(group) ⇒ Object
90 91 92 93 94 95 96 |
# File 'lib/pullall/actions.rb', line 90 def remove_group(group) groups = load_all deleted = groups.delete(group) msg = deleted ? "Group successfully deleted" : "Group doesn't exist" save_all(groups) puts msg end |
#remove_paths(*paths, group) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/pullall/actions.rb', line 98 def remove_paths(*paths, group) groups = load_all if groups.has_key?(group) existent_paths = groups[group] # First check if all the paths belong to the given group paths.each do |path| if existent_paths.include?(path) existent_paths.delete(path) else Trollop::die "Given path #{path} doesn't belong to group #{group}. Changes not saved." end end save_all(groups) puts "Paths successfully removed from group #{colorize(group)}" else Trollop::die "Group #{group} doesn't exist" end end |
#save_all(groups) ⇒ Object
40 41 42 43 |
# File 'lib/pullall/actions.rb', line 40 def save_all(groups) json = Oj.dump(groups) Oj.to_file(STORAGE, json) end |
#save_paths(*paths, group) ⇒ Object
Only allow saving a path or a group of paths if all of them don’t belong to the group
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/pullall/actions.rb', line 46 def save_paths(*paths, group) groups = load_all if groups.has_key?(group) paths.each do |path| if groups[group].include?(path) Trollop::die "Path #{path} already exists in group #{group}" end end groups[group].push(*paths) else groups[group] = [*paths] end save_all(groups) msg = paths.empty? ? "Group successfully created" : "#{pluralize(paths.size, "Path")} successfully saved" puts msg end |