Skip to content

Commit c8b16d3

Browse files
authored
Merge pull request #69 from SocketDev/support-go
Add Golang support
2 parents ae2f58c + 524b9bd commit c8b16d3

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ socket report view QXU8PmK7LfH608RAwfIKdbcHgwEd_ZeWJ9QEGv05FJUQ
2626

2727
* `socket report create <path(s)-to-folder-or-file>` - creates a report on [socket.dev](https://socket.dev/)
2828

29-
Uploads the specified `package.json` and lock files for JavaScript and Python dependency manifests.
29+
Uploads the specified `package.json` and lock files for JavaScript, Python, and Go dependency manifests.
3030
If any folder is specified, the ones found in there recursively are uploaded.
3131

32-
Supports globbing such as `**/package.json`, `**/requirements.txt`, and `**/pyproject.toml`.
32+
Supports globbing such as `**/package.json`, `**/requirements.txt`, `**/pyproject.toml`, and `**/go.mod`.
3333

3434
Ignores any file specified in your project's `.gitignore`, the `projectIgnorePaths` in your project's [`socket.yml`](https://docs.socket.dev/docs/socket-yml) and on top of that has a sensible set of [default ignores](https://www.npmjs.com/package/ignore-by-default)
3535

lib/commands/report/create.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ async function setupCommand (name, description, argv, importMeta) {
107107
Usage
108108
$ ${name} <paths-to-package-folders-and-files>
109109
110-
Uploads the specified "package.json" and lock files for JavaScript and Python dependency manifests.
110+
Uploads the specified "package.json" and lock files for JavaScript, Python, and Go dependency manifests.
111111
If any folder is specified, the ones found in there recursively are uploaded.
112112
113-
Supports globbing such as "**/package.json", "**/requirements.txt", and "**/pyproject.toml".
113+
Supports globbing such as "**/package.json", "**/requirements.txt", "**/pyproject.toml", and "**/go.mod".
114114
115115
Ignores any file specified in your project's ".gitignore", your project's
116116
"socket.yml" file's "projectIgnorePaths" and also has a sensible set of

lib/utils/path-resolve.js

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export async function mapGlobEntryToFiles (entry, supportedFiles) {
100100
let jsLockFiles = []
101101
/** @type {string[]} */
102102
let pyFiles = []
103+
/** @type {string|undefined} */
104+
let pkgGoFile
105+
/** @type {string[]} */
106+
let goExtraFiles = []
103107

104108
const jsSupported = supportedFiles['npm'] || {}
105109
const jsLockFilePatterns = Object.keys(jsSupported)
@@ -108,10 +112,20 @@ export async function mapGlobEntryToFiles (entry, supportedFiles) {
108112

109113
const pyFilePatterns = Object.values(supportedFiles['pypi'] || {})
110114
.map(p => /** @type {{ pattern: string }} */ (p).pattern)
115+
116+
const goSupported = supportedFiles['go'] || {}
117+
const goSupplementalPatterns = Object.keys(goSupported)
118+
.filter(key => key !== 'gomod')
119+
.map(key => /** @type {{ pattern: string }} */ (goSupported[key]).pattern)
120+
111121
if (entry.endsWith('/')) {
112122
// If the match is a folder and that folder contains a package.json file, then include it
113-
const filePath = path.resolve(entry, 'package.json')
114-
if (await fileExists(filePath)) pkgJSFile = filePath
123+
const jsPkg = path.resolve(entry, 'package.json')
124+
if (await fileExists(jsPkg)) pkgJSFile = jsPkg
125+
126+
const goPkg = path.resolve(entry, 'go.mod')
127+
if (await fileExists(goPkg)) pkgGoFile = goPkg
128+
115129
pyFiles = await globby(pyFilePatterns, {
116130
...BASE_GLOBBY_OPTS,
117131
cwd: entry
@@ -126,6 +140,11 @@ export async function mapGlobEntryToFiles (entry, supportedFiles) {
126140
jsLockFiles = [entry]
127141
pkgJSFile = path.resolve(path.dirname(entry), 'package.json')
128142
if (!(await fileExists(pkgJSFile))) return []
143+
} else if (entryFile === 'go.mod') {
144+
pkgGoFile = entry
145+
} else if (micromatch.isMatch(entryFile, goSupplementalPatterns)) {
146+
goExtraFiles = [entry]
147+
pkgGoFile = path.resolve(path.dirname(entry), 'go.mod')
129148
} else if (micromatch.isMatch(entryFile, pyFilePatterns)) {
130149
pyFiles = [entry]
131150
}
@@ -141,7 +160,19 @@ export async function mapGlobEntryToFiles (entry, supportedFiles) {
141160
})
142161
}
143162

144-
return [...jsLockFiles, ...pyFiles].concat(pkgJSFile ? [pkgJSFile] : [])
163+
if (!goExtraFiles.length && pkgGoFile) {
164+
// get go.sum whenever possible
165+
const pkgDir = path.dirname(pkgGoFile)
166+
167+
goExtraFiles = await globby(goSupplementalPatterns, {
168+
...BASE_GLOBBY_OPTS,
169+
cwd: pkgDir
170+
})
171+
}
172+
173+
return [...jsLockFiles, ...pyFiles, ...goExtraFiles]
174+
.concat(pkgJSFile ? [pkgJSFile] : [])
175+
.concat(pkgGoFile ? [pkgGoFile] : [])
145176
}
146177

147178
/**

0 commit comments

Comments
 (0)