Skip to content

Instantly share code, notes, and snippets.

@mackee
Last active October 9, 2019 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mackee/a0493902ec8d10c4a1c74e816d6c97dd to your computer and use it in GitHub Desktop.
Save mackee/a0493902ec8d10c4a1c74e816d6c97dd to your computer and use it in GitHub Desktop.
too simple tool dependencies installer and executor in Golang
#!/usr/bin/perl
use strict;
use warnings;
use utf8;
=head1 SYNOPSIS
gomx - too simple tool dependencies installer and executor in Golang
$ gomx install github.com/golang/mock/mockgen
$ gomx exec mockgen
help - show this message
exec <tool> - execute tool with specify version in go.mod
install <tool import path> - install tool to GOBIN
"exec" and "install" sub commands can be use `--gobin=<path>` option.
If you use this option, set <path> to GOBIN environment variable.
A default of "-gobin" value is "<current directory>/_bin".
=cut
use Getopt::Long qw/:config posix_default no_ignore_case bundling auto_help/;
use Pod::Usage qw/pod2usage/;
use File::Spec;
use File::Path qw/make_path/;
my $gobin = $ENV{GOBIN} || "_bin";
GetOptions("gobin=s" => \$gobin) or pod2usage(1);
if (scalar(@ARGV) < 1 || $ARGV[0] eq "help") {
pod2usage(1);
}
my $subcommand = $ARGV[0];
my $abs_gobin = File::Spec->rel2abs($gobin);
if ($subcommand eq "exec") {
if (scalar(@ARGV) < 2) {
print "tool name is required, Ex. gomx exec <tool>\n";
exit(1);
}
my @commands = @ARGV[1..(scalar(@ARGV)-1)];
if ($commands[0] eq "--") {
shift @commands;
}
exec File::Spec->catpath("", $abs_gobin, $commands[0]), @commands[1..(scalar(@commands)-1)];
} elsif ($subcommand eq "install") {
if (!-d $abs_gobin) {
make_path($abs_gobin);
}
if (scalar(@ARGV) < 2) {
print "tool path is required, Ex. gomx install <tool path>\n";
exit(1);
}
my @paths = @ARGV[1..(scalar(@ARGV)-1)];
if ($paths[0] eq "--") {
shift @paths;
}
local $ENV{GOBIN} = $abs_gobin;
exec qw/go install/, @paths;
} else {
print "$subcommand sub command is not support.";
pod2usage(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment