Toby Inkster avatar Toby Inkster committed 4096109

beginnings of CLI

Comments (0)

Files changed (4)

+#!/usr/bin/env perl
+use App::SoundSwarm;
+App::SoundSwarm->run;

lib/App/SoundSwarm.pm

+package App::SoundSwarm;
+use App::Cmd::Setup -app;
+1;

lib/App/SoundSwarm/Command/enqueue.pm

+package App::SoundSwarm::Command::enqueue;
+
+use strict;
+use warnings;
+use App::SoundSwarm -command;
+
+sub abstract
+{
+	return "add tracks to the queue (by filename)";
+}
+
+sub command_names
+{
+	return qw( enqueue nq );
+}
+
+sub usage_desc
+{
+	my $class  = shift;
+	my ($name) = $class->command_names;
+	return "%c $name %o [file ...]";
+}
+
+sub opt_spec
+{
+	require SoundSwarm;
+	return (
+		[ "port"    => "TCP port to connect to (default ${\SoundSwarm::QUEUE_PORT()})" ],
+		[ "host"    => "host to connect to" ],
+		[ "stdin|i" => "read filenames from STDIN instead of command line parameters" ],
+	);
+}
+
+sub validate_args
+{
+	my ($self, $opt, $args) = @_;
+	for (@$args) {
+	}
+}
+
+sub execute
+{
+	require SoundSwarm::Client::Queue;
+	require Path::Class;
+	
+	my ($self, $opt, $args) = @_;
+	my $queue;
+	
+	my @files =
+		map { "$_" }
+		map { -d $_ ? _expand_dir($_) : Path::Class::File->new($_)->absolute }
+		(delete $opt->{stdin} ? <> : @$args);
+	
+	for (@files)
+	{
+		$self->usage_error("not a file: $_") unless -f $_;
+		($queue ||= SoundSwarm::Client::Queue->new(%$opt))->enqueue($_);
+	}
+}
+
+sub _expand_dir
+{
+	require Path::Class;
+	
+	my $dir = Path::Class::Dir->new($_);
+	my @return;
+	$dir->recurse(callback => sub
+	{
+		my $file = shift;
+		return if $file->is_dir;
+		return unless "$file" =~ /\.(ogg|mp3|mp4|m4a|3gp|flac)$/i;
+		push @return, $file->absolute;
+	});
+	return @return;
+}
+
+1;
+

lib/App/SoundSwarm/Command/queue.pm

+package App::SoundSwarm::Command::queue;
+
+use strict;
+use warnings;
+use App::SoundSwarm -command;
+
+sub abstract
+{
+	return "examine the queue";
+}
+
+sub command_names
+{
+	return qw( queue q );
+}
+
+sub usage_desc
+{
+	my $class  = shift;
+	my ($name) = $class->command_names;
+	return "%c $name %o";
+}
+
+sub opt_spec
+{
+	require SoundSwarm;
+	return (
+		[ "port"    => "TCP port to connect to (default ${\SoundSwarm::QUEUE_PORT()})" ],
+		[ "host"    => "host to connect to" ],
+	);
+}
+
+sub execute
+{
+	require SoundSwarm::Client::Queue;
+	require Path::Class;
+	
+	my ($self, $opt, $args) = @_;
+	my $queue = SoundSwarm::Client::Queue->new(%$opt);
+	
+	print "$_\n" for @{ $queue->list };
+}
+
+1;
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.