| 1 | use v5.20; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use experimental qw( signatures lexical_subs postderef ); | ||
| 5 | |||
| 6 | package App::Chit::Command::init; | ||
| 7 | |||
| 8 | use App::Chit -command; | ||
| 9 | |||
| 10 | use App::Chit::Util (); | ||
| 11 | use Cwd qw( getcwd ); | ||
| 12 | use Path::Tiny qw( path ); | ||
| 13 | |||
| 14 | our $AUTHORITY = 'cpan:TOBYINK'; | ||
| 15 | our $VERSION = '0.001001'; | ||
| 16 | |||
| 17 | sub abstract { | ||
| 18 | return "Initialize chit in the current working directory." | ||
| 19 | } | ||
| 20 | |||
| 21 | sub opt_spec { | ||
| 22 | return ( | ||
| 23 | [ "clean|c", "use a clean slate" ], | ||
| 24 | [ "autofork|a", "fork existing chat from up level directory" ], | ||
| 25 | [ "fork|f=s", "fork existing chat from specified directory" ], | ||
| 26 | [ "overwrite", "overwrite current configuration" ], | ||
| 27 | ); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub validate_args ( $self, $opt, $args ) { | ||
| 31 | exists $opt->{clean} | ||
| 32 | or exists $opt->{autofork} | ||
| 33 | or exists $opt->{fork} | ||
| 34 | or $self->usage_error( "need one of --clean, --autofork, or --fork" ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub execute ( $self, $opt, $args ) { | ||
| 38 | my $dir = path( getcwd ); | ||
| 39 | |||
| 40 | if ( App::Chit::Util::is_chit_dir( $dir ) ) { | ||
| 41 | if ( $opt->{overwrite} ) { | ||
| 42 | $dir->child( App::Chit::Util::CHIT_FILENAME )->remove; | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | $self->usage_error("already initialized, please use --overwrite"); | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | my $chit; | ||
| 50 | if ( $opt->{clean} ) { | ||
| 51 | $chit = {}; | ||
| 52 | } | ||
| 53 | elsif ( $opt->{autofork} ) { | ||
| 54 | $chit = App::Chit::Util::load_chit( App::Chit::Util::find_chit_dir() ); | ||
| 55 | } | ||
| 56 | elsif ( $opt->{fork} ) { | ||
| 57 | $chit = App::Chit::Util::load_chit( App::Chit::Util::find_chit_dir( $opt->{fork} ) ); | ||
| 58 | } | ||
| 59 | App::Chit::Util::save_chit( $dir, $chit ); | ||
| 60 | |||
| 61 | say "ok"; | ||
| 62 | } | ||
| 63 | |||
| 64 | 1; |