Skip to content

Commit

Permalink
Add '--subdir' option: all to specify the directory to search go.mod in
Browse files Browse the repository at this point in the history
So far the first instance of go.mod found was picked. This may cause
issues if multiple go.mod files are in a repository with sub-projects.
Now we assume go.mod will reside in the top-most directory unless
specified otherwise thru a command line option.
Note: We keep this separate from the --basename option: the latter
will most likely contain a version number and will require a change
every time the package is updated, yet it is expected that subdir
will be used more frequently.
This addresses:
See also: #1

Signed-off-by: Egbert Eich <[email protected]>
  • Loading branch information
e4t authored and jfkw committed Feb 27, 2023
1 parent ebd2ddd commit 25c2bc7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
20 changes: 11 additions & 9 deletions go_modules
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,6 @@ def extract(filename, outdir):
os.chdir(cwd)


def find_file(path, filename):
for root, dirs, files in os.walk(path):
if filename in files:
return os.path.join(root, filename)


def cmd_go_mod(cmd, dir):
"""Execute go mod subcommand using subprocess.run().
Capture both stderr and stdout as text.
Expand All @@ -201,6 +195,12 @@ def cmd_go_mod(cmd, dir):
log.error(cp.stderr.strip())
return cp

def sanitize_subdir(dir, subdir):
ret = os.path.normpath(subdir)
if dir == os.path.commonpath([dir, ret]):
return ret
log.error("Invalid path: {ret} not subdir of {dir}")
exit (1)

def main():
log.info(f"Running OBS Source Service: {app_name}")
Expand All @@ -213,9 +213,11 @@ def main():
parser.add_argument("--outdir")
parser.add_argument("--compression", default=DEFAULT_COMPRESSION)
parser.add_argument("--basename")
parser.add_argument("--subdir")
args = parser.parse_args()

outdir = args.outdir
subdir = args.subdir

archive_format, archive_compression, archive_ext = get_archive_parameters(args)
vendor_tarname = f"vendor.{archive_ext}"
Expand All @@ -224,10 +226,10 @@ def main():

with tempfile.TemporaryDirectory() as tempdir:
extract(archive, tempdir)
basename = args.basename or basename_from_archive(archive) or basename_from_archive_name(archive)

go_mod_path = find_file(os.path.join(tempdir, basename), "go.mod")
if go_mod_path:
basename = args.basename or basename_from_archive(archive) or basename_from_archive_name(archive)
go_mod_path = sanitize_subdir(tempdir, os.path.join(tempdir, basename, subdir, "go.mod"))
if go_mod_path and os.path.exists(go_mod_path):
go_mod_dir = os.path.dirname(go_mod_path)
log.info(f"Using go.mod found at {go_mod_path}")
else:
Expand Down
2 changes: 2 additions & 0 deletions go_modules.service
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@
</parameter>
<parameter name="basename">
<description>Normally the go_modules service is able to determine the name of the top directory. Should this not be possible for some reason, use this option to specify.</description>
<parameter name="subdir">
<description>If go.mod is not available in the top directory of the archive, specify its path (relative to the top directory).</description>
</parameter>
</service>

0 comments on commit 25c2bc7

Please sign in to comment.