-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalsmgr.cr
63 lines (58 loc) · 1.52 KB
/
alsmgr.cr
1
2
3
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
def load_aliases_from(alias_file_location) : Hash(String, String)
hash = Hash(String, String).new
if !File.exists?(alias_file_location)
return hash
end
File.each_line(alias_file_location) do |line|
values = line.split("=")
first = values[0]
name = first.split(" ")[1]
path = values[1]
hash[name] = path
end
hash
end
def get_output_for(aliases : Hash(String, String), prefix = false)
a = Array(String).new
aliases.each do |name, path|
line = "#{name}=#{path}"
if prefix
line = "alias #{line}"
end
a << line
end
output = a.sort!.join("\n")
end
def update_aliases(hash, alias_file_location)
if File.exists?(alias_file_location) && !File.empty?(alias_file_location)
File.copy(alias_file_location, "#{alias_file_location}.bak")
end
File.write(alias_file_location, get_output_for(hash, true))
end
alias_file_location = Path["~/.alias"].expand(home: true)
hash = load_aliases_from(alias_file_location)
new_name = ARGV[0]?
command = ARGV[1]?
path = ""
if new_name
if command == "-d"
puts "Deleting alias [#{new_name}]"
hash.delete new_name
elsif command == "-c"
path = hash[new_name]
else
puts "Adding alias [#{new_name}]"
current_dir = Dir.current
puts "Path of [#{current_dir}]"
hash[new_name] = "'#{current_dir}'"
end
update_aliases(hash, alias_file_location)
end
puts "---------------"
puts "Current aliases"
puts "---------------"
puts get_output_for(hash)
if path != ""
puts "---------------"
puts "Path for [#{new_name}] is [#{path}]"
end