Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·115 lines (94 loc) · 4.11 KB
/
Copy pathindex.js
File metadata and controls
executable file
·115 lines (94 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env node
const chalk = require('chalk');
const fs = require('fs');
const figlet = require('figlet');
const axios = require('axios');
const PROJECT_AUTO_CREATE = '--auto-create';
const PROJECT_NAME_FLAG = '--project-name';
const PROJECT_VERSION_FLAG = '--project-version';
const DEPENDENCY_TRACKER_SERVER_FLAG = '--server';
const BOM_PATH_FLAG = '--bom-path';
const API_KEY_FLAG = '--api-key';
const checkFlagsAndRun = async (args) => {
let autoCreate = getFlagValue(args, PROJECT_AUTO_CREATE, true);
let projectName = getFlagValue(args, PROJECT_NAME_FLAG, null);
let projectVersion = getFlagValue(args, PROJECT_VERSION_FLAG, null);
let server = getFlagValue(args, DEPENDENCY_TRACKER_SERVER_FLAG, null);
let bomPath = getFlagValue(args, BOM_PATH_FLAG, null);
let apiKey = getFlagValue(args, API_KEY_FLAG, null);
if(projectName && projectVersion && server && bomPath && apiKey) {
await run(server, bomPath, apiKey, projectName, projectVersion, autoCreate);
}
else {
console.log(
chalk.yellow(
`usage: dtrack-cli [${DEPENDENCY_TRACKER_SERVER_FLAG} <serverBaseUrl>] [${BOM_PATH_FLAG} <bomPath>]\n` +
` [${API_KEY_FLAG} <apiKey>] [${PROJECT_NAME_FLAG} <projectName>]\n` +
` [${PROJECT_VERSION_FLAG} <projectVersion>] [${PROJECT_AUTO_CREATE} <true|false>]`
)
);
console.log(
chalk.cyanBright(
`${DEPENDENCY_TRACKER_SERVER_FLAG} \n\t The Dependency Tracker server base url. Example: https://myserver.com \n` +
`${BOM_PATH_FLAG} \n\t Relative path to the BOM file. This file can be generated by several tools, look here https://cyclonedx.org/. Example: ./bom.xml \n` +
`${PROJECT_NAME_FLAG} \n\t Name of the project in Dependency Tracker. If not exists and ${PROJECT_AUTO_CREATE} it's setted to true, will be created automatically. Examples: name-without-spaces, "Name with spaces" \n` +
`${PROJECT_VERSION_FLAG} \n\t Version of the project in Dependency Tracker. If not exists and ${PROJECT_AUTO_CREATE} it's setted to true, will be created automatically. Examples: 1.0, latest, "Rare version with spaces ¯\_(ツ)_/¯" \n` +
`${PROJECT_AUTO_CREATE} \n\t If setted to true, and ${PROJECT_NAME_FLAG} + ${PROJECT_VERSION_FLAG} don't exists, automatically create them. Default value is true. Examples: true, false" \n`
)
);
}
};
function getFlagValue(args, flagKey, defaultValue) {
let result = defaultValue;
const index = args.indexOf(flagKey);
if(index != -1) {
result = args[index + 1];
}
return result;
}
const run = async (baseUrl, bomPath, apiKey, projectName, projectVersion, autoCreate) => {
const headers = {
headers: {
'X-Api-Key': apiKey,
'Content-Type': 'application/json'
}
}
const bom = fs.readFileSync(bomPath);
const bomBuffer = Buffer.from(bom);
const bomBase64 = bomBuffer.toString('base64');
const data = {
autoCreate: autoCreate,
projectName: projectName,
projectVersion: projectVersion,
bom: bomBase64
}
try {
let finalURL = baseUrl + '/api/v1/bom';
if (baseUrl.slice(-1) === '/') {
finalURL = baseUrl + 'api/v1/bom';
}
else {
finalURL = baseUrl + '/api/v1/bom';
}
console.log(chalk.whiteBright(`PUT ${finalURL}`));
await axios.put(finalURL, data, headers);
console.log(chalk.whiteBright('Done!'));
}
catch(ex) {
console.log(chalk.bgRed('Error uploading bom', ex));
console.log(chalk.bgRedBright(ex.stack));
console.log(chalk.redBright('Review your settings ;)'))
}
};
console.log(
chalk.yellow(
figlet.textSync('dtrack BOM Uploader')
)
);
console.log(
chalk.blue(
'Developed by @DogDeveloper. Show me your love here https://twitter.com/DogDeveloper'
)
);
const myArgs = process.argv.slice(2);
checkFlagsAndRun(myArgs);