Class: NeocitiesRed::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/neocities_red/cli.rb

Overview

Thor-based command-line interface for the NeocitiesRed gem.

Provides subcommands for managing a Neocities site: push, upload, delete, diff, list, info, pull, purge, logout, and pizza.

Authentication is handled lazily — the first command that requires an API connection will prompt for credentials (or read from config/env).

Examples:

Running from shell

$ neocities-red push .
$ neocities-red list -a
$ neocities-red diff --ignore-dotfiles

See Also:

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.app_config_path(name) ⇒ String?

Returns the platform-specific application config directory path.

Parameters:

  • name (String)

    application name (e.g. "neocities")

Returns:

  • (String, nil)

    full path to the config directory, or nil if the platform cannot be determined



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/neocities_red/cli.rb', line 278

def self.app_config_path(name)
  platform = case RUBY_PLATFORM
             when /cygwin|mswin|mingw|bccwin|wince|emx|win32/
               :windows
             when /darwin/
               :darwin
             when /linux/
               :linux
             when /freebsd/
               :freebsd
             else
               :unknown
             end

  case platform
  when :linux
    return File.join(ENV["XDG_CONFIG_HOME"], name) if ENV["XDG_CONFIG_HOME"]
    return File.join(Dir.home, ".config", name) if Dir.home
  when :darwin
    return File.join(Dir.home, "Library", "Application Support", name) if Dir.home
  when :windows
    return File.join(ENV["LOCALAPPDATA"], name) if ENV["LOCALAPPDATA"]

    if ENV["USERPROFILE"]
      return File.join(
        ENV["USERPROFILE"],
        "AppData",
        "Local",
        name
      )
    end
  else
    # FreeBSD and other unknown UNIX-like systems use dotfile directly in home directory
    return File.join(Dir.home, ".#{name}") if Dir.home
  end

  nil
end

Instance Method Details

#delete(*paths) ⇒ void

This method returns an undefined value.

Deletes one or more files from the remote Neocities site.

Parameters:

  • paths (Array<String>)

    remote file paths to delete



71
72
73
74
75
76
# File 'lib/neocities_red/cli.rb', line 71

def delete(*paths)
  return display_help_for("delete") if paths.empty? || help_requested?(options[:help], paths)

  client = ensure_client!
  paths.each { |path| Services::File::Remover.new(client, path, display: display).remove }
end

#diff(path = ".") ⇒ void

This method returns an undefined value.

Compares local files with the remote Neocities site and displays added, modified, and removed files.

Parameters:

  • path (String) (defaults to: ".")

    local directory path to compare (defaults to current directory)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/neocities_red/cli.rb', line 47

def diff(path = ".")
  return display_help_for("diff") if help_requested?(options[:help], path)

  client = ensure_client!
  exclude = Services::Common::Exclusions.build(Array(options[:exclude]), base_path: path)

  added, modified, removed = Services::Site::Differencer.new(
    client,
    path: path,
    detail: false,
    ignore_dotfiles: options[:ignore_dotfiles],
    exclude: exclude
  ).show

  display.display_diff_results(added: added, modified: modified, removed: removed)
end

#help(command = nil) ⇒ void

This method returns an undefined value.

Displays help for a specific command or the general help screen.

Parameters:

  • command (String, nil) (defaults to: nil)

    command name to show help for; nil displays the main help screen



324
325
326
327
328
329
330
331
# File 'lib/neocities_red/cli.rb', line 324

def help(command = nil)
  return display.display_help_and_exit if command.nil?

  custom_help_method = "display_#{command}_help_and_exit"
  return display.public_send(custom_help_method) if display.respond_to?(custom_help_method)

  super
end

#info(sitename = nil) ⇒ void

This method returns an undefined value.

Displays information and statistics for a Neocities site.

Parameters:

  • sitename (String, nil) (defaults to: nil)

    site name to query; defaults to the currently authenticated site when omitted

Raises:



103
104
105
106
107
108
109
110
111
# File 'lib/neocities_red/cli.rb', line 103

def info(sitename = nil)
  return display_help_for("info") if help_requested?(options[:help], sitename)

  client = ensure_client!
  profile_info = Services::Site::Informer.new(client, [sitename].compact, @sitename).pretty_print
  display.say TTY::Table.new(profile_info)
rescue StandardError => e
  display.display_response(e)
end

#list(path = nil) ⇒ void

This method returns an undefined value.

Lists files on the remote Neocities site.

Parameters:

  • path (String, nil) (defaults to: nil)

    remote directory path to list (nil for root, or when --all is used)

Raises:



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/neocities_red/cli.rb', line 124

def list(path = nil)
  if help_requested?(options[:help], path) || (path.nil? && options[:all].nil? && options[:detail].nil?)
    display_help_for("list")
    return
  end

  client = ensure_client!
  path = nil if options[:all]
  display.say Services::File::List.new(client, path, options[:detail], display: display).show
rescue NeocitiesRed::APIError => e
  display.display_response(e)
end

#logoutvoid

This method returns an undefined value.

Removes the stored API key from the local config file.

Requires the --yes / -y flag to confirm the action.



87
88
89
90
91
92
# File 'lib/neocities_red/cli.rb', line 87

def logout
  return display_help_for("logout") if help_requested?(options[:help]) || !options[:yes]

  FileUtils.rm_f(app_config_path)
  display.display_logout_success
end

#pizzavoid

This method returns an undefined value.

Easter egg — displays a humorous pizza-related excuse.



269
270
271
# File 'lib/neocities_red/cli.rb', line 269

def pizza
  display_help_for(__method__)
end

#pullvoid

This method returns an undefined value.

Downloads the latest version of site files from the remote Neocities site.

Skips files that haven't changed since the last pull (based on stored timestamp and working directory). Use --quiet to suppress per-file output and show a spinner instead.

Raises:

  • (StandardError)

    on network or API errors



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/neocities_red/cli.rb', line 217

def pull
  return display_help_for("pull") if help_requested?(options[:help])

  client = ensure_client!
  data = read_config || {}

  last_pull_time = data.dig("LAST_PULL", "time")
  last_pull_loc = data.dig("LAST_PULL", "loc")

  Services::Site::Exporter.new(client, @sitename, data, app_config_path, display: display)
                          .export(quiet: options[:quiet], last_pull_time: last_pull_time, last_pull_loc: last_pull_loc)
rescue StandardError => e
  display.display_response(e)
end

#purgevoid

This method returns an undefined value.

Deletes all files from the Neocities site.

Requires the --yes / -y flag to confirm the destructive action. Use --dry-run to preview what would be deleted without changes.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/neocities_red/cli.rb', line 242

def purge
  return display_help_for("purge") unless options[:yes]

  client = ensure_client!
  display.display_dry_run_notice if options[:dry_run]
  resp = client.list
  deleted_dirs = []
  resp[:files].sort_by { |f| f[:is_directory] ? 0 : 1 }.each do |file|
    next if deleted_dirs.any? { |dir| file[:path].start_with?(dir) }

    display.display_delete_progress(file[:path])
    delete_resp = client.delete_wrapper_with_dry_run(file[:path], dry_run: options[:dry_run])

    if delete_resp[:result] == "success"
      deleted_dirs << file[:path] if file[:is_directory]
      display.display_delete_success
    else
      display.display_delete_error(delete_resp)
    end
  end
end

#push(root = nil) ⇒ void

This method returns an undefined value.

Recursively uploads a local directory to the Neocities site.

Supports --no-gitignore to ignore .gitignore rules, --ignore-dotfiles to skip dot-prefixed files, --exclude to skip specific paths, --dry-run to preview changes, --prune to delete remote files not present locally, and --optimized to skip files whose SHA1 hash matches the server.

Parameters:

  • root (String, nil) (defaults to: nil)

    local directory path to upload

Raises:

  • (ArgumentError)

    if the path does not exist or is not a directory



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/neocities_red/cli.rb', line 157

def push(root = nil)
  return display_help_for("push") if help_requested?(options[:help], root)

  client = ensure_client!
  return display_help_for("push") if root.nil?

  Services::Site::Pusher.new(
    client,
    display,
    root: root,
    no_gitignore: options[:no_gitignore],
    ignore_dotfiles: options[:ignore_dotfiles],
    exclude: Array(options[:exclude]),
    dry_run: options[:dry_run],
    prune: options[:prune],
    optimized: options[:optimized]
  ).push
rescue ArgumentError => e
  display.display_response(result: "error", message: e.message)
  display_help_for("push")
end

#upload(local_path = nil, remote_path = nil) ⇒ void

This method returns an undefined value.

Uploads a single file or an entire folder to the Neocities site.

When LOCAL_PATH is a file, uploads it directly. When it is a directory, uploads all files within it in parallel.

Parameters:

  • local_path (String, nil) (defaults to: nil)

    local file or directory path

  • remote_path (String, nil) (defaults to: nil)

    remote destination; defaults to the basename of local_path



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/neocities_red/cli.rb', line 190

def upload(local_path = nil, remote_path = nil)
  return display_help_for("upload") if help_requested?(options[:help], [local_path, remote_path])
  return display_help_for("upload") if local_path.nil?

  client = ensure_client!
  dest = remote_path || File.basename(local_path)
  if File.file?(local_path)
    Services::File::Uploader.new(client, local_path, dest, display: display).upload
  elsif File.directory?(local_path)
    folder_uploader = Services::File::FolderUploader.new(client, local_path, dest, display: display)
    files_list = folder_uploader.files
    folder_uploader.upload(files_list)
  end
end

#versionvoid

This method returns an undefined value.

Prints the current gem version to stdout.



338
339
340
# File 'lib/neocities_red/cli.rb', line 338

def version
  display.say NeocitiesRed::VERSION
end