Class: Drum::CLI

Inherits:
Thor show all
Includes:
Log
Defined in:
lib/drum.rb

Overview

The command line interface for drum.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#log

Constructor Details

#initialize(*args) ⇒ CLI

Sets up the CLI by registering the services.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/drum.rb', line 24

def initialize(*args)
  super

  @hl = HighLine.new

  # Set up directory for persisted state
  @dot_dir = Pathname.new(Dir.home) / '.local' / 'state' / 'drum'
  @dot_dir.mkdir unless @dot_dir.directory?

  @cache_dir = @dot_dir / 'cache'
  @cache_dir.mkdir unless @cache_dir.directory?

  # Declare services in descending order of parse priority
  @services = [
    MockService.new,
    MusicService.new,
    StdioService.new,
    AppleMusicService.new(@cache_dir),
    SpotifyService.new(@cache_dir),
    # The file service should be last since it may
    # successfully parse refs that overlap with other
    # services.
    FileService.new
  ].map { |s| [s.name, s] }.to_h
end

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/drum.rb', line 50

def self.exit_on_failure?
  true
end

Instance Method Details

#__print_versionvoid

This method returns an undefined value.

Prints the version.



256
257
258
# File 'lib/drum.rb', line 256

def __print_version
  log.all VERSION
end

#cp(raw_src_ref, raw_dest_ref) ⇒ void

This method returns an undefined value.

Copies a playlist from the source to the given destination.

Parameters:

  • raw_src_ref (String)

    The source playlist ref.

  • raw_dest_ref (String)

    The destination playlist ref.



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
194
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
# File 'lib/drum.rb', line 152

def cp(raw_src_ref, raw_dest_ref)
  src_ref = self.parse_ref(raw_src_ref)
  dest_ref = self.parse_ref(raw_dest_ref)

  if src_ref.nil?
    raise "Could not parse src ref: #{raw_src_ref}"
  end
  if dest_ref.nil?
    raise "Could not parse dest ref: #{raw_dest_ref}"
  end

  self.with_service(src_ref.service_name) do |src_name, src_service|
    self.with_service(dest_ref.service_name) do |dest_name, dest_service|
      log.info "Copying from #{src_name} to #{dest_name}..."

      # TODO: Should we handle merging at all or just copy (as currently done)?

      playlists = src_service.download(src_ref).lazy

      unless playlists.size.nil?
        bar = ProgressBar.new(playlists.size)

        # Redirect log output so the bar stays at the bottom
        log.output = bar.method(:puts)
      end

      # Apply transformations to the downloaded playlists.
      # Note that we use 'map' despite mutating the playlists
      # in-place to preserve laziness in the iteration.

      playlists = playlists.map do |playlist|
        bar&.increment!

        if options[:flatten]
          playlist.path = []
        end

        if options[:group_by_author]
          author_name = playlist.author_id.try { |id| playlist.users[id] }&.display_name || 'Other'
          playlist.path.unshift(author_name)
        end

        if options[:recase_paths]
          casing = options[:recase_paths]
          playlist.path.map! do |n|
            case casing
            when 'kebabcase' then n.kebabcase
            when 'startcase' then n.startcase
            when 'camelcase' then n.camelcase
            when 'pascalcase' then n.pascalcase
            else raise "Casing '#{casing}' is not implemented (yet)!"
            end
          end
        end

        if options[:note_date]
          unless playlist.description.end_with?("\n") || playlist.description.empty?
            playlist.description += "\n"
          end
          playlist.description += Time.now.strftime("Pushed with Drum on %Y-%m-%d")
        end

        playlist
      end

      dest_service.upload(dest_ref, playlists)
    end
  end
end

#rm(raw_ref) ⇒ void

This method returns an undefined value.

Removes a playlist from the corresponding service.

Parameters:

  • raw_ref (String)

    The playlist ref.



228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/drum.rb', line 228

def rm(raw_ref)
  ref = self.parse_ref(raw_ref)

  if ref.nil?
    raise "Could not parse ref: #{raw_ref}"
  end

  self.with_service(ref.service_name) do |name, service|
    log.info "Removing from #{name}..."
    service.remove(ref)
  end
end

#servicesvoid

This method returns an undefined value.

Lists available services.



246
247
248
# File 'lib/drum.rb', line 246

def services
  log.info @services.each_key.to_a.join("\n")
end

#show(raw_ref) ⇒ Object

Previews a playlist in a simplified format.

Parameters:

  • raw_ref (String)

    The (raw) playlist ref.



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/drum.rb', line 102

def show(raw_ref)
  ref = self.parse_ref(raw_ref)

  if ref.nil?
    raise "Could not parse ref: #{raw_ref}"
  end

  self.with_service(ref.service_name) do |name, service|
    playlists = service.download(ref)
    
    playlists.each do |playlist|
      log.all({
        'name' => playlist.name,
        'description' => playlist&.description,
        'tracks' => playlist.tracks.each_with_index.map do |track, i|
          artists = (track.artist_ids&.filter_map { |id| playlist.artists[id]&.name } || []).join(', ')
          "#{i + 1}. #{artists} - #{track.name}"
        end
      }.compact.to_yaml)
    end
  end
end