Class: CLI
- Inherits:
-
Object
- Object
- CLI
- Defined in:
- lib/gdrive-lib.rb
Overview
TODO: double upload shouldn’t be possible (same file name), but is is now.
Defined Under Namespace
Classes: ErrorNotDirectory, InvalidPathError
Class Method Summary collapse
-
.build(credentials_file_path) ⇒ Object
Creates a session.
-
.ensure_credentials_file(credentials_file_path) ⇒ Object
This has to be a file, so it can be updated with scope and refresh token (automatically).
Instance Method Summary collapse
- #command_cat(remote_path) ⇒ Object
- #command_download(remote_path, local_path = File.basename(remote_path)) ⇒ Object
- #command_find(destination) ⇒ Object
- #command_list(destination) ⇒ Object
- #command_mkdir(remote_path) ⇒ Object
- #command_remove(remote_path) ⇒ Object
-
#command_search(title) ⇒ Object
FIXME: Doesn’t work.
-
#command_upload(*args) ⇒ Object
Upload to WIP: upload song.mp3 song2.mp3 upload song.mp3 song2.mp3 – songs.
-
#initialize(session) ⇒ CLI
constructor
A new instance of CLI.
Constructor Details
#initialize(session) ⇒ CLI
Returns a new instance of CLI.
35 36 37 |
# File 'lib/gdrive-lib.rb', line 35 def initialize(session) @session = session end |
Class Method Details
.build(credentials_file_path) ⇒ Object
Creates a session. This will prompt the credential via command line for the first time and save it to config.json file for later usages. See this document to learn how to create config.json: github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md
31 32 33 |
# File 'lib/gdrive-lib.rb', line 31 def self.build(credentials_file_path) self.new(GoogleDrive::Session.from_config(credentials_file_path)) end |
.ensure_credentials_file(credentials_file_path) ⇒ Object
This has to be a file, so it can be updated with scope and refresh token (automatically).
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gdrive-lib.rb', line 16 def self.ensure_credentials_file(credentials_file_path) unless File.file?(credentials_file_path) id = ENV.fetch('GDRIVE_CLIENT_ID') secret = ENV.fetch('GDRIVE_CLIENT_SECRET') File.open(credentials_file_path, 'w') do |file| file.puts({client_id: id, client_secret: secret}.to_json) end end end |
Instance Method Details
#command_cat(remote_path) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gdrive-lib.rb', line 71 def command_cat(remote_path) remote_path, local_path = ARGV local_path ||= File.basename(remote_path) file = self.find_file(remote_path) file || abort("File #{remote_path} not found") if file.is_a?(GoogleDrive::Collection) abort "File #{remote_path} is a directory" else puts file.download_to_string end rescue Interrupt puts end |
#command_download(remote_path, local_path = File.basename(remote_path)) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/gdrive-lib.rb', line 85 def command_download(remote_path, local_path = File.basename(remote_path)) puts "~ Downloading '#{remote_path}' to #{local_path}" download(remote_path, local_path) rescue Interrupt puts end |
#command_find(destination) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/gdrive-lib.rb', line 50 def command_find(destination) self.find(self.get_collection(destination)) rescue ErrorNotDirectory => error # If given path points to a file, show more details. p error.file rescue Interrupt puts end |
#command_list(destination) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/gdrive-lib.rb', line 39 def command_list(destination) collection = self.get_collection(destination) collection.files.uniq(&:title).sort_by(&:title).each do |file| puts file.is_a?(GoogleDrive::Collection) ? "#{`tput setaf 4`}#{file.title}#{`tput sgr0`}" : file.title end rescue ErrorNotDirectory => error # If given path points to a file, show more details. p error.file rescue Interrupt puts end |
#command_mkdir(remote_path) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/gdrive-lib.rb', line 127 def command_mkdir(remote_path) *dirnames, basename = ARGV.first.split('/') collection = dirnames.empty? ? @session.root_collection : self.find_file(dirnames.join('/')) collection.create_subcollection(ARGV.first.split('/').last) rescue Interrupt puts end |
#command_remove(remote_path) ⇒ Object
135 136 137 138 139 140 141 |
# File 'lib/gdrive-lib.rb', line 135 def command_remove(remote_path) puts "~ Deleting '#{ARGV.first}'" file = self.find_file(ARGV.first) file.delete(true) rescue Interrupt puts end |
#command_search(title) ⇒ Object
FIXME: Doesn’t work.
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/gdrive-lib.rb', line 59 def command_search(title) drive_service = @session.instance_variable_get(:@fetcher).drive query = "title contains '#{ARGV.first}'" puts "Query: #{query}" files = drive_service.list_files(q: query) files.items.each do |file| puts file.title end rescue Interrupt puts end |
#command_upload(*args) ⇒ Object
Upload to WIP: upload song.mp3 song2.mp3 upload song.mp3 song2.mp3 – songs
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 |
# File 'lib/gdrive-lib.rb', line 95 def command_upload(*args) if separator_index = args.index('--') local_paths = args[0..(separator_index - 1)] remote_paths = args[(separator_index + 1)..-1] if remote_path.length != 1 abort "x" end remote_path = remote_paths.first else local_paths = args remote_path = local_paths.length == 1 ? local_paths.first : 'WIP' end require 'pry'; binding.pry ### local_path, remote_path = "/#{File.basename(local_path)}" # If path is given as relative, File.dirname(remote_path) returns '.' and # self.find_file returns nil. remote_path = "/#{remote_path}" unless remote_path.start_with?('/') puts "~ Uploading '#{local_path}' to #{remote_path}" file = @session.upload_from_file(local_path, File.basename(remote_path), convert: false) # File is always uploaded to the root_collection by default. # https://github.com/gimite/google-drive-ruby/issues/260 unless self.find_file(File.dirname(remote_path)) == @session.root_collection require 'pry'; binding.pry ### self.find_file(File.dirname(remote_path)).add(file) @session.root_collection.remove(file) end rescue Interrupt puts end |