Class: NeocitiesRed::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.app_config_path(name) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/neocities_red/cli.rb', line 175

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) ⇒ Object



45
46
47
48
49
50
# File 'lib/neocities_red/cli.rb', line 45

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).remove }
end

#diff(path = ".") ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/neocities_red/cli.rb', line 26

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

  client = ensure_client!
  exclude = build_diff_exclusions(path, Array(options[:exclude]))

  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) ⇒ Object



215
216
217
218
219
220
221
222
# File 'lib/neocities_red/cli.rb', line 215

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) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/neocities_red/cli.rb', line 64

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) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/neocities_red/cli.rb', line 78

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]).show
end

#logoutObject



55
56
57
58
59
60
# File 'lib/neocities_red/cli.rb', line 55

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

#pizzaObject



171
172
173
# File 'lib/neocities_red/cli.rb', line 171

def pizza
  display_help_for(__method__)
end

#pullObject



138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/neocities_red/cli.rb', line 138

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)
                          .export(quiet: options[:quiet], last_pull_time: last_pull_time, last_pull_loc: last_pull_loc)
end

#purgeObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/neocities_red/cli.rb', line 153

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

  client = ensure_client!
  resp = client.list
  resp[:files].each do |file|
    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"
      display.display_delete_success
    else
      display.display_delete_error(delete_resp)
    end
  end
end

#push(root = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/neocities_red/cli.rb', line 97

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) ⇒ Object



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

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? || remote_path.nil?

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

#versionObject



225
226
227
# File 'lib/neocities_red/cli.rb', line 225

def version
  display.say NeocitiesRed::VERSION
end