Class: Morpheus::Cli::ArchivesCommand

Inherits:
Object
  • Object
show all
Includes:
CliCommand, ProvisioningHelper
Defined in:
lib/morpheus/cli/archives_command.rb

Instance Attribute Summary

Attributes included from CliCommand

#no_prompt

Instance Method Summary collapse

Methods included from ProvisioningHelper

#accounts_interface, #add_perms_options, #api_client, #apps_interface, #cloud_datastores_interface, #clouds_interface, #datastores_interface, #find_app_by_id, #find_app_by_name, #find_app_by_name_or_id, #find_cloud_by_id_for_provisioning, #find_cloud_by_name_for_provisioning, #find_cloud_by_name_or_id_for_provisioning, #find_cloud_resource_pool_by_name_or_id, #find_group_by_id_for_provisioning, #find_group_by_name_for_provisioning, #find_group_by_name_or_id_for_provisioning, #find_host_by_id, #find_host_by_name, #find_host_by_name_or_id, #find_instance_by_id, #find_instance_by_name, #find_instance_by_name_or_id, #find_instance_type_by_code, #find_instance_type_by_id, #find_instance_type_by_name, #find_instance_type_by_name_or_id, #find_instance_type_layout_by_id, #find_server_by_id, #find_server_by_name, #find_server_by_name_or_id, #find_workflow_by_id, #find_workflow_by_name, #find_workflow_by_name_or_id, #format_app_status, #format_blueprint_type, #format_container_connection_string, #format_container_status, #format_instance_connection_string, #format_instance_container_display_name, #format_instance_status, #format_metadata, #format_snapshot_status, #get_available_accounts, #get_available_clouds, #get_available_environments, #get_available_groups, #get_available_plans, #get_provision_type_for_zone_type, #get_static_environments, included, #instance_type_layouts_interface, #instance_types_interface, #instances_interface, #load_balance_protocols_dropdown, #options_interface, #parse_blueprint_type, #parse_host_id_list, #parse_instance_id_list, #parse_metadata, #parse_resource_id_list, #parse_server_id_list, #print_permissions, #prompt_evars, #prompt_exposed_ports, #prompt_instance_load_balancer, #prompt_metadata, #prompt_network_interfaces, #prompt_new_instance, #prompt_permissions, #prompt_resize_volumes, #prompt_security_groups, #prompt_volumes, #provision_types_interface, #reject_service_plan_option_types, #servers_interface

Methods included from CliCommand

#apply_options, #build_common_options, #build_option_type_options, #build_standard_add_options, #build_standard_delete_options, #build_standard_get_options, #build_standard_list_options, #build_standard_post_options, #build_standard_put_options, #build_standard_remove_options, #build_standard_update_options, #command_description, #command_name, #default_refresh_interval, #default_sigdig, #default_subcommand, #establish_remote_appliance_connection, #full_command_usage, #get_subcommand_description, #handle_subcommand, included, #interactive?, #my_help_command, #my_terminal, #my_terminal=, #parse_bytes_param, #parse_id_list, #parse_list_options, #parse_list_subtitles, #parse_passed_options, #parse_payload, #parse_query_options, #print, #print_error, #println, #prog_name, #puts, #puts_error, #raise_args_error, #raise_command_error, #render_response, #run_command_for_each_arg, #subcommand_aliases, #subcommand_description, #subcommand_usage, #subcommands, #usage, #validate_outfile, #verify_args!, #visible_subcommands

Constructor Details

#initializeArchivesCommand

set_default_subcommand :list



45
46
47
# File 'lib/morpheus/cli/archives_command.rb', line 45

def initialize()
  # @appliance_name, @appliance_url = Morpheus::Cli::Remote.active_appliance
end

Instance Method Details

#add_bucket(args) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/morpheus/cli/archives_command.rb', line 244

def add_bucket(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[options]")
    opts.on('--name VALUE', String, "Name") do |val|
      options['name'] = val
    end
    opts.on('--description VALUE', String, "Description") do |val|
      options['description'] = val
    end
    opts.on('--storageProvider VALUE', String, "Storage Provider ID") do |val|
      options['storageProvider'] = val.to_s
    end
    opts.on('--visibility [private|public]', String, "Visibility determines if read access is restricted to the specified Tenants (Private) or all tenants (Public).") do |val|
      options['visibility'] = val.to_s
    end
    opts.on('--accounts LIST', String, "Tenant Accounts (comma separated ids)") do |val|
      # uh don't put commas or leading/trailing spaces in script names pl
      options['accounts'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
    end
    opts.on('--isPublic [on|off]', String, "Enabling Public URL allows files to be downloaded without any authentication.") do |val|
      options['isPublic'] = (val.to_s == 'on' || val.to_s == 'true')
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :quiet, :remote])
    opts.footer = "Create a new archive bucket."
  end
  optparse.parse!(args)
  connect(options)
  begin
    options.merge!(options[:options]) if options[:options] # so -O var= works..
    option_params = options.reject {|k,v| k.is_a?(Symbol) }
    # use the -g GROUP or active group by default
    # options['group'] ||=  @active_group_id
    
    # support first arg as name instead of --name
    if args[0] && !options['name']
      options['name'] = args[0]
    end
    payload = nil
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'archiveBucket' => option_params}) if !option_params.empty?
    else
      archive_bucket_payload = prompt_new_archive_bucket(options)
      return 1 if !archive_bucket_payload
      payload = {'archiveBucket' => archive_bucket_payload}
    end
    @archive_buckets_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_buckets_interface.dry.create(payload)
      return
    end
    json_response = @archive_buckets_interface.create(payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    elsif !options[:quiet]
      new_archive_bucket = json_response['archiveBucket']
      print_green_success "Added archive bucket #{new_archive_bucket['name']}"
      get_bucket([new_archive_bucket['id']])
      # list([])
    end

  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end


1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
# File 'lib/morpheus/cli/archives_command.rb', line 1402

def add_file_link(args)
  options = {}
  expiration_seconds = 20*60 # default expiration is 20 minutes
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path]")
    opts.on('-e', '--expire SECONDS', "The time to live for this link. The default is 1200 (20 minutes). A value less than 1 means never expire.") do |val|
      expiration_seconds = val.to_i
    end
    build_common_options(opts, options, [:json, :dry_run, :quiet, :remote])
    opts.footer = "Create a public link to a file.\n" + 
                  "[bucket:/path] is required. This is the name of the bucket and /path the file or folder to be fetched."
  end
  optparse.parse!(args)
  # consider only allowing args.count == 1 here in the format [bucket:/path]
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} add-file-link expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  bucket_id, file_path  = parse_bucket_id_and_file_path(args[0])
  connect(options)
  begin
    archive_file = find_archive_file_by_bucket_and_path(bucket_id, file_path)
    return 1 if archive_file.nil?

    params = {}
    if expiration_seconds.to_i > 0
      params['expireSeconds'] = expiration_seconds.to_i
    end
    @archive_files_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_files_interface.dry.create_file_link(archive_file['id'], params)
      return
    end
    json_response = @archive_files_interface.create_file_link(archive_file['id'], params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      return 0
    elsif !options[:quiet]
      print_green_success "Created archive file link #{bucket_id}:/#{archive_file['filePath']} token: #{json_response['secretAccessKey']}"
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#connect(opts) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/morpheus/cli/archives_command.rb', line 49

def connect(opts)
  @api_client = establish_remote_appliance_connection(opts)
  @archive_buckets_interface = @api_client.archive_buckets
  @archive_files_interface = @api_client.archive_files
  @options_interface = @api_client.options
  # @active_group_id = Morpheus::Cli::Groups.active_groups[@appliance_name]
end

#download_bucket_zip(args) ⇒ Object



1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
# File 'lib/morpheus/cli/archives_command.rb', line 1616

def download_bucket_zip(args)
  full_command_string = "#{command_name} download-bucket #{args.join(' ')}".strip
  options = {}
  outfile = nil
  do_overwrite = false
  do_mkdir = false
  use_public_url = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket] [local-file]")
    opts.on( '-f', '--force', "Overwrite existing [local-file] if it exists." ) do
      do_overwrite = true
      # do_mkdir = true
    end
    opts.on( '-p', '--mkdir', "Create missing directories for [local-file] if they do not exist." ) do
      do_mkdir = true
    end
    # api endpoint needed still for public bucket.zip
    # opts.on( '-p', '--public', "Use Public Download URL instead of Private. The bucket must be have Public URL enabled." ) do
    #   use_public_url = true
    #   # do_mkdir = true
    # end
    build_common_options(opts, options, [:dry_run, :quiet, :remote])
    opts.footer = "Download an entire archive bucket as a .zip file.\n" + 
                  "[bucket] is required. This is the name of the bucket.\n" +
                  "[local-file] is required. This is the full local filepath for the downloaded file.\n" +
                  "Buckets are be downloaded as a .zip file, so you'll want to specify a [local-file] with a .zip extension."
  end
  optparse.parse!(args)
  if args.count != 2
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} download-bucket expects 2 arguments and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    bucket_id = args[0].to_s
    archive_bucket = find_archive_bucket_by_name_or_id(bucket_id)
    return 1 if archive_bucket.nil?
    
    outfile = args[1]
    # if outfile[-1] == "/" || outfile[-1] == "\\"
    #   outfile = File.join(outfile, archive_bucket['name'].to_s) + ".zip"
    # end
    outfile = File.expand_path(outfile)
    if Dir.exists?(outfile)
      outfile = File.join(outfile, archive_bucket['name'].to_s) + ".zip"
    end
    if Dir.exists?(outfile)
      print_red_alert "[local-file] is invalid. It is the name of an existing directory: #{outfile}"
      return 1
    end
    # always a .zip
    if outfile[-4..-1] != ".zip"
      outfile << ".zip"
    end
    destination_dir = File.dirname(outfile)
    if !Dir.exists?(destination_dir)
      if do_mkdir
        print cyan,"Creating local directory #{destination_dir}",reset,"\n"
        FileUtils.mkdir_p(destination_dir)
      else
        print_red_alert "[local-file] is invalid. Directory not found: #{destination_dir}"
        return 1
      end
    end
    if File.exists?(outfile)
      if do_overwrite
        # uhh need to be careful wih the passed filepath here..
        # don't delete, just overwrite.
        # File.delete(outfile)
      else
        print_error Morpheus::Terminal.angry_prompt
        puts_error "[local-file] is invalid. File already exists: #{outfile}", "Use -f to overwrite the existing file."
        # puts_error optparse
        return 1
      end
    end
    @archive_buckets_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_buckets_interface.dry.download_bucket_zip_chunked(bucket_id, outfile), full_command_string
      return 1
    end
    if !options[:quiet]
      print cyan + "Downloading archive bucket #{bucket_id} to #{outfile} ... "
    end

    http_response = @archive_buckets_interface.download_bucket_zip_chunked(bucket_id, outfile)

    # FileUtils.chmod(0600, outfile)
    success = http_response.code.to_i == 200
    if success
      if !options[:quiet]
        print green + "SUCCESS" + reset + "\n"
      end
      return 0
    else
      if !options[:quiet]
        print red + "ERROR" + reset + " HTTP #{http_response.code}" + "\n"
      end
      # F it, just remove a bad result
      if File.exists?(outfile) && File.file?(outfile)
        Morpheus::Logging::DarkPrinter.puts "Deleting bad file download: #{outfile}" if Morpheus::Logging.debug?
        File.delete(outfile)
      end
      if options[:debug]
        puts_error http_response.inspect
      end
      return 1
    end
    
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#download_file(args) ⇒ Object



1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
# File 'lib/morpheus/cli/archives_command.rb', line 1219

def download_file(args)
  full_command_string = "#{command_name} download #{args.join(' ')}".strip
  options = {}
  outfile = nil
  do_overwrite = false
  do_mkdir = false
  use_public_url = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path] [local-file]")
    opts.on( '-f', '--force', "Overwrite existing [local-file] if it exists." ) do
      do_overwrite = true
      # do_mkdir = true
    end
    opts.on( '-p', '--mkdir', "Create missing directories for [local-file] if they do not exist." ) do
      do_mkdir = true
    end
    opts.on( '-p', '--public', "Use Public Download URL instead of Private. The file must be in a public archives." ) do
      use_public_url = true
      # do_mkdir = true
    end
    build_common_options(opts, options, [:dry_run, :quiet, :remote])
    opts.footer = "Download an archive file or directory.\n" + 
                  "[bucket:/path] is required. This is the name of the bucket and /path the file or folder to be downloaded.\n" +
                  "[local-file] is required. This is the full local filepath for the downloaded file.\n" +
                  "Directories will be downloaded as a .zip file, so you'll want to specify a [local-file] with a .zip extension."
  end
  optparse.parse!(args)
  if args.count != 2
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} download expects 2 arguments and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    bucket_id, file_path  = parse_bucket_id_and_file_path(args[0])
    # just make 1 api call for now
    # archive_file = find_archive_file_by_bucket_and_path(bucket_id, file_path)
    # return 1 if archive_file.nil?
    full_file_path = "#{bucket_id}/#{file_path}".squeeze('/')
    # full_file_path = args[0]
    # end download destination with a slash to use the local file basename
    outfile = args[1]
    # if outfile[-1] == "/" || outfile[-1] == "\\"
    #   outfile = File.join(outfile, File.basename(full_file_path))
    # end
    outfile = File.expand_path(outfile)
    if Dir.exists?(outfile)
      outfile = File.join(outfile, File.basename(full_file_path))
    end
    if Dir.exists?(outfile)
      print_red_alert "[local-file] is invalid. It is the name of an existing directory: #{outfile}"
      return 1
    end
    destination_dir = File.dirname(outfile)
    if !Dir.exists?(destination_dir)
      if do_mkdir
        print cyan,"Creating local directory #{destination_dir}",reset,"\n"
        FileUtils.mkdir_p(destination_dir)
      else
        print_red_alert "[local-file] is invalid. Directory not found: #{destination_dir}"
        return 1
      end
    end
    if File.exists?(outfile)
      if do_overwrite
        # uhh need to be careful wih the passed filepath here..
        # don't delete, just overwrite.
        # File.delete(outfile)
      else
        print_error Morpheus::Terminal.angry_prompt
        puts_error "[local-file] is invalid. File already exists: #{outfile}", "Use -f to overwrite the existing file."
        # puts_error optparse
        return 1
      end
    end
    begin
      @archive_files_interface.setopts(options)
      if options[:dry_run]
        # print_dry_run @archive_files_interface.dry.download_file_by_path(full_file_path), full_command_string
        if use_public_url
          print_dry_run @archive_files_interface.dry.download_public_file_by_path_chunked(full_file_path, outfile), full_command_string
        else
          print_dry_run @archive_files_interface.dry.download_file_by_path_chunked(full_file_path, outfile), full_command_string
        end
        return 0
      end
      if !options[:quiet]
        print cyan + "Downloading archive file #{bucket_id}:#{file_path} to #{outfile} ... "
      end
      # file_response = @archive_files_interface.download_file_by_path(full_file_path)
      # File.write(outfile, file_response.body)
      # err, maybe write to a random tmp file, then mv to outfile
      # currently, whatever the response is, it's written to the outfile. eg. 404 html
      http_response = nil
      if use_public_url
        http_response = @archive_files_interface.download_public_file_by_path_chunked(full_file_path, outfile)
      else
        http_response = @archive_files_interface.download_file_by_path_chunked(full_file_path, outfile)
      end

      # FileUtils.chmod(0600, outfile)
      success = http_response.code.to_i == 200
      if success
        if !options[:quiet]
          print green + "SUCCESS" + reset + "\n"
        end
        return 0
      else
        if !options[:quiet]
          print red + "ERROR" + reset + " HTTP #{http_response.code}" + "\n"
        end
        # F it, just remove a bad result
        if File.exists?(outfile) && File.file?(outfile)
          Morpheus::Logging::DarkPrinter.puts "Deleting bad file download: #{outfile}" if Morpheus::Logging.debug?
          File.delete(outfile)
        end
        if options[:debug]
          puts_error http_response.inspect
        end
        return 1
      end
    rescue RestClient::Exception => e
      # this is not reached
      if e.response && e.response.code == 404
        print_red_alert "Archive file not found by path #{full_file_path}"
        return nil
      else
        raise e
      end
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
  
end


1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
# File 'lib/morpheus/cli/archives_command.rb', line 1505

def download_file_link(args)
  full_command_string = "archives download-link #{args.join(' ')}".strip
  options = {}
  outfile = nil
  do_overwrite = false
  dor_mkdir = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[link-key] [local-file]")
    opts.on( '-f', '--force', "Overwrite existing [local-file] if it exists." ) do
      do_overwrite = true
      # do_mkdir = true
    end
    opts.on( '-p', '--mkdir', "Create missing directories for [local-file] if they do not exist." ) do
      do_mkdir = true
    end
    build_common_options(opts, options, [:dry_run, :quiet, :remote])
    opts.footer = "Download an archive file link.\n" + 
                  "[link-key] is required. This is the secret access key for the archive file link.\n" +
                  "[local-file] is required. This is the full local filepath for the downloaded file."
  end
  optparse.parse!(args)
  if args.count != 2
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} download-link expects 2 arguments and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    link_key = args[0]
    # archive_file_link = find_archive_file_link_by_key(link_key)
    # just make 1 api call for now
    # archive_file = find_archive_file_by_bucket_and_path(bucket_id, file_path)
    # return 1 if archive_file.nil?
    full_file_path = "#{bucket_id}/#{file_path}".squeeze('/')
    # full_file_path = args[0]
    outfile = File.expand_path(args[1])
    # [local-file] must include the full file name when downloading a link
    # if Dir.exists?(outfile)
    #   outfile = File.join(outfile, File.basename(archive_file['name']))
    # end
    if Dir.exists?(outfile)
      print_red_alert "[local-file] is invalid. It is the name of an existing directory: #{outfile}"
      return 1
    end
    destination_dir = File.dirname(outfile)
    if !Dir.exists?(destination_dir)
      if do_mkdir
        print cyan,"Creating local directory #{destination_dir}",reset,"\n"
        FileUtils.mkdir_p(destination_dir)
      else
        print_red_alert "[local-file] is invalid. Directory not found: #{destination_dir}"
        return 1
      end
    end
    if File.exists?(outfile)
      if do_overwrite
        # uhh need to be careful wih the passed filepath here..
        # don't delete, just overwrite.
        # File.delete(outfile)
      else
        print_red_alert "[local-file] is invalid. File already exists: #{outfile}"
        # print_error Morpheus::Terminal.angry_prompt
        # puts_error  "[local-file] is invalid. File already exists: #{outfile}\n#{optparse}"
        puts_error "Use -f to overwrite the existing file."
        # puts_error optparse
        return 1
      end
    end
    @archive_files_interface.setopts(options)
    if options[:dry_run]
      # print_dry_run @archive_files_interface.dry.download_file_by_path(full_file_path), full_command_string
      print_dry_run @archive_files_interface.dry.download_file_by_link_chunked(link_key, outfile), full_command_string
      return 1
    end
    if !options[:quiet]
      print cyan + "Downloading archive file link #{link_key} to #{outfile} ... "
    end
    # file_response = @archive_files_interface.download_file_by_path(full_file_path)
    # File.write(outfile, file_response.body)
    # err, maybe write to a random tmp file, then mv to outfile
    # currently, whatever the response is, it's written to the outfile. eg. 404 html
    http_response = @archive_files_interface.download_file_by_link_chunked(link_key, outfile)

    # FileUtils.chmod(0600, outfile)
    success = http_response.code.to_i == 200
    if success
      if !options[:quiet]
        print green + "SUCCESS" + reset + "\n"
      end
      return 0
    else
      if !options[:quiet]
        print red + "ERROR" + reset + " HTTP #{http_response.code}" + "\n"
      end
      # F it, just remove a bad result
      if File.exists?(outfile) && File.file?(outfile)
        Morpheus::Logging::DarkPrinter.puts "Deleting bad file download: #{outfile}" if Morpheus::Logging.debug?
        File.delete(outfile)
      end
      if options[:debug]
        puts_error http_response.inspect
      end
      return 1
    end
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
  
end

#file_history(args) ⇒ Object



1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
# File 'lib/morpheus/cli/archives_command.rb', line 1092

def file_history(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path]")
    build_common_options(opts, options, [:list, :json, :dry_run, :remote])
    opts.footer = "List history log events for an archive file."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} file-history expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  bucket_id, file_path  = parse_bucket_id_and_file_path(args[0])
  connect(options)
  begin
    # todo: only 1 api call needed here.
    # archive_bucket = find_archive_bucket_by_name_or_id(bucket_id)
    # return 1 if archive_bucket.nil?
    archive_file = find_archive_file_by_bucket_and_path(bucket_id, file_path)
    return 1 if archive_file.nil?
    # ok, load history
    params = {}
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    @archive_files_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_files_interface.dry.history(archive_file['id'], params)
      return
    end
    json_response = @archive_files_interface.history(archive_file['id'], params)
    archive_logs = json_response['archiveLogs']

    if options[:json]
      print JSON.pretty_generate(json_response)
      return
    end

    print_h1 "Archive File History", ["#{bucket_id}:#{file_path}"]
    # print cyan
    # description_cols = {
    #   "File ID" => 'id',
    #   "Bucket" => lambda {|it| bucket_id },
    #   "File Path" => lambda {|it| file_path }
    # }
    # print_description_list(description_cols, archive_file)
    # print "\n"
    # print_h2 "History"
    if archive_logs && archive_logs.size > 0
      print_archive_logs_table(archive_logs)
      print_results_pagination(json_response, {:label => "history record", :n_label => "history records"})
    else
      puts "No history found"
    end
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end


1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/morpheus/cli/archives_command.rb', line 1155

def file_links(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path]")
    build_common_options(opts, options, [:list, :json, :dry_run, :remote])
    opts.footer = "List links for an archive file."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} file-history expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  bucket_id, file_path  = parse_bucket_id_and_file_path(args[0])
  connect(options)
  begin
    # todo: only 1 api call needed here.
    # archive_bucket = find_archive_bucket_by_name_or_id(bucket_id)
    # return 1 if archive_bucket.nil?
    archive_file = find_archive_file_by_bucket_and_path(bucket_id, file_path)
    return 1 if archive_file.nil?
    # ok, load links
    params = {}
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    @archive_files_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_files_interface.dry.list_links(archive_file['id'], params)
      return
    end
    json_response = @archive_files_interface.list_links(archive_file['id'], params)
    archive_file_links = json_response['archiveFileLinks']

    if options[:json]
      print JSON.pretty_generate(json_response)
      return
    end

    print_h1 "Archive File Links", ["#{bucket_id}:#{file_path}"]
    # print_h1 "Archive File"
    # print cyan
    # description_cols = {
    #   "File ID" => 'id',
    #   "Bucket" => lambda {|it| bucket_id },
    #   "File Path" => lambda {|it| file_path }
    # }
    # print_description_list(description_cols, archive_file)
    # print "\n"
    # print_h2 "Links"
    if archive_file_links && archive_file_links.size > 0
      print_archive_file_links_table(archive_file_links)
      print_results_pagination(json_response, {:label => "link", :n_label => "links"})
    else
      puts "No file links found"
    end
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#get_bucket(args) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/morpheus/cli/archives_command.rb', line 150

def get_bucket(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path]")
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Display archive bucket details and files. " +
                  "\nThe [bucket] component of the argument is the name or id of an archive bucket." +
                  "\nThe [:/path] component is optional and can be used to display files under a sub-directory."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} get expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  bucket_id, search_file_path  = parse_bucket_id_and_file_path(args[0])
  connect(options)
  begin
    @archive_buckets_interface.setopts(options)
    if options[:dry_run]
      if args[0].to_s =~ /\A\d{1,}\Z/
        print_dry_run @archive_buckets_interface.dry.get(bucket_id.to_i)
      else
        print_dry_run @archive_buckets_interface.dry.list({name:bucket_id})
      end
      return
    end
    archive_bucket = find_archive_bucket_by_name_or_id(bucket_id)
    return 1 if archive_bucket.nil?
    json_response = {'archiveBucket' => archive_bucket}  # skip redundant request
    # json_response = @archive_buckets_interface.get(archive_bucket['id'])
    archive_bucket = json_response['archiveBucket']
    if options[:json]
      print JSON.pretty_generate(json_response)
      return
    end
    subtitles = []
    if search_file_path != "/"
      subtitles << "Path: #{search_file_path}"
    end
    print_h1 "Archive Bucket Details", subtitles
    print cyan

    description_cols = {
      "ID" => 'id',
      "Name" => 'name',
      "Description" => 'description',
      # "Created By" => lambda {|it| it['createdBy'] ? it['createdBy']['username'] : '' },
      "Owner" => lambda {|it| it['owner'] ? it['owner']['name'] : '' },
      "Tenants" => lambda {|it| it['accounts'] ? it['accounts'].collect {|acnt| acnt['name']}.join(', ') : '' },
      "Visibility" => lambda {|it| it['visibility'] ? it['visibility'].capitalize() : '' },
      "Public URL" => lambda {|it| it['isPublic'] ? 'Yes' : 'No' },
      "Storage" => lambda {|it| it['storageProvider'] ? it['storageProvider']['name'] : '' },
      "# Files" => lambda {|it| it['fileCount'] },
      "Size" => lambda {|it| format_bytes(it['rawSize']) },
      "Date Created" => lambda {|it| format_local_dt(it['dateCreated']) },
      "Last Updated" => lambda {|it| format_local_dt(it['lastUpdated']) },
    }
    print_description_list(description_cols, archive_bucket)

    # show files
    # search_file_path = "/"
    # if args[1]
    #   search_file_path = args[1]
    # end
    # if search_file_path[0].chr != "/"
    #   search_file_path = "/" + search_file_path
    # end
    # print_h2 "Path: #{search_file_path}"
    print "\n"
    archive_files_json_response = @archive_buckets_interface.list_files(archive_bucket['name'], search_file_path)
    archive_files = archive_files_json_response['archiveFiles']
    if archive_files && archive_files.size > 0
      # archive_files.each do |archive_file|
      #   puts " = #{archive_file['name']}"
      # end
      print_archive_files_table(archive_files)
    else
      if search_file_path.empty? || search_file_path == "/"
        puts "This archive bucket has no files."
      else
        puts "No files found for path #{search_file_path}"
      end
    end
    print cyan

    print reset,"\n"

  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#get_file(args) ⇒ Object



894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
# File 'lib/morpheus/cli/archives_command.rb', line 894

def get_file(args)
  full_command_string = "#{command_name} get-file #{args.join(' ')}".strip
  options = {}
  max_links = 10
  max_history = 10
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path]")
    opts.on('-L', '--all-links', "Display all links instead of only 10." ) do
      max_links = 10000
    end
    opts.on('-H', '--all-history', "Display all history instead of only 10." ) do
      max_history = 10000
    end
    build_common_options(opts, options, [:json, :dry_run, :remote])
    opts.footer = "Get details about an archive file.\n" + 
                  "[bucket:/path] is required. This is the name of the bucket and /path the file or folder to be fetched." + "\n" +
                  "[id] can be passed instead of [bucket:/path]. This is the numeric File ID."
  end
  optparse.parse!(args)
  # consider only allowing args.count == 1 here in the format [bucket:/path]
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} get-file expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  file_id = nil
  bucket_id = nil
  file_path = nil
  # allow id in place of bucket:path
  if args[0].to_s =~ /\A\d{1,}\Z/
    file_id = args[0]
  else
    bucket_id, file_path  = parse_bucket_id_and_file_path(args[0])
  end

  connect(options)
  begin
    # archive_bucket = find_archive_bucket_by_name_or_id(bucket_id)
    # return 1 if archive_bucket.nil?
    params = {}
    @archive_buckets_interface.setopts(options)
    @archive_files_interface.setopts(options)
    if options[:dry_run]
      if file_id
        print_dry_run @archive_files_interface.dry.get(file_id, params), full_command_string
      else
        print_dry_run @archive_buckets_interface.dry.list_files(bucket_id, file_path, params), full_command_string
      end
      return 0
    end
    archive_file = nil
    json_response = nil
    if !file_id
      archive_file = find_archive_file_by_bucket_and_path(bucket_id, file_path)
      return 1 if archive_file.nil?
      file_id = archive_file['id']
    end
    # archive_file = find_archive_file_by_id(file_id)
    json_response = @archive_files_interface.get(file_id, params)
    if options[:json]
      puts as_json(json_response, options)
      return 0
    end
    archive_file = json_response['archiveFile']
    archive_logs = json_response['archiveLogs']
    is_owner = json_response['isOwner']
    if !bucket_id && archive_file["archiveBucket"]
      bucket_id = archive_file["archiveBucket"]["name"]
    end

    print_h1 "Archive File Details"
    print cyan
    description_cols = {
      "File ID" => 'id',
      "Bucket" => lambda {|it| bucket_id },
      "File Path" => lambda {|it| it['filePath'] },
      "Type" => lambda {|it| it['isDirectory'] ? 'directory' : (it['contentType']) },
      "Size" => lambda {|it| format_bytes(it['rawSize']) },
      "Downloads" => lambda {|it| it['downloadCount'] },
      "Created" => lambda {|it| format_local_dt(it['dateCreated']) },
      "Last Modified" => lambda {|it| format_local_dt(it['lastUpdated']) }
    }
    print_description_list(description_cols, archive_file)
    
    # print "\n"
    
    
    print_h2 "Download URLs"
    private_download_url = "#{@appliance_url}/api/archives/download/#{CGI::escape(bucket_id)}" + "/#{CGI::escape(archive_file['filePath'])}".squeeze('/')
    public_download_url = nil
    if archive_file['archiveBucket'] && archive_file['archiveBucket']['isPublic']
      public_download_url = "#{@appliance_url}/public-archives/download/#{CGI::escape(bucket_id)}" + "/#{CGI::escape(archive_file['filePath'])}".squeeze('/')
    end
    print cyan
    puts "Private URL: #{private_download_url}"
    if public_download_url
      puts " Public URL: #{public_download_url}"
    end

    do_show_links = is_owner
    if do_show_links
      links_json_response = @archive_files_interface.list_links(archive_file['id'], {max: max_links})
      archive_file_links = links_json_response['archiveFileLinks']
      if archive_file_links && archive_file_links.size > 0
        print_h2 "Links"
        print_archive_file_links_table(archive_file_links)
        print_results_pagination(links_json_response, {:label => "link", :n_label => "links"})
      else
        print_h2 "File Links"
        puts "No links found"
      end
    end
    # print "\n"
    do_show_history = is_owner
    if do_show_history
      history_json_response = @archive_files_interface.history(archive_file['id'], {max: max_history})
      archive_logs =         history_json_response['archiveLogs']
      print_h2 "History"
      if archive_logs && archive_logs.size > 0
        print_archive_logs_table(archive_logs, {exclude:[:bucket]})
        print_results_pagination(history_json_response, {:label => "history record", :n_label => "history records"})
      else
        puts "No history found"
      end
    end
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#handle(args) ⇒ Object



57
58
59
# File 'lib/morpheus/cli/archives_command.rb', line 57

def handle(args)
  handle_subcommand(args)
end

#list_buckets(args) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/morpheus/cli/archives_command.rb', line 61

def list_buckets(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage()
    build_common_options(opts, options, [:list, :json, :dry_run, :remote])
    opts.footer = "List archive buckets."
  end
  optparse.parse!(args)
  if args.count != 0
    raise_command_error "#{command_name} list expects 0 arguments\n#{optparse}"
  end
  connect(options)
  begin
    params = {}
    [:phrase, :offset, :max, :sort, :direction].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    @archive_buckets_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_buckets_interface.dry.list(params)
      return
    end

    json_response = @archive_buckets_interface.list(params)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
      return
    end
    archive_buckets = json_response['archiveBuckets']
    title = "Morpheus Archive Buckets"
    subtitles = []
    # if group
    #   subtitles << "Group: #{group['name']}".strip
    # end
    # if cloud
    #   subtitles << "Cloud: #{cloud['name']}".strip
    # end
    if params[:phrase]
      subtitles << "Search: #{params[:phrase]}".strip
    end
    print_h1 title, subtitles
    if archive_buckets.empty?
      print cyan,"No archive buckets found.",reset,"\n"
    else
      rows = archive_buckets.collect {|archive_bucket| 
          row = {
            id: archive_bucket['id'],
            name: archive_bucket['name'],
            description: archive_bucket['description'],
            storageProvider: archive_bucket['storageProvider'] ? archive_bucket['storageProvider']['name'] : 'N/A',
            fileCount: archive_bucket['fileCount'],
            # createdBy: archive_bucket['createdBy'] ? archive_bucket['createdBy']['username'] : '',
            size: format_bytes(archive_bucket['rawSize']),
            owner: archive_bucket['owner'] ? archive_bucket['owner']['name'] : '',
            tenants: archive_bucket['accounts'] ? archive_bucket['accounts'].collect {|it| it['name'] }.join(', ') : '',
            visibility: archive_bucket['visibility'] ? archive_bucket['visibility'].capitalize() : '',
            isPublic: archive_bucket['isPublic'] ? 'Yes' : 'No'
          }
          row
        }
        columns = [
          :id, 
          :name, 
          {:storageProvider => {label: 'Storage'.upcase}}, 
          {:fileCount => {label: '# Files'.upcase}}, 
          :size,
          :owner,
          :tenants,
          :visibility,
          {:isPublic => {label: 'Public URL'.upcase}}
        ]
        
        if options[:include_fields]
          columns = options[:include_fields]
        end
        print cyan
        print as_pretty_table(rows, columns, options)
        print reset
        print_results_pagination(json_response, {:label => "bucket", :n_label => "buckets"})
        print reset,"\n"
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    exit 1
  end
end

#list_files(args) ⇒ Object



659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/morpheus/cli/archives_command.rb', line 659

def list_files(args)
  options = {}
  params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path]")
    opts.on('-a', '--all', "Show all files, including subdirectories under the /path.") do
      params[:fullTree] = true
    end
    build_common_options(opts, options, [:list, :json, :dry_run, :remote])
    opts.footer = "List files in an archive bucket. \nInclude [/path] to show files under a directory."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} list-files expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  bucket_id, search_file_path  = parse_bucket_id_and_file_path(args[0])
  connect(options)
  begin
    archive_bucket = find_archive_bucket_by_name_or_id(bucket_id)
    return 1 if archive_bucket.nil?
    [:phrase, :offset, :max, :sort, :direction, :fullTree].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    if params[:phrase]
      params[:fullTree] = true # these are not exclusively supported by api yet
    end
    @archive_buckets_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_buckets_interface.dry.list_files(bucket_id, search_file_path, params)
      return
    end
    json_response = @archive_buckets_interface.list_files(bucket_id, search_file_path, params)
    archive_files = json_response['archiveFiles']
    # archive_bucket = json_response['archiveBucket']
    if options[:json]
      print JSON.pretty_generate(json_response)
      return
    end
    print_h1 "Archive Files", ["#{archive_bucket['name']}:#{search_file_path}"]
    print cyan
    description_cols = {
      "Bucket ID" => 'id',
      "Bucket Name" => 'name',
      #"Path" => lambda {|it| search_file_path }
    }
    #print_description_list(description_cols, archive_bucket)
    #print "\n"
    #print_h2 "Path: #{search_file_path}"
    # print "Directory: #{search_file_path}"
    if archive_files && archive_files.size > 0
      print_archive_files_table(archive_files, {fullTree: params[:fullTree]})
      print_results_pagination(json_response, {:label => "file", :n_label => "files"})
    else
      # puts "No files found for path #{search_file_path}"
      if search_file_path.empty? || search_file_path == "/"
        puts "This archive bucket has no files."
      else
        puts "No files found for path #{search_file_path}"
        return 1
      end
    end
    print reset,"\n"
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#ls(args) ⇒ Object



730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
# File 'lib/morpheus/cli/archives_command.rb', line 730

def ls(args)
  options = {}
  params = {}
  do_one_file_per_line = false
  do_long_format = false
  do_human_bytes = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket/path]")
    opts.on('-a', '--all', "Show all files, including subdirectories under the /path.") do
      params[:fullTree] = true
      do_one_file_per_line = true
    end
    opts.on('-l', '--long', "Lists files in the long format, which contains lots of useful information, e.g. the exact size of the file, the file type, and when it was last modified.") do
      do_long_format = true
      do_one_file_per_line
    end
    opts.on('-H', '--human', "Humanized file sizes. The default is just the number of bytes.") do
      do_human_bytes = true
    end
    opts.on('-1', '--oneline', "One file per line. The default delimiter is a single space.") do
      do_one_file_per_line = true
    end
    build_common_options(opts, options, [:list, :json, :dry_run, :remote])
    opts.footer = "Print filenames for a given archive location.\nPass archive location in the format bucket/path."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} ls expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  bucket_id, search_file_path  = parse_bucket_id_and_file_path(args[0])
  connect(options)
  begin
    # archive_bucket = find_archive_bucket_by_name_or_id(bucket_id)
    # return 1 if archive_bucket.nil?
    [:phrase, :offset, :max, :sort, :direction, :fullTree].each do |k|
      params[k] = options[k] unless options[k].nil?
    end
    @archive_buckets_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_buckets_interface.dry.list_files(bucket_id, search_file_path, params)
      return 0
    end
    json_response = @archive_buckets_interface.list_files(bucket_id, search_file_path, params)
    if options[:json]
      puts as_json(json_response, options)
      # no files is an error condition for this command
      if !json_response['archiveFiles'] || json_response['archiveFiles'].size == 0
        return 1
      end
      return 0
    end
    archive_bucket = json_response['archiveBucket'] # yep, this is returned too
    archive_files = json_response['archiveFiles']
    # archive_bucket = json_response['archiveBucket']
    # print_h2 "Directory: #{search_file_path}"
    # print "Directory: #{search_file_path}"
    if archive_files && archive_files.size > 0
      if do_long_format
        # ls long format
        # owner groups filesize type filename
        now = Time.now
        archive_files.each do |archive_file|
          # -rw-r--r--    1 jdickson  staff   1361 Oct 23 08:00 voltron_2.10.log
          file_color = cyan # reset
          if archive_file['isDirectory']
            file_color = blue
          end
          file_info = []
          # Number of links
          # file_info << file["linkCount"].to_i + 1
          # Owner
          owner_str = ""
          if archive_file['owner']
            owner_str = archive_file['owner']['name']
          elsif archive_bucket['owner']
            owner_str = archive_bucket['owner']['name']
          else
            owner_str = "noone"
          end
          file_info << truncate_string(owner_str, 15).ljust(15, " ")
          # Group (Tenants)
          groups_str = ""
          if archive_file['visibility'] == 'public'
            # this is confusing because of Public URL (isPublic) setting
            groups_str = "public"
          else
            if archive_file['accounts'].instance_of?(Array) && archive_file['accounts'].size > 0
              # groups_str = archive_file['accounts'].collect {|it| it['name'] }.join(',')
              groups_str = (archive_file['accounts'].size == 1) ? "#{archive_file['accounts'][0]['name']}" : "#{archive_file['accounts'].size} tenants"
            elsif archive_bucket['accounts'].instance_of?(Array) && archive_bucket['accounts'].size > 0
              # groups_str = archive_bucket['accounts'].collect {|it| it['name'] }.join(',')
              groups_str = (archive_bucket['accounts'].size == 1) ? "#{archive_bucket['accounts'][0]['name']}" : "#{archive_bucket['accounts'].size} tenants"
            else
              groups_str = owner_str
            end
          end
          file_info << truncate_string(groups_str, 15).ljust(15, " ")
          # File Type
          content_type = archive_file['contentType'].to_s
          if archive_file['isDirectory']
            content_type = "directory"
          else
            content_type = archive_file['contentType'].to_s
          end
          file_info << content_type.ljust(25, " ")
          filesize_str = ""
          if do_human_bytes
            # filesize_str = format_bytes(archive_file['rawSize'])
            filesize_str = format_bytes_short(archive_file['rawSize'])
          else
            filesize_str = archive_file['rawSize'].to_i.to_s
          end
          # file_info << filesize_str.ljust(12, " ")
          file_info << filesize_str.ljust(7, " ")
          mtime = ""
          last_updated = parse_time(archive_file['lastUpdated'])
          if last_updated
            if last_updated.year == now.year
              mtime = format_local_dt(last_updated, {format: "%b %e %H:%M"})
            else
              mtime = format_local_dt(last_updated, {format: "%b %e %Y"})
            end
          end
          file_info << mtime.ljust(12, " ")
          if params[:fullTree]
            file_info << file_color + archive_file["filePath"].to_s + cyan
          else
            file_info << file_color + archive_file["name"].to_s + cyan
          end
          print cyan, file_info.join("  "), reset, "\n"
        end
      else
        file_names = archive_files.collect do |archive_file|
          file_color = cyan # reset
          if archive_file['isDirectory']
            file_color = blue
          end
          if params[:fullTree]
            file_color + archive_file["filePath"].to_s + reset
          else
            file_color + archive_file["name"].to_s + reset
          end
        end
        if do_one_file_per_line
          print file_names.join("\n")
        else
          print file_names.join("\t")
        end
        print "\n"
      end
    else
      print_error yellow, "No files found for path: #{search_file_path}", reset, "\n"
      return 1
    end
    
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#read_file(args) ⇒ Object



1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
# File 'lib/morpheus/cli/archives_command.rb', line 1356

def read_file(args)
  full_command_string = "archives read #{args.join(' ')}".strip
  options = {}
  outfile = nil
  do_overwrite = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path]")
    build_common_options(opts, options, [:auto_confirm, :dry_run, :remote])
    opts.footer = "Print the contents of an archive file.\n" + 
                  "[bucket:/path] is required. This is the name of the bucket and /path the file or folder to be downloaded.\n" +
                  "Confirmation is needed if the specified file is more than 1KB.\n" +
                  "This confirmation can be skipped with the -y option."
  end
  optparse.parse!(args)
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} read expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  connect(options)
  begin
    bucket_id, file_path  = parse_bucket_id_and_file_path(args[0])
    archive_file = find_archive_file_by_bucket_and_path(bucket_id, file_path)
    return 1 if archive_file.nil?
    full_file_path = "#{bucket_id}/#{file_path}".squeeze('/')
    @archive_files_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_files_interface.dry.download_file_by_path(full_file_path), full_command_string
      return 1
    end
    if archive_file['rawSize'].to_i > 1024
      pretty_size = format_bytes(archive_file['rawSize'])
      unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to print the contents of this file (#{pretty_size}) ?")
        return 9, "aborted command"
      end
    end
    file_response = @archive_files_interface.download_file_by_path(full_file_path)
    puts file_response.body.to_s
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
  
end

#remove_bucket(args) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'lib/morpheus/cli/archives_command.rb', line 391

def remove_bucket(args)
  full_command_string = "#{command_name} remove #{args.join(' ')}".strip
  options = {}
  query_params = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket]")
    build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
  end
  optparse.parse!(args)

  if args.count < 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} remove expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  bucket_id = args[0]
  connect(options)
  begin
    # archive_bucket = find_archive_bucket_by_name_or_id(args[0])
    json_response = @archive_buckets_interface.get(bucket_id, {})
    archive_bucket = json_response['archiveBucket']
    is_owner = json_response['isOwner']
    return 1 if archive_bucket.nil?
    if is_owner == false
      print_red_alert "You must be the owner of archive bucket to remove it."
      return 3
    end
    unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the archive bucket: #{archive_bucket['name']}?")
      return 9, "aborted command"
    end
    @archive_buckets_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_buckets_interface.dry.destroy(archive_bucket['id'], query_params), full_command_string
      return 0
    end
    json_response = @archive_buckets_interface.destroy(archive_bucket['id'], query_params)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Removed archive bucket #{archive_bucket['name']}"
      # list([])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#remove_file(args) ⇒ Object

Use upload file bucket:/path def add_file(args)

raise "not yet implemented"

end



1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
# File 'lib/morpheus/cli/archives_command.rb', line 1032

def remove_file(args)
  options = {}
  query_params = {}
  do_recursive = false
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path]")
    opts.on( '-R', '--recursive', "Delete a directory and all of its files. This must be passed if specifying a directory." ) do
      do_recursive = true
    end
    build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
    opts.footer = "Delete an archive file or directory."
  end
  optparse.parse!(args)
  # consider only allowing args.count == 1 here in the format [bucket:/path]
  if args.count != 1
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} remove-file expects 1 argument and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  bucket_id, file_path  = parse_bucket_id_and_file_path(args[0])
  connect(options)
  begin
    
    archive_file = find_archive_file_by_bucket_and_path(bucket_id, file_path)
    return 1 if archive_file.nil?
    if archive_file['isDirectory']
      if !do_recursive
        print_error Morpheus::Terminal.angry_prompt
        puts_error  "bad argument: '#{file_path}' is a directory.  Use -R or --recursive to delete a directory.\n#{optparse}"
        return 1
      end
      unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the archive directory: #{args[0]}?")
        return 9, "aborted command"
      end
    else
      unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to delete the archive file: #{args[0]}?")
        return 9, "aborted command"
      end
    end
    @archive_files_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_files_interface.dry.destroy(archive_file['id'], query_params)
      return 0
    end
    json_response = @archive_files_interface.destroy(archive_file['id'], query_params)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    else
      print_green_success "Removed archive file #{args[0]}"
    end
    return 0

  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end

end


1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
# File 'lib/morpheus/cli/archives_command.rb', line 1451

def remove_file_link(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket:/path] [token]")
    build_common_options(opts, options, [:auto_confirm, :dry_run, :quiet, :remote])
    opts.footer = "Delete a public link to a file.\n" + 
                  "[bucket:/path] is required. This is the name of the bucket and /path the file or folder to be fetched." +
                  "[token] is required. This is the secret access key that identifies the link."
  end
  optparse.parse!(args)
  # consider only allowing args.count == 1 here in the format [bucket:/path]
  if args.count != 2
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} remove-file-link expects 2 arguments and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  bucket_id, file_path  = parse_bucket_id_and_file_path(args[0])
  connect(options)
  begin
    archive_file = find_archive_file_by_bucket_and_path(bucket_id, file_path)
    return 1 if archive_file.nil?
    link_id = nil
    secret_access_key = args[1]
    secret_access_key = secret_access_key.sub('/public-archives/link?s=', '')
    # find the int id via token...
    links_json_response = @archive_files_interface.list_links(archive_file['id'], {s: secret_access_key})
    if links_json_response['archiveFileLinks'] && links_json_response['archiveFileLinks'][0]
      link_id = links_json_response['archiveFileLinks'][0]['id']
    end
    if !link_id
      print_red_alert "Archive file link not found for #{bucket_id}:/#{archive_file['filePath']} token: #{secret_access_key}"
      return 1
    end
    params = {}
    @archive_files_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_files_interface.dry.destroy_file_link(archive_file['id'], link_id, params)
      return
    end
    json_response = @archive_files_interface.destroy_file_link(archive_file['id'], link_id, params)

    if options[:json]
      print JSON.pretty_generate(json_response)
      return 0
    elsif !options[:quiet]
      print_green_success "Deleted archive file link #{bucket_id}:/#{archive_file['filePath']} token: #{secret_access_key}"
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#update_bucket(args) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/morpheus/cli/archives_command.rb', line 313

def update_bucket(args)
  options = {}
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[bucket] [options]")
    opts.on('--name VALUE', String, "Name") do |val|
      options['name'] = val
    end
    opts.on('--description VALUE', String, "Description") do |val|
      options['description'] = val
    end
    # storage provider cannot be changed
    # opts.on('--storageProvider VALUE', String, "Storage Provider ID") do |val|
    #   options['storageProvider'] = val.to_s
    # end
    opts.on('--payload JSON', String, "JSON Payload") do |val|
      options['payload'] = JSON.parse(val.to_s)
    end
    opts.on('--payload-file FILE', String, "JSON Payload from a local file") do |val|
      payload_file = val.to_s
      options['payload'] = JSON.parse(File.read(payload_file))
    end
    
    opts.on('--visibility [private|public]', String, "Visibility determines if read access is restricted to the specified Tenants (Private) or all tenants (Public).") do |val|
      options['visibility'] = val.to_s
    end
    opts.on('--accounts LIST', String, "Tenant Accounts (comma separated ids)") do |val|
      # uh don't put commas or leading/trailing spaces in script names pl
      options['accounts'] = val.to_s.split(",").collect {|it| it.to_s.strip }.select {|it| it }.compact
    end
    opts.on('--isPublic [on|off]', String, "Enabling Public URL allows files to be downloaded without any authentication.") do |val|
      options['isPublic'] = (val.to_s == 'on' || val.to_s == 'true')
    end
    build_common_options(opts, options, [:options, :payload, :json, :dry_run, :quiet, :remote])
    opts.footer = "Update an existing archive bucket."
  end
  optparse.parse!(args)
  if args.count < 1
    puts optparse
    return 1
  end
  connect(options)

  begin
    archive_bucket = find_archive_bucket_by_name_or_id(args[0])

    options.merge!(options[:options]) if options[:options] # so -O var= works..
    option_params = options.reject {|k,v| k.is_a?(Symbol) }

    payload = nil
    if options[:payload]
      payload = options[:payload]
      payload.deep_merge!({'archiveBucket' => option_params}) if !option_params.empty?
    else
      archive_bucket_payload = prompt_edit_archive_bucket(archive_bucket, options)
      return 1 if !archive_bucket_payload
      payload = {'archiveBucket' => archive_bucket_payload}
    end
    @archive_buckets_interface.setopts(options)
    if options[:dry_run]
      print_dry_run @archive_buckets_interface.dry.update(archive_bucket["id"], payload)
      return
    end

    json_response = @archive_buckets_interface.update(archive_bucket["id"], payload)
    if options[:json]
      print JSON.pretty_generate(json_response)
      print "\n"
    elsif !options[:quiet]
      print_green_success "Updated archive bucket #{archive_bucket['name']}"
      get([archive_bucket['id']])
    end
    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end

#upload_file(args) ⇒ Object



441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
# File 'lib/morpheus/cli/archives_command.rb', line 441

def upload_file(args)
  options = {}
  query_params = {}
  do_recursive = false
  ignore_regexp = nil
  optparse = Morpheus::Cli::OptionParser.new do |opts|
    opts.banner = subcommand_usage("[local-file] [bucket:/path]")
    # opts.on('--filename FILEPATH', String, "Remote file path for the file or folder being uploaded, this is an alternative to [remote-file-path]." ) do |val|
    #   options['type'] = val
    # end
    opts.on( '-R', '--recursive', "Upload a directory and all of its files. This must be passed if [local-file] is a directory." ) do
      do_recursive = true
    end
    opts.on('--ignore-files PATTERN', String, "Pattern of files to be ignored when uploading a directory." ) do |val|
      ignore_regexp = /#{Regexp.escape(val)}/
    end
    opts.footer = "Upload a local file or folder to an archive bucket. " +
                  "\nThe first argument [local-file] should be the path of a local file or directory." +
                  "\nThe second argument [bucket:/path] should contain the bucket name." +
                  "\nThe [:/path] component is optional and can be used to specify the destination of the uploaded file or folder." +
                  "\nThe default destination is the same name as the [local-file], under the root bucket directory '/'. " +
                  "\nThis will overwrite any existing remote files that match the destination /path."
    build_common_options(opts, options, [:auto_confirm, :json, :dry_run, :remote])
  end
  optparse.parse!(args)
  
  if args.count != 2
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} upload expects 2 arguments and received #{args.count}: #{args.join(' ')}\n#{optparse}"
    return 1
  end
  # validate local file path
  local_file_path = File.expand_path(args[0].squeeze('/'))
  if local_file_path == "" || local_file_path == "/" || local_file_path == "."
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} missing argument: [local-file]\n#{optparse}"
    return 1
  end
  if !File.exists?(local_file_path)
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} bad argument: [local-file]\nFile '#{local_file_path}' was not found.\n#{optparse}"
    return 1
  end

  # validate bucket:/path
  bucket_id, remote_file_path  = parse_bucket_id_and_file_path(args[1])

  # if local_file_path.include?('../') # || options[:yes]
  #   raise_command_error "Sorry, you may not use relative paths in your local filepath."
  # end
  
  # validate bucket name (or id)
  if !bucket_id
    print_error Morpheus::Terminal.angry_prompt
    puts_error  "#{command_name} missing argument: [bucket]\n#{optparse}"
    return 1
  end
  
  # strip leading slash of remote name
  # if remote_file_path[0].chr == "/"
  #   remote_file_path = remote_file_path[1..-1]
  # end

  if remote_file_path.include?('./') # || options[:yes]
    raise_command_error "Sorry, you may not use relative paths in your remote filepath."
  end

  # if !options[:yes]
  scary_local_paths = ["/", "/root", "C:\\"]
  if scary_local_paths.include?(local_file_path)
    unless Morpheus::Cli::OptionTypes.confirm("Are you sure you want to upload all the files in local directory '#{local_file_path}' !?")
      return 9, "aborted command"
    end
  end
  # end

  connect(options)
  begin
    archive_bucket = find_archive_bucket_by_name_or_id(bucket_id)
    return 1 if archive_bucket.nil?

    # how many files we dealing with?
    files_to_upload = []
    if File.directory?(local_file_path)
      # upload directory
      if !do_recursive
        print_error Morpheus::Terminal.angry_prompt
        puts_error  "bad argument: '#{local_file_path}' is a directory.  Use -R or --recursive to upload a directory.\n#{optparse}"
        return 1
      end
      found_files = Dir.glob("#{local_file_path}/**/*")
      # note:  api call for directories is not needed
      found_files = found_files.select {|file| File.file?(file) }
      if ignore_regexp
        found_files = found_files.reject {|it| it =~ ignore_regexp} 
      end
      files_to_upload = found_files

      if files_to_upload.size == 0
        print_error Morpheus::Terminal.angry_prompt
        puts_error  "bad argument: Local directory '#{local_file_path}' contains 0 files."
        return 1
      end

      unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to upload directory #{local_file_path} (#{files_to_upload.size} files) to #{archive_bucket['name']}:#{remote_file_path}?")
        return 9, "aborted command"
      end

      if !options[:yes]
        if files_to_upload.size > 100
          unless Morpheus::Cli::OptionTypes.confirm("Are you REALLY sure you want to upload #{files_to_upload.size} files ?")
            return 9, "aborted command"
          end
        end
      end

      # local_dirname = File.dirname(local_file_path)
      # local_basename = File.basename(local_file_path)
      upload_file_list = []
      files_to_upload.each do |file|
        destination = file.sub(local_file_path, (remote_file_path || "")).squeeze('/')
        upload_file_list << {file: file, destination: destination}
      end

      @archive_buckets_interface.setopts(options)
      if options[:dry_run]
        # print_h1 "DRY RUN"
        print "\n",cyan, bold, "Uploading #{upload_file_list.size} Files...", reset, "\n"
        upload_file_list.each do |obj|
          file, destination = obj[:file], obj[:destination]
          #print cyan,bold, "  - Uploading #{file} to #{bucket_id}:#{destination} DRY RUN", reset, "\n"
          print_dry_run @archive_buckets_interface.dry.upload_file(bucket_id, file, destination)
          print "\n"
        end
        return 0
      end

      print "\n",cyan, bold, "Uploading #{upload_file_list.size} Files...", reset, "\n"
      bad_upload_responses = []
      upload_file_list.each do |obj|
        file, destination = obj[:file], obj[:destination]
        print cyan,bold, "  - Uploading #{file} to #{bucket_id}:#{destination}", reset
        upload_response = @archive_buckets_interface.upload_file(bucket_id, file, destination)
        if upload_response['success']
          print bold," #{green}SUCCESS#{reset}"
        else
          print bold," #{red}ERROR#{reset}"
          if upload_response['msg']
            bad_upload_responses << upload_response
            print " #{upload_response['msg']}#{reset}"
          end
        end
        print "\n"
      end
      if bad_upload_responses.size > 0
        print cyan, bold, "Completed Upload of #{upload_file_list.size} Files. #{red}#{bad_upload_responses.size} Errors!", reset, "\n"
      else
        print cyan, bold, "Completed Upload of #{upload_file_list.size} Files!", reset, "\n"
      end

    else

      # upload file
      if !File.exists?(local_file_path) && !File.file?(local_file_path)
        print_error Morpheus::Terminal.angry_prompt
        puts_error  "#{command_name} bad argument: [local-file]\nFile '#{local_file_path}' was not found.\n#{optparse}"
        return 1
      end

      # local_dirname = File.dirname(local_file_path)
      # local_basename = File.basename(local_file_path)
      
      file = local_file_path
      destination = File.basename(file)
      if remote_file_path[-1].chr == "/"
        # work like `cp`, and place into the directory
        destination = remote_file_path + File.basename(file)
      elsif remote_file_path
        # renaming file
        destination = remote_file_path
      end
      destination = destination.squeeze('/')

      unless options[:yes] || Morpheus::Cli::OptionTypes.confirm("Are you sure you want to upload #{local_file_path} to #{archive_bucket['name']}:#{destination}?")
        return 9, "aborted command"
      end

      @archive_buckets_interface.setopts(options)
      if options[:dry_run]
        #print cyan,bold, "  - Uploading #{file} to #{bucket_id}:#{destination} DRY RUN", reset, "\n"
        # print_h1 "DRY RUN"
        print_dry_run @archive_buckets_interface.dry.upload_file(bucket_id, file, destination)
        print "\n"
        return 0
      end
    
      print cyan,bold, "  - Uploading #{file} to #{bucket_id}:#{destination}", reset
      upload_response = @archive_buckets_interface.upload_file(bucket_id, file, destination)
      if upload_response['success']
        print bold," #{green}Success#{reset}"
      else
        print bold," #{red}Error#{reset}"
        if upload_response['msg']
          print " #{upload_response['msg']}#{reset}"
        end
      end
      print "\n"

    end
    #print cyan, bold, "Upload Complete!", reset, "\n"

    return 0
  rescue RestClient::Exception => e
    print_rest_exception(e, options)
    return 1
  end
end