Last active
March 2, 2021 12:52
-
-
Save takakd/5a786eb5ec17cd95868828341f09f8cf to your computer and use it in GitHub Desktop.
The helper script of mockgen in golang/mock.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
# Usage | |
sub usage() { | |
my $usage = <<"EOS"; | |
$0 creates go mock file in the same directory where the go file is located. | |
Usage: | |
$0 <go file path> | |
EOS | |
print($usage); | |
} | |
# Check arg count. | |
my $argc = @ARGV; | |
if ($argc < 1) { | |
usage; | |
exit 1; | |
} | |
# Get go file. | |
my $file = $ARGV[0]; | |
# Get package name in the input go file. | |
my $pkg = ""; | |
my $fh; | |
open($fh, '<', $file) or die $!; | |
while (my $line = <$fh>) { | |
# Parse package definition line. | |
if ($line =~ /^package ([a-z_0-9]+)$/) { | |
$pkg = $1; | |
last; | |
} | |
} | |
close($fh); | |
# Add "_mock" to the output mock file. | |
my $mock_file = $file; | |
$mock_file =~ s/.go$/_mock.go/; | |
# mockgen command. | |
my $cmd = "mockgen -source=${file} -destination=${mock_file} -package=${pkg}"; | |
# Run command. | |
system($cmd); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment