82221df19f82ffdda3e22d8f0fba07279667e8c2
commit 82221df19f82ffdda3e22d8f0fba07279667e8c2
Author: spesk1 <spesk@pm.me>
Date: Fri Jun 21 21:05:46 2019 +0000

Basics of data gathering done. Need to redo all HTML writing still

diff --git a/README.md b/README.md
index 3b9f638..e237a81 100644
--- a/README.md
+++ b/README.md
@@ -14,7 +14,8 @@
* Line numbers for file browsing
* README.md markdown renderer/parser / display in project index
* Figure out way to expose clone/merge/etc interface
-* (Stretch GoaL) HTML based syntax highlighting for files
+* (Stretch Goal) HTML based syntax highlighting for files
+* Support detection of misconfig of config file

## Framework Plans ##
* gsg -- Perl script to generate the site, uses modules below
@@ -27,4 +28,5 @@
## Deps ##
* Shellex.pm ( https://spwbk.site/git/projects/perl-shellex.git/index.html )
* git
-* Log4Perl
+* Perl Log4Perl
+* Perl GetOpts
diff --git a/config.example b/config.example
new file mode 100644
index 0000000..3c40eaf
--- /dev/null
+++ b/config.example
@@ -0,0 +1,3 @@
+GitHome = "/home/git"
+WebRoot = "/var/www/html/git"
+IgnoredProjects = [ "/home/git/finance-perl.git/", "/home/git/git-site-gen.git/", "/home/git/misc-scripts.git/" ]
diff --git a/gsg b/gsg
new file mode 100755
index 0000000..4e2128d
--- /dev/null
+++ b/gsg
@@ -0,0 +1,56 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Getopt::Long qw(GetOptions);
+use Log::Log4perl qw(:easy);
+use lib "/usr/local/lib";
+use Shellex::Shellex qw(shellex findBin);
+use Gsg::ConfigParse qw(parse_gsg_config);
+use Gsg::Gather qw(get_file_tree get_projects);
+
+my $log_file = "gsg.log";
+sub get_log_name { return $log_file; };
+my $log_conf = q(
+ log4perl.rootLogger = INFO, LOG1, screen
+ log4perl.appender.LOG1 = Log::Log4perl::Appender::File
+ log4perl.appender.LOG1.filename = sub { get_log_name(); }
+ log4perl.appender.LOG1.mode = append
+ log4perl.appender.LOG1.layout = Log::Log4perl::Layout::PatternLayout
+ log4perl.appender.LOG1.layout.ConversionPattern = %d %p >> %m %n
+
+ log4perl.appender.screen = Log::Log4perl::Appender::Screen
+ log4perl.appender.screen.stderr = 0
+ log4perl.appender.screen.layout = PatternLayout
+ log4perl.appender.screen.layout.ConversionPattern = %d %p >> %m %n
+);
+
+Log::Log4perl::init(\$log_conf);
+my $logger = get_logger();
+
+my $config_file = "config.example";
+my %config = parse_gsg_config($config_file,$logger);
+
+my $web_root = $config{'WebRoot'};
+my $web_projects_dir = $web_root . "/projects/";
+my $root_index = $web_projects_dir . "index.html";
+
+foreach my $key ( keys %config ) {
+ if ( $key eq "IgnoredProjects" ) {
+ print "$key contains:\n";
+ foreach my $val ( @{$config{$key}} ) {
+ print "$val\n";
+ }
+ } else {
+ print "Key is $key and val is $config{$key}\n";
+ }
+}
+
+print "Projects are:\n";
+my $git_projects_ref = get_projects($config{'GitHome'},$config{'IgnoredProjects'},$logger);
+foreach my $p ( @$git_projects_ref ) {
+ print "$p\n";
+}
+# my ( $file_tree_ref, $file_content_ref, $commits_ref ) = get_file_tree($project_dir,$logger);
+
diff --git a/gsg.log b/gsg.log
new file mode 100644
index 0000000..b073aa4
--- /dev/null
+++ b/gsg.log
@@ -0,0 +1,11 @@
+2019/06/21 21:00:48 INFO >> Running: which cat 2>&1
+2019/06/21 21:00:48 INFO >> Returned: 0
+2019/06/21 21:00:48 INFO >> Running: /bin/cat config.example 2>&1
+2019/06/21 21:00:48 INFO >> Returned: 0
+2019/06/21 21:00:48 INFO >> Running: which ls 2>&1
+2019/06/21 21:00:48 INFO >> Returned: 0
+2019/06/21 21:00:48 INFO >> Running: /bin/ls -d /home/git/*/ 2>&1
+2019/06/21 21:00:48 INFO >> Returned: 0
+2019/06/21 21:00:48 INFO >> Found /home/git/finance-perl.git/ in ignore list, skipping...
+2019/06/21 21:00:48 INFO >> Found /home/git/git-site-gen.git/ in ignore list, skipping...
+2019/06/21 21:00:48 INFO >> Found /home/git/misc-scripts.git/ in ignore list, skipping...
diff --git a/install.sh b/install.sh
new file mode 100755
index 0000000..edbce03
--- /dev/null
+++ b/install.sh
@@ -0,0 +1,11 @@
+#!/bin/bash
+
+libDir="/usr/local/lib/Gsg/"
+
+if [ ! -d $libDir ]; then
+ echo "Making $libDir"
+ sudo mkdir -p $libDir
+fi
+
+echo "Calling sudo to copy libs"
+sudo cp ./lib/Gsg/* $libDir
diff --git a/lib/Gsg/ConfigParse.pm b/lib/Gsg/ConfigParse.pm
index 89278c3..da4858b 100644
--- a/lib/Gsg/ConfigParse.pm
+++ b/lib/Gsg/ConfigParse.pm
@@ -5,40 +5,41 @@ use Log::Log4perl qw(:easy);
use lib "/usr/local/lib";
use Shellex::Shellex qw(shellex findBin);
use Exporter qw(import);
-our @EXPORT_OK = qw();
+our @EXPORT_OK = qw(parse_gsg_config);

# https://perlmaven.com/trim
sub trim { my $s = shift; $s =~ s/^\s+|\s+$//g; return $s };

-sub parseGsgConfig($$) {
+sub parse_gsg_config($$) {

- my $configPath = shift;
+ my $config_path = shift;
my $logger = shift;
- if ( ! -e $config ) {
- $logger->error("$config doesn't look like a regular file, exiting...");
+ if ( ! -e $config_path ) {
+ $logger->error("$config_path doesn't look like a regular file, exiting...");
exit 1;
}
- my $catCmd = findBin("cat",$logger);
- my @configLines = split("\n",shellex("$catCmd $config",$logger));
- my %configHash;
- foreach my $line ( @configLines ) {
+ # Should probably just open with perl, needless shellout TODO
+ my $cat_cmd = findBin("cat",$logger);
+ my @config_lines = split("\n",shellex("$cat_cmd $config_path",$logger));
+ my %config_hash;
+ foreach my $line ( @config_lines ) {
chomp $line;
if ( $line =~ m/^(.*)\ =\ "(.*)"$/ ) {
- $configHash{$1} = $2;
+ $config_hash{$1} = $2;
}

if ( $line =~ m/^(.*)\ =\ \[(.*)\]/ ) {
- my @trimmedPorts;
- my @ports = split(",",$2);
- foreach my $port (@ports) {
- $port =~ /(\d{1,5})/;
- push(@trimmedPorts,trim($1));
+ my @trimmed_vals;
+ my @vals = split(",",$2);
+ foreach my $val (@vals) {
+ $val =~ /"(.*)"/;
+ push(@trimmed_vals,trim($1));
}
- $configHash{$1} = \@trimmedPorts;
+ $config_hash{$1} = \@trimmed_vals;
}
}

- return %configHash;
+ return %config_hash;

}

diff --git a/lib/Gsg/Gather.pm b/lib/Gsg/Gather.pm
index 6aae46f..ea848b4 100644
--- a/lib/Gsg/Gather.pm
+++ b/lib/Gsg/Gather.pm
@@ -2,7 +2,74 @@ package Gsg::Gather;
use strict;
use warnings;
use Log::Log4perl qw(:easy);
+use Shellex::Shellex qw(shellex findBin);
use Exporter qw(import);
-our @EXPORT_OK = qw();
+our @EXPORT_OK = qw(get_file_tree get_projects);
+
+sub get_projects($$$) {
+
+ my $git_dir = shift;
+ my $ignored_projects_ref = shift;
+ my $logger = shift;
+ my $ls_cmd = findBin("ls",$logger);
+ my @git_project_dirs;
+ foreach my $dir ( split("\n", shellex("$ls_cmd -d $git_dir/*/",$logger)) ) {
+ if ( $dir !~ m/\.git/ ) {
+ next;
+ }
+ if ( grep( /^$dir$/, @$ignored_projects_ref ) ) {
+ $logger->info("Found $dir in ignore list, skipping...");
+ next;
+ } else {
+ push(@git_project_dirs,$dir);
+ }
+ }
+
+ return \@git_project_dirs;
+
+}
+
+sub get_file_tree($$) {
+
+ my $projectDir = shift;
+ my $logger = shift;
+ my $gitCmd = findBin("git",$logger);
+
+ # Get files
+ my %file_tree;
+ foreach my $file ( split("\n", shellex("$gitCmd --git-dir=\"$projectDir\" ls-tree --full-tree -r HEAD",$logger)) ) {
+ chomp $file;
+ $file =~ /([a-z0-9]{40})\t(.*)$/;
+ # Name - object id
+ $file_tree{$2} = $1;
+ }
+
+ # Get file content
+ my %file_content;
+ foreach my $filename ( keys %file_tree ) {
+ my $content = shellex("$gitCmd --git-dir=\"$projectDir\" show $file_tree{$filename}",$logger);
+ chomp $content;
+ # Name - file content
+ $file_content{$filename} = $content;
+ }
+
+ # Get logs
+ my @commit_ids;
+ foreach my $log_line ( split("\n",shellex("$gitCmd --git-dir=\"$projectDir\" log",$logger)) ) {
+ if ( $log_line =~ m/commit\ ([a-z0-9]{40})/ ) {
+ push(@commit_ids,$1);
+ }
+ }
+
+ my %commits;
+ foreach my $commit_id ( @commit_ids ) {
+ my $commit_info = shellex("git --git-dir=\"$projectDir\" show $commit_id",$logger);
+ chomp $commit_info;
+ $commits{$commit_id} = $commit_info;
+ }
+
+ return ( \%file_tree, \%file_content, \%commits );
+
+}

1;