class PackageCloud::Validator

Public Class Methods

new(client) click to toggle source
# File lib/package_cloud/validator.rb, line 3
def initialize(client)
  @client = client
end

Public Instance Methods

distribution_id(repo, filenames, package_type) click to toggle source
# File lib/package_cloud/validator.rb, line 20
def distribution_id(repo, filenames, package_type)
  if distributions[package_type]
    _,_,dist_sel,ver_sel = repo.split("/")

    # These are all 'single version' distros
    if (dist_sel == "python" || dist_sel == "java" || dist_sel == "node" || dist_sel == "anyfile")
      dist = distributions[package_type].detect { |d| d["index_name"] == dist_sel }
      dist["versions"].first["id"]
    elsif dist_sel && ver_sel
      dist = distributions[package_type].detect { |d| d["index_name"] == dist_sel }

      if dist
        ver = dist["versions"].detect { |v| v["index_name"] == ver_sel || v["version_number"] == ver_sel }

        if ver
          ver["id"]
        else
          puts "#{ver_sel} isn't a valid version of #{dist["display_name"]}\n\n"
          select_dist(repo, filenames, package_type)
        end
      else
        puts "#{dist_sel} isn't a valid operating system.\n\n"
        select_dist(repo, filenames, package_type)
      end
    else
      puts "#{package_type} packages require an operating system and version to be selected.\n\n"
      select_dist(repo, filenames, package_type)
    end
  else
    nil
  end
end
distribution_id_from_repo_url(repo, filenames) click to toggle source
# File lib/package_cloud/validator.rb, line 7
def distribution_id_from_repo_url(repo, filenames)
  _,_,dist_sel,ver_sel = repo.split("/")
 package_type = dist_sel
 puts "ambiguous extensions, so using repo url to deduce package_type, package_type: #{package_type}".color(:red)

 # NOTE: The distributions pulled from API uses "py" (package_type) as key for "python"
 #       so we need to map "python" to "py"
 #       See PackageCloud::Validator#distributions
 package_type = package_type == "python" ? "py": package_type

 distribution_id(repo, filenames, package_type)
end

Private Instance Methods

distributions() click to toggle source
# File lib/package_cloud/validator.rb, line 54
def distributions
  @distributions ||= @client.distributions
end
get_valid(prompt) click to toggle source
# File lib/package_cloud/validator.rb, line 66
def get_valid(prompt)
  selection = ""
  times = 0
  until yield(selection)
    if times > 0
      puts "#{selection} is not a valid selection."
    end
    print "\n #{prompt}: "
    selection = ::Kernel.gets.chomp
    times += 1
  end

  selection
end
select_dist(repo, filenames, package_type) click to toggle source
# File lib/package_cloud/validator.rb, line 81
def select_dist(repo, filenames, package_type)
  puts "If you don't see your OS or version here, send us an email at support@packagecloud.io:\n\n"
  all_distros = distributions[package_type]

  filtered_distros = all_distros.select {|dist| dist["index_name"] != "any" && dist["index_name"] != "rpm_any" }
  
  filtered_distros.each_with_index do |dist, index|
    puts "\t#{index}. #{dist["display_name"]}"
  end

  distro = select_from(filtered_distros)

  puts "\nYou selected #{distro["display_name"]}. Select a version:\n\n"
  distro["versions"].each_with_index do |ver, index|
    puts "\t#{index}. #{ver["display_name"]} (#{ver["index_name"]})"
  end
  version = select_from(distro["versions"])

  repo = repo.split("/")[0..1].join("/")
  os_shortcut = "#{distro["index_name"]}/#{version["index_name"]}"
  shortcut = "#{repo}/#{os_shortcut}"
  if filenames.length > 1
    puts "\nPush #{filenames.length} packages to #{shortcut}? "
  else
    puts "\nPush #{filenames.first} to #{shortcut}? "
  end
  answer = get_valid("(y/n)") { |sel| sel == "y" || sel == "n" }

  if answer == "y"
    print "\nContinuing...".color(:green)
    puts " Note that next time you can push directly to #{os_shortcut} by specifying #{shortcut} on the commandline."
    version["id"]
  else
    abort("Cancelled.")
  end
end
select_from(list) click to toggle source
# File lib/package_cloud/validator.rb, line 58
def select_from(list)
  selection = get_valid("0-#{list.length - 1}") do |s|
    s =~ /^\d+$/ && list[s.to_i]
  end

  list[selection.to_i]
end