NAME

Lang::Go::Mod - parse and model go.mod files

SYNOPSIS

# $ cat go.mod
# module github.com/example/my-project
# go 1.16
# // comments
# exclude (
#    example.com/whatmodule v1.4.0
# )
# replace (
#    github.com/example/my-project/pkg/app => ./pkg/app
# )
# require (
#    golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
# )
# retract (
#    v1.0.0 // this version should not be used
# )

use Lang::Go::Mod qw(read_go_mod parse_go_mod);

my $go_mod_path = '/path/to/go.mod';

# read and parse the go.mod file
#
# all errors croak, so wrap this in your favorite variant of try/catch
# to gracefully manage errors
my $m = read_go_mod($go_mod_path);
# use parse_go_mod to parse the go.mod content if it is already in a scalar
# my $m = parse_go_mod($go_mod_file_content);

print $m->{module}; # github.com/example/my-project
print $m->{go}; # 1.16
print $m->{exclude}->{'example.com/whatmodule'}; # [v1.4.0]
print $m->{replace}->{'github.com/example/my-project/pkg/app'}; # ./pkg/app
print $m->{'require'}->{'golang.org/x/sys'}; # v0.0.0-20210510120138-977fb7262007
print $m->{retract}->{'v1.0.0'}; # 1

DESCRIPTION

This module creates a hash representation of a go.mod file. Both single line and multiline exclude, replace, require and retract sections are supported. For a full reference of the go.mod format, see

https://golang.org/doc/modules/gomod-ref

EXPORTED METHODS

read_go_mod

Given a full filepath for a go.mod file, read it, parse it and return the hash representation of the contents. All errors croak.

parse_go_mod

Given a scalar of the contents of a go.mod file, parse it and return the hash representation of the contents. All errors croak.

LICENSE

Lang::Go::Mod is licensed under the same terms as Perl itself.

https://opensource.org/licenses/artistic-license-2.0

CONTRIBUTORS

Ben Bullock (https://github.com/benkasminbullock) Brad Clawsie (https://b7j0c.org)