added support for export flag
[migration-tools.git] / mig-bin / mig-env
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 mig-env - This tool is for tracking and setting environment variables used by
8 B<mig> and its sub-tools.
9
10 =head1 SYNOPSIS
11
12 B<mig-env> <create|use> <migration_schema>
13
14 B<mig-env> <show> [migration_schema]
15
16 B<mig-env> <list>
17
18 B<mig-env> <help>
19
20 =head1 DESCRIPTION
21
22 For most invocations, B<mig-env> will either create or use a migration-specific
23 file (~/.mig/<migration_schema>.env) for setting the following environment
24 variables:
25
26 =over 15
27
28 =item MIGSCHEMA
29
30 The name of the migration schema.  Convention has this being a single lowercased
31 word or acronym identifying the library, prefixed with 'm_'.
32
33 =item MIGWORKDIR
34
35 The base working directory for containing migration data, scripts, and other
36 files.
37
38 =item PGHOST
39
40 The IP address or hostname for the PostgreSQL database used for a migration.
41
42 =item PGPORT
43
44 The TCP port for the PostgreSQL database.
45
46 =item PGUSER
47
48 The PostgreSQL user to use for the database.
49
50 =item PGDATABASE
51
52 The name of the actual database containing the migration schema.
53
54 =back
55
56 This script may also setup a symlink from a specified Git repository to a
57 scripts/ directory within the migration work directory.  The default for this is
58 ~/git/migration-work/past_migrations/MIGSCHEMA --> MIGWORKDIR/scripts
59
60 It may also create the migration work directory if necessary.
61
62 =head1 COMMANDS
63
64 =over 15
65
66 =item B<create> <schema>
67
68 This invocation will prompt for various values and create a .env file for the
69 specified migration schema, and a symlink between the specified Git repository
70 and migration work directory (which will also be created if needed).
71
72 =item B<use> <schema>
73
74 This command will spawn a bash shell that executes the corresponding
75 ~/.mig/<schema>.env script for setting up environment variables encoded during
76 B<create>.
77
78 =item B<show> [schema]
79
80 This command will show the contents of the corresponding ~/.mig/<schema>.env
81 script, or, if no schema is specified, then it will list pertinent variables in
82 the current environment if they exist.
83
84 =item B<list>
85
86 This command will list migration schemas found in ~/.mig
87
88 =item B<help>
89
90 Display the documentation you're reading now.
91
92 =back
93
94 =cut
95
96 ###############################################################################
97
98 use strict;
99 use 5.012;
100 use Switch;
101 use Env qw(
102     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
103     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
104 );
105 use Pod::Usage;
106 use File::Path qw(make_path);
107 use FindBin;
108 my $mig_bin = "$FindBin::Bin/";
109 use lib "$FindBin::Bin/";
110
111 pod2usage(-verbose => 2) if ! $ARGV[0];
112
113 my $migration_schema = $ARGV[1] || '';
114 my $filename = "$HOME/.mig/$migration_schema.env";
115 switch($ARGV[0]) {
116     case "--help" {
117         pod2usage(-verbose => 2);
118     }
119     case "help" {
120         pod2usage(-verbose => 2);
121     }
122     case "create" {
123         pod2usage(-verbose => 1) if ! $ARGV[1];
124         mig_env_create();
125     }
126     case "use" {
127         pod2usage(-verbose => 1) if ! $ARGV[1];
128         if (-e $filename) {
129             exec '/bin/bash', '--init-file', $filename;
130         } else {
131             die "\n$filename does not exist\n";
132         }
133     }
134     case "show" {
135         if (-e $filename) {
136             exec '/bin/cat', $filename;
137         } else {
138             print `env | sort | egrep 'MIG|PG'`;
139         }
140     }
141     case "list" {
142         opendir(my $dh, "$HOME/.mig") || die "can't open $HOME/.mig: $!";
143         while (readdir $dh) {
144             if (/^(.*)\.env$/) {
145                 print "$1\n";
146             }
147         }
148         closedir $dh;
149     }
150     else {
151         pod2usage(1);
152     }
153 }
154
155 sub mig_env_create {
156     if (-e $filename) {
157         print "Re-Creating $filename\n";
158         print `cat $filename`;
159     } else {
160         print "Creating $filename\n";
161     }
162     print "\n";
163
164     # directories
165
166     $MIGBASEWORKDIR = "$HOME/data/" unless $MIGBASEWORKDIR;
167     my $migworkdir_default = "$MIGBASEWORKDIR$migration_schema/";
168     print "Main work directory (default $migworkdir_default): ";
169     my $MIGWORKDIR = <STDIN>;
170     chomp $MIGWORKDIR;
171     if (! $MIGWORKDIR) {
172         $MIGWORKDIR = $migworkdir_default;
173     }
174     $MIGBASEGITDIR = "$HOME/git/migration-work/" unless $MIGBASEGITDIR;
175     my $miggitdir_default = "${MIGBASEGITDIR}past_migrations/$migration_schema/";
176     print "git repo for migration-specific scripts (default $miggitdir_default): ";
177     my $MIGGITDIR = <STDIN>;
178     chomp $MIGGITDIR;
179     if (! $MIGGITDIR) {
180         $MIGGITDIR = $miggitdir_default;
181     }
182
183     # PostgreSQL
184
185     $PGHOST = 'localhost' unless $PGHOST;
186     my $pghost_default = $PGHOST;
187     print "PGHOST (default $pghost_default): ";
188     $PGHOST = <STDIN>;
189     chomp $PGHOST;
190     if (! $PGHOST) {
191         $PGHOST = $pghost_default;
192     }
193     $PGPORT = 5432 unless $PGPORT;
194     my $pgport_default = $PGPORT;
195     print "PGPORT (default $pgport_default): ";
196     $PGPORT = <STDIN>;
197     chomp $PGPORT;
198     if (! $PGPORT) {
199         $PGPORT = $pgport_default;
200     }
201     $PGDATABASE = 'evergreen' unless $PGDATABASE;
202     my $pgdatabase_default = $PGDATABASE;
203     print "PGDATABASE (default $pgdatabase_default): ";
204     $PGDATABASE = <STDIN>;
205     chomp $PGDATABASE;
206     if (! $PGDATABASE) {
207         $PGDATABASE = $pgdatabase_default;
208     }
209     $PGUSER = $PGDATABASE unless $PGUSER;
210     my $pguser_default = $PGUSER;
211     print "PGUSER (default $pguser_default): ";
212     my $PGUSER = <STDIN>;
213     chomp $PGUSER;
214     if (! $PGUSER) {
215         $PGUSER = $pguser_default;
216     }
217
218     # create files and directories if needed
219
220     mkdir "$HOME/.mig";
221     make_path($MIGGITDIR, { verbose => 1 });
222     `touch $MIGGITDIR/README`;
223     make_path($MIGWORKDIR, { verbose => 1 });
224     symlink $MIGGITDIR, "$MIGWORKDIR/scripts";
225     open FILE, ">$filename";
226     print FILE "export PGHOST=$PGHOST\n";
227     print FILE "export PGPORT=$PGPORT\n";
228     print FILE "export PGDATABASE=$PGDATABASE\n";
229     print FILE "export PGUSER=$PGUSER\n";
230     print FILE "export PGOPTIONS='-c search_path=$migration_schema,public,evergreen'\n";
231     print FILE "export MIGENVPROMPT=$migration_schema\n";
232     print FILE "export MIGSCHEMA=$migration_schema\n";
233     print FILE "export MIGBASEWORKDIR=$MIGBASEWORKDIR\n";
234     print FILE "export MIGWORKDIR=$MIGWORKDIR\n";
235     print FILE "export MIGBASEGITDIR=$MIGBASEGITDIR\n";
236     print FILE "export MIGGITDIR=$MIGGITDIR\n";
237     print FILE "alias wcd='cd `mig wdir`'\n";
238     print FILE "alias gcd='cd `mig gdir`'\n";
239     print FILE "alias scd='cd `mig sdir`'\n";
240     print FILE "source ~/.profile\n";
241     print FILE "env | sort | egrep 'PG|MIG'\n";
242     print FILE 'echo shell PID = $$' . "\n";
243     close FILE;
244 }
245