11import * as core from '@actions/core'
22import { run } from '@github/dependency-submission-toolkit'
33import { ProcessDependenciesContent } from '@github/dependency-submission-toolkit/dist/processor'
4- import { parseDependents } from './go_mod_parser'
4+ import { Detector , Metadata } from '@github/dependency-submission-toolkit/dist/snapshot'
5+ import { parseDependents } from './parse-go-package'
56import * as path from 'path'
67import * as process from 'process'
7- import execa from 'execa '
8+ import fs from 'fs '
89
910const parseDependentsFunc : ProcessDependenciesContent = parseDependents
1011
@@ -15,43 +16,35 @@ const detector = {
1516 version : core . getInput ( 'detector-version' )
1617}
1718
18- async function searchForFile ( filename : string ) {
19- console . log ( `searching for ${ filename } in ${ process . cwd ( ) } ` )
20-
21- const { stdout } = await execa ( 'find' , [ process . cwd ( ) , '-name' , filename ] )
22-
23- const dirs = stdout
24- . split ( '\n' )
25- . filter ( ( s ) => s . length > 0 )
26- // remove the file name
27- . map ( ( filename ) => path . dirname ( filename ) )
28- // map to absolute path
29- . map ( ( pathname ) => path . resolve ( process . cwd ( ) , pathname ) )
30-
31- return dirs
32- }
19+ // For a specific Go _build target_, this commands lists all dependencies used
20+ // to build the build target It does not provide association between the
21+ // dependencies (i.e. which dependencies depend on which)
22+ // eslint-disable-next-line quotes
23+ const goListDependencies = `go list -deps -f '{{define "M"}}{{.Path}}@{{.Version}}{{end}}{{with .Module}}{{if not .Main}}{{if .Replace}}{{template "M" .Replace}}{{else}}{{template "M" .}}{{end}}{{end}}{{end}}'`
3324
3425// Enumerate directories
3526async function detect ( ) {
36- const goModPaths = await searchForFile ( 'go.mod' )
37-
3827 // If provided, set the metadata provided from the action workflow input
28+ const goModPath = core . getInput ( 'goModPath' )
29+ if ( path . basename ( goModPath ) !== 'go.mod' && fs . existsSync ( goModPath ) ) {
30+ throw new Error ( `${ goModPath } is not a go.mod file or does not exist!` )
31+ }
32+ const goModDir = path . dirname ( goModPath )
33+ const goBuildTarget = core . getInput ( 'goBuildTarget' )
34+ if ( goBuildTarget !== 'all' && goBuildTarget !== '...' && fs . existsSync ( path . join ( goModDir , goBuildTarget ) ) ) {
35+ throw new Error ( `The build target '${ goBuildTarget } ' does not exist at ${ path . join ( goModDir , goBuildTarget ) } ` )
36+ }
37+
3938 const metadataInput = core . getInput ( 'metadata' )
4039
41- goModPaths . forEach ( ( path ) => {
42- process . chdir ( path )
43- console . log ( `Running go mod graph in ${ path } ` )
44- if ( metadataInput . length < 1 ) {
45- run ( parseDependentsFunc , { command : 'go mod graph' } , { detector } )
46- } else {
47- const metadata = JSON . parse ( metadataInput )
48- run (
49- parseDependentsFunc ,
50- { command : 'go mod graph' } ,
51- { metadata, detector }
52- )
53- }
54- } )
40+ process . chdir ( goModPath )
41+ console . log ( `Running go package detection in ${ path } on build target ${ goBuildTarget } ` )
42+ const options : { detector : Detector , metadata ?: Metadata } = { detector }
43+ if ( metadataInput ) {
44+ const metadata = JSON . parse ( metadataInput )
45+ options . metadata = metadata
46+ }
47+ run ( parseDependentsFunc , { command : `${ goListDependencies } ${ goBuildTarget } ` } , options )
5548}
5649
5750detect ( )
0 commit comments