1package App::SD::CLI::NewReplicaCommand;
2use Any::Moose 'Role';
3
4# steal email from $ENV{EMAIL} or prompt, and prompt to edit settings
5sub new_replica_wizard {
6    my $self = shift;
7    my %args = (
8        edit_settings => 1,
9        @_,
10    );
11
12    # VCS wrappers themselves should take care of settings email addresses on
13    # init/clone from VCS configuration, don't put that here
14
15    # non-interactive option is useful for testing and scriptability
16    unless ( $self->has_arg('non-interactive') ) {
17        # don't prompt for configuration if there's already a user-wide email set
18        if ( ! defined $self->config->get( key => 'user.email-address' ) ) {
19
20            print "\nYou need an email address configured to use SD. I'll try"
21                ." to find one.\n";
22
23            if ( $ENV{PROPHET_EMAIL} ) {
24                $self->_migrate_email_from_env( 'PROPHET_EMAIL' );
25            }
26        }
27        if ( ! defined $self->config->get( key => 'user.email-address' ) ) {
28            if ( $ENV{EMAIL} ) {
29                $self->_migrate_email_from_env( 'EMAIL' );
30            }
31        }
32        # if we still don't have an email, ask
33        if ( ! defined $self->config->get( key => 'user.email-address' ) ) {
34            $self->_prompt_email;
35        }
36
37        # new replicas probably want to change settings right away,
38        # at least to change the project name ;)
39        $self->_prompt_edit_settings if $args{edit_settings};
40    }
41
42    # this message won't print if the user has a ~/.sdrc, which is
43    # probably a pretty good indication that they're not new
44    my $script = $self->cli->get_script_name;
45    print <<"END_MSG" unless -f $self->config->user_file;
46
47If you're new to SD, you can find out what to do now by looking at
48'${script}help intro' and '${script}help tickets'. You can see a list of all
49help topics with '${script}help'. Have fun!
50END_MSG
51}
52
53# default is the replica-specific config file
54sub _prompt_which_config_file {
55    my $self = shift;
56    my $email = shift;
57
58    print "\nUse '$email' for (a)ll your bug databases, (j)ust"
59            ." this one,\nor (n)ot at all? [a/J/n] ";
60    chomp( my $response = <STDIN> );
61
62    my $config_file = lc $response eq 'a'
63        ? $self->config->user_file
64        : lc $response eq 'n'
65        ? undef
66        : $self->config->replica_config_file;
67
68    return $config_file;
69}
70
71sub _migrate_email_from_env {
72    my $self = shift;
73    my $var = shift;
74
75    print "Found '$ENV{$var}' in \$$var.\n";
76    my $config_file = $self->_prompt_which_config_file( $ENV{$var} );
77
78    if ( $config_file ) {
79        $self->config->set(
80            key      => 'user.email-address',
81            value    => $ENV{$var},
82            filename => $config_file,
83        );
84        print "  - added email '$ENV{$var}' to\n    $config_file\n";
85    }
86}
87
88sub _prompt_email {
89    my $self = shift;
90
91    Prophet::CLI->end_pager(); # XXX where does this get turned back on?
92    print "\nCouldn't determine an email address to attribute your SD changes to.\n";
93
94    my $email;
95    while ( ! $email ) {
96        print "What email shall I use? ";
97        chomp( $email = <STDIN> );
98    }
99
100    my $use_dir_config = $self->prompt_choices( 'j', 'a',
101        'Use this for (a)ll your SD databases or (j)ust this one?' );
102
103    my $config_file = $use_dir_config
104                    ? $self->config->replica_config_file
105                    : $self->config->user_file;
106    $self->config->set(
107        key      => 'user.email-address',
108        value    => $email,
109        filename => $config_file,
110    );
111    print "  - added email '$email' to\n    $config_file\n";
112}
113
114sub _prompt_edit_settings {
115    my $self = shift;
116
117    my $prompt_for_settings
118        = $self->prompt_Yn(
119            "\nWant to edit your new bug database's settings now?" );
120    if ( $prompt_for_settings ) {
121        my @classes = App::SD::CLI::Dispatcher->class_names('Settings');
122        for my $class (@classes) {
123            $self->app_handle->try_to_require($class) or next;
124
125            # reset args for new command
126            my $args = {
127                edit => 1,
128            };
129            $self->context->mutate_attributes( args => $args );
130
131            my $command = $class->new(
132                uuid    => $self->context->uuid,
133                cli     => $self->cli,
134                context => $self->context,
135            );
136            $command->run();
137        }
138
139    }
140}
141
142no Any::Moose;
143
1441;
145
146