Class: Neocities::CLI
- Inherits:
-
Object
- Object
- Neocities::CLI
- Defined in:
- lib/neocities/cli.rb
Constant Summary collapse
- API_KEY_PATH =
ENV['HOME']+'/.neocities/api_key'
- SUBCOMMANDS =
%w{upload delete list info sync pizza}
- HELP_SUBCOMMANDS =
['-h', '--help', 'help']
- PENELOPE_MOUTHS =
%w{^ o ~ - v U}
- PENELOPE_EYES =
%w{o ~ O}
Instance Method Summary collapse
- #delete ⇒ Object
- #display_banner ⇒ Object
- #display_delete_help_and_exit ⇒ Object
- #display_help_and_exit ⇒ Object
- #display_info_help_and_exit ⇒ Object
- #display_list_help_and_exit ⇒ Object
- #display_pizza_help_and_exit ⇒ Object
- #display_response(resp) ⇒ Object
- #display_sync_help_and_exit ⇒ Object
- #display_upload_help_and_exit ⇒ Object
- #info ⇒ Object
-
#initialize(argv) ⇒ CLI
constructor
A new instance of CLI.
- #list ⇒ Object
- #run ⇒ Object
- #sync ⇒ Object
- #upload ⇒ Object
Constructor Details
#initialize(argv) ⇒ CLI
Returns a new instance of CLI.
16 17 18 19 20 21 22 23 |
# File 'lib/neocities/cli.rb', line 16 def initialize(argv) @argv = argv.dup @pastel = Pastel.new eachline: "\n" @subcmd = @argv.first @subargs = @argv[1..@argv.length] @prompt = TTY::Prompt.new @api_key = ENV['NEOCITIES_API_KEY'] || nil end |
Instance Method Details
#delete ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/neocities/cli.rb', line 76 def delete @subargs.each do |file| puts @pastel.bold("Deleting #{file} ...") resp = @client.delete file display_response resp end end |
#display_banner ⇒ Object
315 316 317 318 319 320 321 322 323 |
# File 'lib/neocities/cli.rb', line 315 def puts <<HERE |\\---/| | #{PENELOPE_EYES.sample}_#{PENELOPE_EYES.sample} | #{@pastel.on_cyan.bold ' Neocities '} \\_#{PENELOPE_MOUTHS.sample}_/ HERE end |
#display_delete_help_and_exit ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/neocities/cli.rb', line 251 def display_delete_help_and_exit puts <<HERE #{@pastel.green.bold 'delete'} - Delete files on your Neocities site #{@pastel.dim 'Examples:'} #{@pastel.green '$ neocities delete myfile.jpg'} Delete myfile.jpg #{@pastel.green '$ neocities delete myfile.jpg myfile2.jpg'} Delete myfile.jpg and myfile2.jpg HERE exit end |
#display_help_and_exit ⇒ Object
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
# File 'lib/neocities/cli.rb', line 325 def display_help_and_exit puts <<HERE #{@pastel.dim 'Subcommands:'} sync Recursively upload a local directory to your site upload Upload individual files to your Neocities site delete Delete files from your Neocities site list List files from your Neocities site info Information and stats for your site version Unceremoniously display version and self destruct pizza Order a free pizza HERE exit end |
#display_info_help_and_exit ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/neocities/cli.rb', line 301 def display_info_help_and_exit puts <<HERE #{@pastel.green.bold 'info'} - Get site info #{@pastel.dim 'Examples:'} #{@pastel.green '$ neocities info fauux'} Gets info for 'fauux' site HERE exit end |
#display_list_help_and_exit ⇒ Object
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/neocities/cli.rb', line 233 def display_list_help_and_exit puts <<HERE #{@pastel.green.bold 'list'} - List files on your Neocities site #{@pastel.dim 'Examples:'} #{@pastel.green '$ neocities list /'} List files in your root directory #{@pastel.green '$ neocities list -a'} Recursively display all files and directories #{@pastel.green '$ neocities list -d /mydir'} Show detailed information on /mydir HERE exit end |
#display_pizza_help_and_exit ⇒ Object
228 229 230 231 |
# File 'lib/neocities/cli.rb', line 228 def display_pizza_help_and_exit puts "Sorry, we're fresh out of dough today. Try again tomorrow." exit end |
#display_response(resp) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/neocities/cli.rb', line 25 def display_response(resp) if resp[:result] == 'success' puts "#{@pastel.green.bold 'SUCCESS:'} #{resp[:message]}" else out = "#{@pastel.red.bold 'ERROR:'} #{resp[:message]}" out += " (#{resp[:error_type]})" if resp[:error_type] puts out end end |
#display_sync_help_and_exit ⇒ Object
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/neocities/cli.rb', line 283 def display_sync_help_and_exit puts <<HERE #{@pastel.green.bold 'sync'} - Upload a local directory to your Neocities site #{@pastel.dim 'Examples:'} #{@pastel.green '$ neocities sync .'} Recursively upload current directory #{@pastel.green '$ neocities sync -e node_modules -e secret.txt .'} Exclude certain files from sync #{@pastel.green '$ neocities sync --no-gitignore .'} Don't use .gitignore to exclude files HERE exit end |
#display_upload_help_and_exit ⇒ Object
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/neocities/cli.rb', line 267 def display_upload_help_and_exit puts <<HERE #{@pastel.green.bold 'upload'} - Upload individual files to your Neocities site #{@pastel.dim 'Examples:'} #{@pastel.green '$ neocities upload img.jpg img2.jpg'} Upload images to the root of your site #{@pastel.green '$ neocities upload -d images img.jpg'} Upload img.jpg to the 'images' directory on your site HERE exit end |
#info ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/neocities/cli.rb', line 85 def info resp = @client.info(@subargs[0] || @sitename) if resp[:result] == 'error' display_response resp exit end out = [] resp[:info].each do |k,v| v = Time.parse(v).localtime if k == :created_at || k == :last_updated out.push [@pastel.bold(k), v] end puts TTY::Table.new(out).to_s exit end |
#list ⇒ Object
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 |
# File 'lib/neocities/cli.rb', line 104 def list if @subargs.delete('-d') == '-d' @detail = true end if @subargs.delete('-a') @subargs[0] = nil end resp = @client.list @subargs[0] if resp[:result] == 'error' display_response resp exit end if @detail out = [ [@pastel.bold('Path'), @pastel.bold('Size'), @pastel.bold('Updated')] ] resp[:files].each do |file| out.push([ @pastel.send(file[:is_directory] ? :blue : :green).bold(file[:path]), file[:size] || '', Time.parse(file[:updated_at]).localtime ]) end puts TTY::Table.new(out).to_s exit end resp[:files].each do |file| puts @pastel.send(file[:is_directory] ? :blue : :green).bold(file[:path]) end end |
#run ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/neocities/cli.rb', line 35 def run if @argv[0] == 'version' puts Neocities::VERSION exit end display_help_and_exit if @subcmd.nil? || @argv.include?(HELP_SUBCOMMANDS) || !SUBCOMMANDS.include?(@subcmd) send "display_#{@subcmd}_help_and_exit" if @subargs.empty? begin @api_key = File.read API_KEY_PATH rescue Errno::ENOENT @api_key = nil end if @api_key.nil? puts "Please login to get your API key:" if !@sitename && !@password @sitename = @prompt.ask('sitename:', default: ENV['NEOCITIES_SITENAME']) @password = @prompt.mask('password:', default: ENV['NEOCITIES_PASSWORD']) end @client = Neocities::Client.new sitename: @sitename, password: @password res = @client.key if res[:api_key] FileUtils.mkdir_p Pathname(API_KEY_PATH).dirname File.write API_KEY_PATH, res[:api_key] puts "The api key for #{@pastel.bold @sitename} has been stored in #{@pastel.bold API_KEY_PATH}." else display_response resp exit end else @client = Neocities::Client.new api_key: @api_key end send @subcmd end |
#sync ⇒ Object
140 141 142 143 144 145 146 147 148 149 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 |
# File 'lib/neocities/cli.rb', line 140 def sync @no_gitignore = false @excluded_files = [] loop { case @subargs[0] when '--no-gitignore' then @subargs.shift; @no_gitignore = true when '-e' then @subargs.shift; @excluded_files.push(@subargs.shift) when /^-/ then puts(@pastel.red.bold("Unknown option: #{@subargs[0].inspect}")); display_sync_help_and_exit else break end } root_path = Pathname @subargs[0] if !root_path.exist? display_response result: 'error', message: "path #{root_path} does not exist" display_sync_help_and_exit end if !root_path.directory? display_response result: 'error', message: 'provided path is not a directory' display_sync_help_and_exit end Dir.chdir(root_path) do paths = Dir.glob(File.join('**', '*')) if @no_gitignore == false begin ignore = Buff::Ignore::IgnoreFile.new '.gitignore' ignore.apply! paths puts "Not syncing .gitignore entries (--no-gitignore to disable)" rescue Buff::Ignore::IgnoreFileNotFound end end paths.select! {|p| !@excluded_files.include?(p)} paths.collect! {|path| Pathname path} paths.each do |path| next if path.directory? print @pastel.bold("Syncing #{path} ... ") resp = @client.upload path, path if resp[:result] == 'success' print @pastel.green.bold("SUCCESS") + "\n" else print "\n" display_response resp end end end end |
#upload ⇒ Object
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 |
# File 'lib/neocities/cli.rb', line 195 def upload display_upload_help_and_exit if @subargs.empty? @dir = '' loop { case @subargs[0] when '-d' then @subargs.shift; @dir = @subargs.shift when /^-/ then puts(@pastel.red.bold("Unknown option: #{@subargs[0].inspect}")); display_upload_help_and_exit else break end } @subargs.each do |path| path = Pathname path if !path.exist? display_response result: 'error', message: "#{path} does not exist locally." next end if path.directory? puts "#{path} is a directory, skipping (see the sync command)" next end remote_path = ['/', @dir, path.basename.to_s].join('/').gsub %r{/+}, '/' puts @pastel.bold("Uploading #{path} to #{remote_path} ...") resp = @client.upload path, remote_path display_response resp end end |