Sha256: b404632834cda78a7c2c295723f4cfd438cdd0c89307b1b76d486a76fee695fd

Contents?: true

Size: 2 KB

Versions: 2

Compression:

Stored size: 2 KB

Contents

# 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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
boilercode-0.1.2 lib/boilercode/commands/dotfiles/download.rb
boilercode-0.1.1 lib/boilercode/commands/dotfiles/download.rb