The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.
#!/usr/bin/perl
use 5.006;
use strict;
use warnings;
our $VERSION = 0.001;

use Getopt::Long;
use JSON;
use Metabase::User::Profile;
use Pod::Usage;

my (%profile, $help, $output, $full_name, $email_address, $secret);
my $result = GetOptions( 
  'help|h'      => \$help,
  'output|o:s'  => \$output,
  'name:s'      => \$full_name,
  'email:s'     => \$email_address,
  'secret:s'    => \$secret,
);

pod2usage({-verbose => 2}) if ! $result || $help;

$profile{full_name}     = $full_name      if defined $full_name;
$profile{email_address} = $email_address  if defined $email_address;
$profile{secret}        = $secret         if defined $secret;

my @prompts = (
  full_name     => 'full name',
  email_address => 'email address',
  secret        => 'password/secret',
);

$|++; # autoflush prompts
while (@prompts) {
  my ($key, $phrase) = splice(@prompts,0,2);
  next if $profile{$key};
  print "Enter $phrase\: ";
  chomp( my $answer = <STDIN> );
  $profile{$key} = $answer;
}

my $profile = Metabase::User::Profile->create( %profile );

if ( $output ) {
  print "Writing profile to '$output'\n";
  $profile->save( $output );
  chmod 0600, $output;
}
else {
  print JSON->new->encode( $profile->as_struct );
}

__END__

=head1 NAME

metabase-profile - create a metabase profile

=head1 SYNOPSIS

  $ metabase-profile -o myprofile.json
  Enter full name: John Doe
  Enter email address: jdoe@example.com
  Enter password/secret: zqxjkh
  Writing profile to 'myprofile.json'


=head1 USAGE

The metabase-profile program makes it easy to create a user profile for
submitting facts and reports to a Metabase server.  

Valid options include:

  -o, --output  FILE      filename to save profile in
      --name    FULLNAME  full user name, eg "John Doe"
      --email   ADDRESS   user email address eg "jd@example.com"
      --secret  PASSWORD  password for authentication
  -h, --help              print man page

Unless an output filename is provided, the profile will be printed to
STDOUT.

The profile contains a shared-secret password.  Typically, when a Metabase server
first receives a report from a new user profile, the shared secret is recorded and
will be used to authenticate subsequent submissions.  This file should not be 
shared publicly or made group or world readable.

Use the resulting profile according to the instructions of your Metabase client
program. You may wish to copy it across computers if you would like to be 
identified consistently when submitting reports from different locations.

=head1 AUTHOR

=over 

=item * David A. Golden (DAGOLDEN)

=back

=head1 COPYRIGHT AND LICENSE

Copyright (c) 2009 by David A. Golden

Licensed under the same terms as Perl itself (the "License").
You may not use this file except in compliance with the License.
A copy of the License was distributed with this file or you may obtain a 
copy of the License from http://dev.perl.org/licenses/

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut