X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=blobdiff_plain;f=mig-bin%2Fmig-gsheet;h=5975bd666e1d53a3ecde4e122dc918b4764eb560;hp=a4e089c24ad68c57ae6fc0b1d0c698208d11a965;hb=45d54504cfad23fbe1c151ba57dad1c1fe3f9f17;hpb=2f34c8427944b6572c9e0c695b2f241b4f10cb3a diff --git a/mig-bin/mig-gsheet b/mig-bin/mig-gsheet index a4e089c..5975bd6 100755 --- a/mig-bin/mig-gsheet +++ b/mig-bin/mig-gsheet @@ -14,10 +14,6 @@ use lib "$FindBin::Bin/"; my $mig_bin = "$FindBin::Bin/"; use Mig; use strict; -use Env qw( - HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA - MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR - ); use Switch; use Cwd 'abs_path'; use Pod::Usage; @@ -41,6 +37,8 @@ my $sql; my $sth; my @ws; my @tracked_ws_names; +my $authfile = $ENV{HOME} . '/.mig/oauth.env'; +my $next_arg_is_authfile; foreach my $arg (@ARGV) { if ($arg eq '--push') { @@ -61,6 +59,15 @@ foreach my $arg (@ARGV) { $next_arg_is_pull = 0; next; } + if ($arg eq '--authfile') { + $next_arg_is_authfile = 1; + next; + } + if ($next_arg_is_authfile) { + $authfile = $arg; + $next_arg_is_authfile = 0; + next; + } if ($arg eq '--export') { $cmd_export = 1; next; @@ -70,8 +77,20 @@ foreach my $arg (@ARGV) { abort('must specify --push (db->worksheets) or --pull (worksheets->db)') unless (defined $cmd_push or defined $cmd_pull); if (defined $cmd_push and defined $cmd_pull) { abort('you can not specify both a --push and --pull on the same command'); } +my $clientid; +my $clientsecret; +my $sessionfile; + +open (my $fh, '<', $authfile) or abort("Could not open $authfile"); +while (my $var = <$fh>) { + chomp $var; + my ($var1, $var2) = split /=/,$var; + if ($var1 eq 'CLIENTID') { $clientid = $var2; } + if ($var1 eq 'CLIENTSECRET') { $clientsecret = $var2; } + if ($var1 eq 'SESSIONFILE') { $sessionfile = $var2; } +} my $dbh = Mig::db_connect(); -my $spreadsheet = connect_gsheet(); +my $spreadsheet = connect_gsheet($clientid,$clientsecret,$sessionfile); abort('could not connect to google sheet') unless (defined $spreadsheet); $sql = 'SELECT tab_name FROM gsheet_tracked_table;'; @@ -190,7 +209,7 @@ sub export_table { my $efile = $MIGGITDIR . $table . '_' . $date . '_' . $hms . '.tsv'; my @data; my $record_count = 0; - $dbh->do("COPY $table TO STDOUT CSV HEADER;"); + $dbh->do("COPY $table TO STDOUT CSV DELIMITER E'\t' HEADER;"); 1 while $dbh->pg_getcopydata(\$data[$record_count++]) >= 0; open (my $eout, '>', $efile) or abort("Could NOT open $efile."); foreach my $d (@data) { @@ -203,7 +222,7 @@ sub export_table { sub die_if_gsheet_tracked_table_does_not_exist { if (!check_for_gsheet_tracked_table()) { - die "Table $MIGSCHEMA.gsheet_tracked_table does not exist. Bailing...\n"; + die "Table gsheet_tracked_table does not exist. Bailing...\n"; } } @@ -370,18 +389,17 @@ sub abort { } sub connect_gsheet { - if (!defined $ENV{'CLIENTID'}) { - exec '/bin/bash', '--init-file', '~/.mig/oauth.env'; - print "Open Authentication settings were not loaded, please re-run.\n"; - } - my $session_filename = $ENV{SESSIONFILE}; + + my ($clientid,$clientsecret,$sessionfile) = @_; + my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new( - client_id => $ENV{CLIENTID}, - client_secret => $ENV{CLIENTSECRET}, + client_id => $clientid, + client_secret => $clientsecret, scope => ['http://spreadsheets.google.com/feeds/'], redirect_uri => 'https://developers.google.com/oauthplayground', ); - my $session = retrieve($session_filename); + if ($sessionfile =~ m/~/) {$sessionfile =~ s/~/$ENV{HOME}/; } + my $session = retrieve($sessionfile); my $restored_token = Net::OAuth2::AccessToken->session_thaw( $session, auto_refresh => 1,