| 1 | package Yakuake::Sessions::TraitFor::FileData; | ||
| 2 | |||
| 3 | use namespace::autoclean; | ||
| 4 | |||
| 5 | use Class::Usul::Constants qw( EXCEPTION_CLASS FALSE OK SPC TRUE ); | ||
| 6 | use Class::Usul::Functions qw( io throw ); | ||
| 7 | use File::DataClass::Types qw( Bool ); | ||
| 8 | use Moo::Role; | ||
| 9 | use Class::Usul::Options; | ||
| 10 | |||
| 11 | requires qw( add_leader apply_sessions config debug debug_flag dumper file | ||
| 12 | get_sessions_from_yakuake loc options next_argv | ||
| 13 | profile_path run_cmd storage_class yorn ); | ||
| 14 | |||
| 15 | # Public attributes | ||
| 16 | option 'force' => is => 'ro', isa => Bool, default => FALSE, | ||
| 17 | documentation => 'Overwrite the output file if it already exists', | ||
| 18 | short => 'f'; | ||
| 19 | |||
| 20 | # Public methods | ||
| 21 | sub dump : method { | ||
| 22 | my $self = shift; my $path = $self->next_argv; | ||
| 23 | |||
| 24 | my $session_tabs = $self->get_sessions_from_yakuake; | ||
| 25 | |||
| 26 | ($self->debug or not $path) and $self->dumper( $session_tabs ); | ||
| 27 | $path or return OK; $path = io $path; | ||
| 28 | |||
| 29 | my $prompt; $path->exists and $path->is_file and not $self->force | ||
| 30 | and $prompt = $self->loc( 'Specified file exists, overwrite?' ) | ||
| 31 | and not $self->yorn( $self->add_leader( $prompt ), FALSE, FALSE ) | ||
| 32 | and return OK; | ||
| 33 | |||
| 34 | $self->file->data_dump( data => { sessions => $session_tabs }, | ||
| 35 | path => $path, | ||
| 36 | storage_class => $self->storage_class ); | ||
| 37 | return OK; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub load : method { | ||
| 41 | my ($self, $data_only) = @_; my $path = $self->profile_path; | ||
| 42 | |||
| 43 | $path->exists and $path->is_file | ||
| 44 | or throw error => 'Path [_1] does not exist or is not a file', | ||
| 45 | args => [ $path ]; | ||
| 46 | |||
| 47 | $data_only and return $self->_get_sessions_from_file; | ||
| 48 | |||
| 49 | if ($self->options->{detached}) { | ||
| 50 | $self->apply_sessions( $self->_get_sessions_from_file ); return OK; | ||
| 51 | } | ||
| 52 | |||
| 53 | my $cmd = 'nohup '.$self->config->pathname.SPC.$self->debug_flag; | ||
| 54 | $cmd .= " -o detached=1 load ${path}"; | ||
| 55 | my $out = $self->config->tempdir->catfile( 'load_session.out' ); | ||
| 56 | |||
| 57 | $self->run_cmd( $cmd, { async => TRUE, out => $out, err => 'out', } ); | ||
| 58 | return OK; | ||
| 59 | } | ||
| 60 | |||
| 61 | # Private methods | ||
| 62 | sub _get_sessions_from_file { | ||
| 63 | my $self = shift; my $path = $self->profile_path; | ||
| 64 | |||
| 65 | my $tabs = $self->file->data_load | ||
| 66 | ( paths => [ $path ], storage_class => $self->storage_class )->{sessions}; | ||
| 67 | |||
| 68 | $tabs->[ 0 ] or throw 'No session tabs info found'; | ||
| 69 | |||
| 70 | return $tabs; | ||
| 71 | } | ||
| 72 | |||
| 73 | 1; | ||
| 74 | |||
| 75 | __END__ |