forked from jish/pre-commit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyaml.rb
67 lines (54 loc) · 1.47 KB
/
yaml.rb
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
64
65
66
67
require 'yaml'
module PreCommit
class Configuration
class Providers
class Yaml
def self.priority
20
end
def [](name)
config[name]
end
def update(name, value)
content = read_config(local_file)
content[name] = value
save_config(local_file, content)
end
private
def config
return @config if @config
@config = {}
@config.merge!(read_config(system_file))
@config.merge!(read_config(global_file))
@config.merge!(read_config(local_file))
@config
end
def read_config(path)
content = YAML.load_file(path) if File.exist?(path)
content || {}
end
def save_config(path, content)
parent = File.expand_path('..', path)
Dir.mkdir(parent) unless Dir.exist?(parent)
File.open(path, "w") do |file|
file.write(YAML.dump(content))
end
end
def system_file
@system_file ||= '/etc/pre_commit.yml'
end
def global_file
@global_file ||= File.join(ENV['HOME'], '.pre_commit.yml')
end
def local_file
File.join(top_level, 'config', 'pre_commit.yml')
end
def top_level
top_level = `git rev-parse --show-toplevel`.chomp.strip
raise "no git repo!" if top_level == ""
top_level
end
end
end
end
end