-
Notifications
You must be signed in to change notification settings - Fork 0
/
netrc.rb
executable file
·55 lines (49 loc) · 1.04 KB
/
netrc.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
#!/usr/local/bin/ruby
# = netrc.rb
#
# Author:: Dirk Meyer
# Copyright:: Copyright (c) 2018-2021 Dirk Meyer
# License:: Distributes under the same terms as Ruby
#
# == module NetRc
#
# Namespace for utility methods for easy acceess of .netrc file
#
# === Module Functions
#
# require 'netrc'
#
# NetRc.login_data( hostname )
#
# This module reads the .netrc file.
module NetRc
class << self
# Path to .netrc file
NETRC = "#{Dir.home}/.netrc".freeze
# Get login data for hostname
def login_data( hostname )
user = nil
pass = nil
found = false
File.read( NETRC ).split( "\n" ).each do |l|
token, val = l.split
case token
when 'machine'
next unless val == hostname
found = true
end
next unless found
case token
when 'login'
user = val
when 'password'
pass = val
break # first match only
end
end
warn 'Login not found.' if user.nil?
[ user, pass ]
end
end
end
# eof