-
Notifications
You must be signed in to change notification settings - Fork 12
/
ftpas.rb
66 lines (56 loc) · 1.85 KB
/
ftpas.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
#!/usr/bin/ruby
# Tool to download RPG sources from AS400 via FTP
# If you don't specify any command line argument, the program will ask for server name, user, etc.
# If you pass the name of a configuration, it searches it in the ftpas.yml file in your user home,
# getting the connection parameters from there or, of they are not present, asking them from console
# You can find a sample config file in ./ftpas.sample.yaml
# Remember you can use * in member specification
# Example call:
# ruby ftpas.rb lab
require 'net/ftp'
require 'yaml'
require 'io/console'
def notSpecified?(x)
x.nil? || x.strip.empty?
end
def fromConfigOrConsole(conf, key, hidden=false)
x = conf.nil? ? nil : conf[key]
if notSpecified?(x)
puts "Enter " + key
if hidden == true
x = STDIN.noecho(&:gets).chomp
else
x = STDIN.gets.chomp
end
end
return x
end
configFileName = "#{Dir.home}/ftpas.yml"
if File.exists? (configFileName)
configs = YAML.load_file(configFileName)
else
configs = {}
end
currentConfig = configs[ARGV[0]]
server = fromConfigOrConsole(currentConfig, "server")
user = fromConfigOrConsole(currentConfig, "user")
pwd = fromConfigOrConsole(currentConfig, "password", true)
library = fromConfigOrConsole(currentConfig, "library")
file = fromConfigOrConsole(currentConfig, "file")
member = fromConfigOrConsole(currentConfig, "member")
ftp = Net::FTP::new(server)
ftp.debug_mode = true
puts "Connecting to server..."
ftp.login(user, pwd)
puts "Connected!"
ftp.chdir(library)
fileList = ftp.nlst(file + "." + member)
fileList.each do |file|
name = file.split('.')
localName = name.last
ftp.gettextfile(file, localName)
(system("iconv -f WINDOWS-1252 -t UTF-8 #{localName} > #{localName}.rpgle") and File.delete(localName)) or
puts "iconv failed for file #{localName}: #{$?}"
end
ftp.close
puts "OK"