A bit more modern filestoraged client.
This commit is contained in:
parent
7bb6bf909a
commit
3491a394a9
@ -1,6 +1,8 @@
|
||||
|
||||
def authd_get_token(key_file : String? = nil, login : String? = nil, pass : String? = nil)
|
||||
|
||||
# return AuthD::Token.new("test", 1000).to_s if CLI.simulation
|
||||
|
||||
authd = AuthD::Client.new
|
||||
key_file.try do |file| # FIXME: fail if missing?
|
||||
authd.key = File.read(file).chomp
|
||||
|
@ -5,6 +5,8 @@ require "json"
|
||||
|
||||
require "base64"
|
||||
|
||||
require "baguette-crystal-base"
|
||||
|
||||
require "./authd_api.cr"
|
||||
require "./lib.cr"
|
||||
require "../server/network.cr"
|
||||
@ -15,86 +17,127 @@ require "../server/storage/*"
|
||||
# For now, this example only upload files.
|
||||
# In a near future, we should be able to download files, too.
|
||||
|
||||
service_name = "filestorage"
|
||||
class Context
|
||||
class_property authd_login : String = "test"
|
||||
class_property authd_pass : String = "test"
|
||||
|
||||
files_and_directories_to_transfer = Array(String).new
|
||||
class_property to_transfer = Array(String).new
|
||||
|
||||
authd_login : String = "test"
|
||||
authd_pass : String = "test"
|
||||
class_property service_name = "filestorage"
|
||||
class_property command = "unknown"
|
||||
end
|
||||
|
||||
opt_unknown_args = ->(parser : OptionParser) {
|
||||
parser.unknown_args do |arg|
|
||||
Context.to_transfer = arg
|
||||
end
|
||||
}
|
||||
|
||||
OptionParser.parse do |parser|
|
||||
parser.banner = "#{PROGRAM_NAME} [OPTIONS] <files-to-upload>"
|
||||
parser.on "-s service-name",
|
||||
"--service-name service-name",
|
||||
"Service name." do |name|
|
||||
service_name = name
|
||||
Context.service_name = name
|
||||
end
|
||||
|
||||
parser.on "get", "Get files from IDs (file digests)." do
|
||||
parser.banner = "#{PROGRAM_NAME} get digest [digest…] [OPTIONS]"
|
||||
Context.command = "get"
|
||||
opt_unknown_args.call parser
|
||||
end
|
||||
parser.on "put", "Send files." do
|
||||
parser.banner = "#{PROGRAM_NAME} put path [path…] [OPTIONS]"
|
||||
Context.command = "put"
|
||||
opt_unknown_args.call parser
|
||||
end
|
||||
|
||||
parser.on "-l login",
|
||||
"--login login-name",
|
||||
"Login name for authd." do |name|
|
||||
authd_login = name
|
||||
Context.authd_login = name
|
||||
end
|
||||
|
||||
parser.on "-p pass",
|
||||
"--pass pass",
|
||||
"Password for authd." do |pass|
|
||||
authd_pass = pass
|
||||
Context.authd_pass = pass
|
||||
end
|
||||
|
||||
parser.unknown_args do |arg|
|
||||
files_and_directories_to_transfer = arg
|
||||
parser.on "-v verbosity",
|
||||
"--verbosity level",
|
||||
"Verbosity. From 0 to 4." do |v|
|
||||
Baguette::Context.verbosity = v.to_i
|
||||
end
|
||||
|
||||
parser.on "-h", "--help", "Show this help" do
|
||||
puts parser
|
||||
puts "program [OPTIONS] <files-to-upload>"
|
||||
exit -1
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
#
|
||||
# Verify we can read files
|
||||
#
|
||||
def put(client : FileStorage::Client)
|
||||
Baguette::Log.info "Putting files on the server"
|
||||
#
|
||||
# Verify we can read files
|
||||
#
|
||||
|
||||
files = [] of String
|
||||
files = [] of String
|
||||
|
||||
puts "files and directories to transfer"
|
||||
files_and_directories_to_transfer.each do |f|
|
||||
Baguette::Log.debug "files and directories to transfer"
|
||||
Context.to_transfer.each do |f|
|
||||
if File.directory? f
|
||||
# TODO
|
||||
puts "Directories not supported, for now"
|
||||
Baguette::Log.warning "Directories not supported, for now"
|
||||
elsif File.file?(f) && File.readable? f
|
||||
files << f
|
||||
else
|
||||
if ! File.exists? f
|
||||
puts "#{f} does not exist"
|
||||
Baguette::Log.error "#{f} does not exist"
|
||||
elsif ! File.file? f
|
||||
puts "#{f} is neither a directory or a file"
|
||||
Baguette::Log.error "#{f} is neither a directory or a file"
|
||||
elsif ! File.readable? f
|
||||
puts "#{f} is not readable"
|
||||
Baguette::Log.error "#{f} is not readable"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
#
|
||||
# Connection to the service
|
||||
#
|
||||
|
||||
# Authentication.
|
||||
pp! authd_login
|
||||
pp! authd_pass
|
||||
token = authd_get_token login: authd_login, pass: authd_pass
|
||||
|
||||
# Connection and authentication to filestoraged.
|
||||
client = FileStorage::Client.new token, service_name
|
||||
client.login
|
||||
|
||||
files.each do |file|
|
||||
puts "upload: #{file}"
|
||||
files.each do |file|
|
||||
Baguette::Log.info "upload: #{file}"
|
||||
pp! client.upload file
|
||||
puts "transfer"
|
||||
Baguette::Log.debug "transfer"
|
||||
client.transfer file
|
||||
end
|
||||
end
|
||||
|
||||
client.close
|
||||
def get(client : FileStorage::Client)
|
||||
Baguette::Log.error "get command not available, yet"
|
||||
end
|
||||
|
||||
|
||||
|
||||
def main
|
||||
#
|
||||
# Connection to the service
|
||||
#
|
||||
|
||||
token = authd_get_token login: Context.authd_login, pass: Context.authd_pass
|
||||
|
||||
# Connection and authentication to filestoraged.
|
||||
client = FileStorage::Client.new token, Context.service_name
|
||||
client.login
|
||||
|
||||
case Context.command
|
||||
when /put/
|
||||
put client
|
||||
when /get/
|
||||
get client
|
||||
else
|
||||
Baguette::Log.error "unkown command"
|
||||
end
|
||||
|
||||
client.close
|
||||
end
|
||||
|
||||
main
|
||||
|
Loading…
Reference in New Issue
Block a user