Skip to content

Commit

Permalink
feat: send the go runtime version in the metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-go committed Sep 12, 2017
1 parent 8d4449d commit 8a2a2b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
35 changes: 25 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,23 @@ module.exports = {
};

function inspect(root, targetFile, options) {
if (!options) { options = { dev: false }; }
return Promise.all([
getMetaData(root),
getDependencies(root, targetFile),
])
.then(function (result) {
return {
plugin: result[0],
package: result[1],
};
});
}

function getDependencies(root, targetFile) {
var depLocks;
return new Promise(function (resolve, reject) {
try {
depLocks = parseDepLock(root, targetFile, options);
depLocks = parseDepLock(root, targetFile);
resolve(depLocks);
} catch (e) {
reject(new Error('failed parsing Gopkg.lock file: ' + e.message));
Expand All @@ -34,13 +45,7 @@ function inspect(root, targetFile, options) {
var pkgsTree = recursivelyBuildPkgTree(tree, depLocks, projectRootPath, []);
pkgsTree.packageFormatVersion = 'golang:0.0.1';

return {
plugin: {
name: 'snyk-go-plugin',
// TODO: engine: `go version`
},
package: pkgsTree,
}
return pkgsTree;
}).catch(function (error) {
if (typeof error === 'string') {
if (error.indexOf('Unresolved packages:') !== -1) {
Expand All @@ -52,6 +57,16 @@ function inspect(root, targetFile, options) {
});
}

function getMetaData(root) {
return subProcess.execute('go', ['version'], {cwd: root})
.then(function (output) {
return {
name: 'snyk-go-plugin',
runtime: /(go\d+\.\d+\.\d+)/.exec(output)[0],
};
});
}

function isRootSubpkg(pkgPath, projectRootPath) {
if (pkgPath == projectRootPath) {
return true;
Expand Down Expand Up @@ -104,7 +119,7 @@ function recursivelyBuildPkgTree(goDepsTree, depLocks, projectRootPath, fromPath
return pkg;
}

function parseDepLock(root, targetFile, options) {
function parseDepLock(root, targetFile) {
var lock = fs.readFileSync(path.join(root, targetFile));

// TODO: handle parse error
Expand Down
5 changes: 4 additions & 1 deletion test/inspect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ test('happy inspect', function (t) {
t.test('plugin', function (t) {
t.ok(plugin, 'plugin');
t.equal(plugin.name, 'snyk-go-plugin', 'name');
t.match(plugin.runtime, /^go\d+/, 'engine');
t.end();
});

Expand Down Expand Up @@ -81,6 +82,7 @@ test('pkg with local import', function (t) {
t.test('plugin', function (t) {
t.ok(plugin, 'plugin');
t.equal(plugin.name, 'snyk-go-plugin', 'name');
t.match(plugin.runtime, /^go\d+/, 'engine');
t.end();
});

Expand Down Expand Up @@ -163,6 +165,7 @@ test('pkg without external deps', function (t) {
t.test('plugin', function (t) {
t.ok(plugin, 'plugin');
t.equal(plugin.name, 'snyk-go-plugin', 'name');
t.match(plugin.runtime, /^go\d+/, 'engine');
t.end();
});

Expand All @@ -188,4 +191,4 @@ function chdirToPkg(pkgPathArray) {
[__dirname, 'fixtures', 'gopath', 'src'].concat(pkgPathArray)
)
);
}
}

0 comments on commit 8a2a2b6

Please sign in to comment.