forked from avwsolutions/setup-dapr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.js
More file actions
162 lines (134 loc) · 4.88 KB
/
action.js
File metadata and controls
162 lines (134 loc) · 4.88 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
const fs = require("fs");
const util = require("util");
const path = require("path");
const core = require("@actions/core");
const tc = require("@actions/tool-cache");
// The name of the tool we are installing with this action.
const toolName = "dapr";
// Base form of the the URL to download the release archives. As long as this
// does not change this will be able to download any version the CLI.
const baseURL = "https://github.com/dapr/cli/releases/download/v";
// Returns the URL used to download a specific version of the Dapr CLI for a
// specific platform.
function getDaprDownloadURL(currentOs, version) {
var file = "";
switch (currentOs) {
case "Linux":
file = "dapr_linux_amd64.tar.gz";
break;
case "Darwin":
file = "dapr_darwin_amd64.tar.gz";
break;
case "Windows_NT":
default:
file = "dapr_windows_amd64.zip";
break;
}
return util.format("%s%s/%s", baseURL, version, file);
}
// Downloads and extracts the archive to the runner and returns the path.
async function downloadDapr(currentOs, version) {
// See if we have cached this tool already
let cachedToolPath = tc.find(toolName, version);
// If we did not find the tool in the cache download it now.
if (!cachedToolPath) {
let downloadPath;
let downloadUrl = getDaprDownloadURL(currentOs, version);
try {
core.info(`Downloading Dapr from ${downloadUrl}...`);
downloadPath = await tc.downloadTool(downloadUrl);
} catch (exception) {
throw new Error(
util.format("Failed to download Dapr from location", downloadUrl)
);
}
// (chmod a+rwx) sets permissions so that, User / owner can read, can
// write and can execute. Group can read, can write and can execute.
// Others can read, can write and can execute.
fs.chmodSync(downloadPath, "777");
// Stores the path where the archive was extracted
let installedToolPath;
if (currentOs === "Windows_NT") {
installedToolPath = await tc.extractZip(downloadPath);
} else {
// Both Linux and macOS use a .tar.gz file
installedToolPath = await tc.extractTar(downloadPath);
}
// Cache to tool so we do not have to download multiple times
cachedToolPath = await tc.cacheDir(installedToolPath, toolName, version);
}
// Get the full path to the executable
const toolPath = findTool(currentOs, cachedToolPath);
if (!toolPath) {
throw new Error(
util.format("Dapr executable not found in path", cachedToolPath)
);
}
core.info(`Dapr installed to ${toolPath}...`);
// (chmod a+rwx) sets permissions so that, User / owner can read, can
// write and can execute. Group can read, can write and can execute.
// Others can read, can write and can execute.
fs.chmodSync(toolPath, "777");
return toolPath;
}
// Returns a install path of the desired tool
function findTool(currentOs, rootFolder) {
fs.chmodSync(rootFolder, "777");
// Holds all the paths. The tool might be installed in multiple locations.
var fileList;
// walkSync is recursive which is why we pass in fileList and assign it the
// return value of this function.
fileList = walkSync(
rootFolder,
fileList,
toolName + getExecutableExtension(currentOs)
);
if (!fileList || fileList.length == 0) {
throw new Error(
util.format("Dapr executable not found in path", rootFolder)
);
} else {
// Return the first one we find.
return fileList[0];
}
}
// Returns the full name of the executable with extension if any. On Linux and
// macOS the executable does not have an extension but on Windows it does.
function getExecutableExtension(currentOs) {
return currentOs.match(/^Win/) ? ".exe" : "";
}
// Returns a list of path to the fileToFind in the dir provided.
function walkSync(dir, fileList, fileToFind) {
var files = fs.readdirSync(dir);
fileList = fileList || [];
files.forEach(function (file) {
if (fs.statSync(path.join(dir, file)).isDirectory()) {
fileList = walkSync(path.join(dir, file), fileList, fileToFind);
} else {
core.debug(file);
if (file == fileToFind) {
fileList.push(path.join(dir, file));
}
}
});
return fileList;
}
// The main function of this action. After the archive is downloaded and
// extracted this function adds it location to the path. This will make sure
// other steps in your workflow will be able to call the Dapr CLI.
async function run(currentOs, version) {
let cachedPath = await downloadDapr(currentOs, version);
if (!process.env["PATH"].startsWith(path.dirname(cachedPath))) {
core.addPath(path.dirname(cachedPath));
}
console.log(
`Dapr CLI version: '${version}' has been cached at ${cachedPath}`
);
// set a an output of this action incase future steps need the path to the tool.
core.setOutput("dapr-path", cachedPath);
}
module.exports = {
run: run,
downloadDapr: downloadDapr,
getDaprDownloadURL: getDaprDownloadURL,
};