A bit more modern filestoraged client.

This commit is contained in:
Karchnu 2020-10-21 15:43:41 +02:00
parent 7bb6bf909a
commit 3491a394a9
2 changed files with 92 additions and 47 deletions

View File

@ -1,6 +1,8 @@
def authd_get_token(key_file : String? = nil, login : String? = nil, pass : String? = nil) 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 authd = AuthD::Client.new
key_file.try do |file| # FIXME: fail if missing? key_file.try do |file| # FIXME: fail if missing?
authd.key = File.read(file).chomp authd.key = File.read(file).chomp

View File

@ -5,6 +5,8 @@ require "json"
require "base64" require "base64"
require "baguette-crystal-base"
require "./authd_api.cr" require "./authd_api.cr"
require "./lib.cr" require "./lib.cr"
require "../server/network.cr" require "../server/network.cr"
@ -15,86 +17,127 @@ require "../server/storage/*"
# For now, this example only upload files. # For now, this example only upload files.
# In a near future, we should be able to download files, too. # 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" class_property service_name = "filestorage"
authd_pass : String = "test" class_property command = "unknown"
end
opt_unknown_args = ->(parser : OptionParser) {
parser.unknown_args do |arg|
Context.to_transfer = arg
end
}
OptionParser.parse do |parser| OptionParser.parse do |parser|
parser.banner = "#{PROGRAM_NAME} [OPTIONS] <files-to-upload>"
parser.on "-s service-name", parser.on "-s service-name",
"--service-name service-name", "--service-name service-name",
"Service name." do |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 end
parser.on "-l login", parser.on "-l login",
"--login login-name", "--login login-name",
"Login name for authd." do |name| "Login name for authd." do |name|
authd_login = name Context.authd_login = name
end end
parser.on "-p pass", parser.on "-p pass",
"--pass pass", "--pass pass",
"Password for authd." do |pass| "Password for authd." do |pass|
authd_pass = pass Context.authd_pass = pass
end end
parser.unknown_args do |arg| parser.on "-v verbosity",
files_and_directories_to_transfer = arg "--verbosity level",
"Verbosity. From 0 to 4." do |v|
Baguette::Context.verbosity = v.to_i
end end
parser.on "-h", "--help", "Show this help" do parser.on "-h", "--help", "Show this help" do
puts parser puts parser
puts "program [OPTIONS] <files-to-upload>"
exit -1 exit -1
end end
end end
def put(client : FileStorage::Client)
Baguette::Log.info "Putting files on the server"
# #
# Verify we can read files # Verify we can read files
# #
files = [] of String files = [] of String
puts "files and directories to transfer" Baguette::Log.debug "files and directories to transfer"
files_and_directories_to_transfer.each do |f| Context.to_transfer.each do |f|
if File.directory? f if File.directory? f
# TODO # TODO
puts "Directories not supported, for now" Baguette::Log.warning "Directories not supported, for now"
elsif File.file?(f) && File.readable? f elsif File.file?(f) && File.readable? f
files << f files << f
else else
if ! File.exists? f if ! File.exists? f
puts "#{f} does not exist" Baguette::Log.error "#{f} does not exist"
elsif ! File.file? f 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 elsif ! File.readable? f
puts "#{f} is not readable" Baguette::Log.error "#{f} is not readable"
end end
end end
end end
files.each do |file|
Baguette::Log.info "upload: #{file}"
pp! client.upload file
Baguette::Log.debug "transfer"
client.transfer file
end
end
def get(client : FileStorage::Client)
Baguette::Log.error "get command not available, yet"
end
def main
# #
# Connection to the service # Connection to the service
# #
# Authentication. token = authd_get_token login: Context.authd_login, pass: Context.authd_pass
pp! authd_login
pp! authd_pass
token = authd_get_token login: authd_login, pass: authd_pass
# Connection and authentication to filestoraged. # Connection and authentication to filestoraged.
client = FileStorage::Client.new token, service_name client = FileStorage::Client.new token, Context.service_name
client.login client.login
files.each do |file| case Context.command
puts "upload: #{file}" when /put/
pp! client.upload file put client
puts "transfer" when /get/
client.transfer file get client
else
Baguette::Log.error "unkown command"
end end
client.close client.close
end
main