#!/bin/sh
#! -*-perl-*-
eval 'exec perl -x -wS $0 ${1+"$@"}' if 0;
#
# -*- mode: cperl; eval: (follow-mode) -*-
#

use strict;
use warnings;
use App::gqmt;

App::gqmt->new->run;

__END__

=head1 NAME

gqmt - Graphql Query Mutation Tool

=head1 SYNOPSIS

gqmt [-h] <-u USER -R REPO -T TOKEN -P PACKAGE -t query.tt> REST OF OPTIONS

=head1 DESCRIPTION

script to clean up old package versions from GitHub repository

=head1 OPTIONS

=over 4

=item B<-n | --dry-run>

dry run

=item B<-a | --age> I<INTEGER>

age of version in seconds, to keep. default is 2 weeks

=item B<-u | --user> I<STRING>

user name of repository owner

=item B<-R | --repository> I<STRING>

name of the repository to manipulate images of

=item B<-t | --query-template> I<STRING>

path to query template (see B<TEMPLATES> bellow). Template is injected with these variables:

=over

repo     : repository name

user     : user name

pkg_num  : first num element of packages list

pkg_name : package name

vers_num : first num elements from the version files

cursor   : pagination object

=back

=item B<-T | --token> I<STRING>

personal access token to access the GitHub API

=item B<-U | --url> I<STRING>

GraphQL API endpoint, default is I<https://api.github.com/graphql>

=item B<-P | --package> I<STRING>

package name to manage versions of

supported packages are:

=item B<--package-regex> I<STRING>

regex to exclude package version name from been processed

=over

api

app

scheduler

=back

=item B<-N | --rows-number> I<INTEGER>

number of rows for reply pagination, max 100 (default 100)

=item B<-C | --colored>
	      
to use terminal colors in output

=item B<-D | --delete>

if set, then all versions selected are to be deleted, if option I<-v>
is set, then the only one single version is to be deleted (the one, set
with I<-v>)

=item B<-s | --single-run>

process only first page of rows

=item B<-v | --package-version>

package version to manipulate with

=item B<--versions-to-hold>

minimum number of versions to hold (default: 2)

=item B<-V | --version>

version information

=item B<-d | --debug>

be verbose

=item B<-h | --help>

help message

=back

=head1 EXAMPLE

=over

    gqmt < -u user-name -R repo-name -P pkg-name -T xxxxxxxxxxxxxxxxxx >

to exclude all versions from been deleted:

    gqmt -u user-name -R repo-name -P pkg-name -T xxxxxxxxxxxxxxxxxx -D --package-regex '.*'

=back

=head1 TEMPLATES

Templates are .tt files. Please refer to B<Template::Toolkit> documentation.

See examples in distribution directory I<etc>

Example of query template:

    query { repository(name: "[% repo %]", owner: "[% user %]") {
      packages(first: [% pkg_num %] names: ["[% pkg_name %]"]) {
          nodes {
            id
            name
            versions(last: [% vers_num %][% cursor %]) {
              nodes {
                id
                version
                files(first:1, orderBy: {direction: DESC, field: CREATED_AT}) {
                  totalCount
                  nodes {
                    updatedAt
                  }
                }
              }
              pageInfo {
                endCursor
                hasNextPage
                hasPreviousPage
                startCursor
              }
            }
          }
        }
      }
    }

=head1 SEE ALSO

L<https://docs.github.com/en/graphql/guides/forming-calls-with-graphql>

=head1 AUTHOR

Zeus Panchenko <zeus@gnu.org.ua>

=head1 COPYRIGHT

Copyright 2020 Zeus Panchenko.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

=cut