-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_config.rb
More file actions
55 lines (39 loc) · 1.05 KB
/
Copy pathfind_config.rb
File metadata and controls
55 lines (39 loc) · 1.05 KB
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require "pathname"
CONFIG_FILENAME = "adbx.config"
def find_config_file(start_dir)
dir = Pathname.new(start_dir).expand_path
loop do
candidate = dir / CONFIG_FILENAME
return candidate if candidate.file?
break if dir.root?
dir = dir.parent
end
nil
end
def parse_config(path)
config = {}
path.read.each_line do |line|
line = line.strip
next if line.empty? || line.start_with?("#")
next unless line.include?("=")
key, value = line.split("=", 2).map(&:strip)
next if key.empty?
config[key] = value.to_s
end
config
rescue StandardError
{}
end
def find_package_in_config
config_path = find_config_file(Dir.pwd)
return "" unless config_path
config = parse_config(config_path)
config.fetch("package", "")
end
# if user entered a package when invoking command then return it, otherwise search in config
def get_package(cli_package_name)
return cli_package_name unless cli_package_name.nil? || cli_package_name.empty?
return find_package_in_config
end