<commit_before>package CSG::Storage::SlotFS::Command::init;

use CSG::Storage::SlotFS -command;

use Modern::Perl;
use Module::Load;
use Try::Tiny;
use Number::Bytes::Human qw(parse_bytes);

use CSG::Logger;

sub opt_spec {
  return (
    ['project|p=s', 'Project name the slot belongs to',                                         {required => 1}],
    ['name|n=s',    'Name of the slot to initialize',                                           {required => 1}],
    ['size|s=s',    'Size of the initial sample directory in human readable form (i.e. 400GB)', {required => 1}],
    ['prefix|=s',   'Optional path prefix (e.g. /tmp)'],
  );
}

sub validate_args {
  my ($self, $opts, $args) = @_;

  if ($opts->{prefix} and not -e $opts->{prefix}) {
    $self->usage_error('Prefix must exist');
  }
}

sub execute {
  my ($self, $opts, $args) = @_;

  my $rc     = 0;
  my $logger = CSG::Logger->new();
  my $class  = 'CSG::Storage::SlotFS::' . ucfirst(lc($opts->{project}));
  my $slot   = undef;

  try {
    load $class;

    my %params = (
      name => $opts->{name},
      size => parse_bytes($opts->{size}),
    );

    if ($opts->{prefix}) {
      $params{prefix} = $opts->{prefix};
    }

    $slot = $class->new(%params);

    $slot->initialize;
    $slot->save_manifest;

    $logger->info($slot->to_string);
  }
  catch {
    if (not ref $_) {
      $logger->error('Unknown error occured');
    } elsif ($_->isa('CSG::Storage::Slots::Exceptions::Sample::FailedSkeletonDirectory')) {
      $logger->error($_->error);
    } else {
      if ($_->isa('Exception::Class')) {
        $logger->error($_->error);
      } else {
        $logger->error("something went sideways: $_");
      }
    }

    $rc = 1;
  };

  exit $rc;
}

1;

__END__

=head1

CSG::Storage::SlotFS::Command::init - Initialize slot directory structure<commit_msg>include the actual error in the output for debugging<commit_after>package CSG::Storage::SlotFS::Command::init;

use CSG::Storage::SlotFS -command;

use Modern::Perl;
use Module::Load;
use Try::Tiny;
use Number::Bytes::Human qw(parse_bytes);

use CSG::Logger;

sub opt_spec {
  return (
    ['project|p=s', 'Project name the slot belongs to',                                         {required => 1}],
    ['name|n=s',    'Name of the slot to initialize',                                           {required => 1}],
    ['size|s=s',    'Size of the initial sample directory in human readable form (i.e. 400GB)', {required => 1}],
    ['prefix|=s',   'Optional path prefix (e.g. /tmp)'],
  );
}

sub validate_args {
  my ($self, $opts, $args) = @_;

  if ($opts->{prefix} and not -e $opts->{prefix}) {
    $self->usage_error('Prefix must exist');
  }
}

sub execute {
  my ($self, $opts, $args) = @_;

  my $rc     = 0;
  my $logger = CSG::Logger->new();
  my $class  = 'CSG::Storage::SlotFS::' . ucfirst(lc($opts->{project}));
  my $slot   = undef;

  try {
    load $class;

    my %params = (
      name => $opts->{name},
      size => parse_bytes($opts->{size}),
    );

    if ($opts->{prefix}) {
      $params{prefix} = $opts->{prefix};
    }

    $slot = $class->new(%params);

    $slot->initialize;
    $slot->save_manifest;

    $logger->info($slot->to_string);
  }
  catch {
    if (not ref $_) {
      $logger->error("Unknown error occured: $_");
    } elsif ($_->isa('CSG::Storage::Slots::Exceptions::Sample::FailedSkeletonDirectory')) {
      $logger->error($_->error);
    } else {
      if ($_->isa('Exception::Class')) {
        $logger->error($_->error);
      } else {
        $logger->error("something went sideways: $_");
      }
    }

    $rc = 1;
  };

  exit $rc;
}

1;

__END__

=head1

CSG::Storage::SlotFS::Command::init - Initialize slot directory structure
{'fraction_non_alphanumeric': 0.19339242546333602, 'fraction_numerical': 0.005372011818426001, 'mean_word_length': 2.643835616438356, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7140430', 'n_tokens_mistral': 1292, 'n_tokens_neox': 1240, 'n_words': 287}