| 1 | package Jifty::Plugin::Config::Action::Config; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use base qw/Jifty::Action/; | ||
| 6 | use UNIVERSAL::require; | ||
| 7 | use Jifty::YAML; | ||
| 8 | use File::Spec; | ||
| 9 | use Scalar::Defer; | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Jifty::Plugin::Config::Action::Config - Register config | ||
| 14 | |||
| 15 | =head1 METHODS | ||
| 16 | |||
| 17 | =head2 arguments | ||
| 18 | |||
| 19 | Provides a single argument, C<config>, which is a textarea with | ||
| 20 | Jifty's L<YAML> configuration in it. | ||
| 21 | |||
| 22 | =cut | ||
| 23 | |||
| 24 | sub arguments { | ||
| 25 | my $self = shift; | ||
| 26 | return $self->{__cached_arguments} if ( $self->{__cached_arguments} ); | ||
| 27 | my $args = { | ||
| 28 | 'config' => { | ||
| 29 | label => '', # don't show label | ||
| 30 | render_as => 'Textarea', | ||
| 31 | rows => 60, | ||
| 32 | default_value => defer { | ||
| 33 | local $/; | ||
| 34 | open my $fh, '<', Jifty::Util->app_root . '/etc/config.yml'; | ||
| 35 | return <$fh>; | ||
| 36 | } | ||
| 37 | }, | ||
| 38 | }; | ||
| 39 | |||
| 40 | return $self->{__cached_arguments} = $args; | ||
| 41 | } | ||
| 42 | |||
| 43 | =head2 take_action | ||
| 44 | |||
| 45 | Attempts to update the application's F<etc/config.yml> file with the | ||
| 46 | new configuration. | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub take_action { | ||
| 51 | my $self = shift; | ||
| 52 | |||
| 53 | if ( $self->has_argument('config') ) { | ||
| 54 | my $new_config = $self->argument_value( 'config' ); | ||
| 55 | $new_config =~ s/\r\n/\n/g; #textarea gives us dos format | ||
| 56 | eval { Jifty::YAML::Load( $new_config ) }; | ||
| 57 | if ( $@ ) { | ||
| 58 | # invalid yaml | ||
| 59 | $self->result->message( _( "invalid yaml" ) ); | ||
| 60 | $self->result->failure(1); | ||
| 61 | return; | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | if ( open my $fh, '>', Jifty::Util->app_root . '/etc/config.yml' ) { | ||
| 65 | print $fh $new_config; | ||
| 66 | close $fh; | ||
| 67 | } | ||
| 68 | else { | ||
| 69 | $self->result->message( | ||
| 70 | _("can't write to etc/config.yml: $1") ); | ||
| 71 | $self->result->failure(1); | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | } | ||
| 76 | $self->report_success; | ||
| 77 | |||
| 78 | Jifty->config->load; | ||
| 79 | |||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 report_success | ||
| 84 | |||
| 85 | Reports that the action succeeded. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub report_success { | ||
| 90 | my $self = shift; | ||
| 91 | |||
| 92 | # Your success message here | ||
| 93 | $self->result->message('Success'); | ||
| 94 | } | ||
| 95 | |||
| 96 | 1; |