# frozen_string_literal: true require_relative "../../../boilercode" module Boilercode module Commands class Dotfiles class Download < Boilercode::Command def initialize(options) @options = options end def execute(input: $stdin, output: $stdout, page: 1) res = client.get("/dotfiles?page=#{page}") if res.success? output.puts handle_search_response(res) else output.puts pastel.red res.inspect end end def handle_search_response(res) headers = res.headers response = parse_json(res) Dir.chdir(File.expand_path("~")) if response.empty? pastel.red "No uploads found, try updating your query." elsif prompt.yes?("Do you want to download all dotfiles?") download_all(response) else handle_selecting_dotfiles(response, headers) end end def download_all(response) response.each do |upload| download(upload["url"]) puts pastel.green(upload["url"]) end end def handle_selecting_dotfiles(response, headers) dotfiles = prompt.multi_select("Choose your dotfiles:", choices(response, headers)) # choice = dotfiles.first # if choice == "Next Page" # return execute(page: headers["Current-Page"].to_i + 1) # end dotfiles.each do |file| upload = response.find { |dotfile| dotfile["filename"] == file } download(upload["url"]) if download_file? puts pastel.green(upload["url"]) end end def choices(response, headers) choices = response.map { |upload| upload["filename"] } choices << "Next Page" if headers["Total-Pages"].to_i > 1 choices end def download_file? ask("Do you want to download the file? (y/n)") == "y" end end end end end # here