diff --git a/src/client/authd_api.cr b/src/client/authd_api.cr index 5e2cda9..8263351 100644 --- a/src/client/authd_api.cr +++ b/src/client/authd_api.cr @@ -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 diff --git a/src/client/main.cr b/src/client/main.cr index 3579923..816d711 100644 --- a/src/client/main.cr +++ b/src/client/main.cr @@ -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] " 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] " 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| - if File.directory? f - # TODO - puts "Directories not supported, for now" - elsif File.file?(f) && File.readable? f - files << f - else - if ! File.exists? f - puts "#{f} does not exist" - elsif ! File.file? f - puts "#{f} is neither a directory or a file" - elsif ! File.readable? f - puts "#{f} is not readable" + Baguette::Log.debug "files and directories to transfer" + Context.to_transfer.each do |f| + if File.directory? f + # TODO + Baguette::Log.warning "Directories not supported, for now" + elsif File.file?(f) && File.readable? f + files << f + else + if ! File.exists? f + Baguette::Log.error "#{f} does not exist" + elsif ! File.file? f + Baguette::Log.error "#{f} is neither a directory or a file" + elsif ! File.readable? f + Baguette::Log.error "#{f} is not readable" + 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 -# -# 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}" - pp! client.upload file - puts "transfer" - client.transfer file +def get(client : FileStorage::Client) + Baguette::Log.error "get command not available, yet" end -client.close + + +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