-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.js
More file actions
161 lines (143 loc) · 4.5 KB
/
main.js
File metadata and controls
161 lines (143 loc) · 4.5 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
import core from '@actions/core'
import tool from '@actions/tool-cache'
import exec from '@actions/exec'
import path from 'node:path'
import fs from 'node:fs'
const DEFAULT_VERSION = `v0.113.0`
// get the Updatecli version from the action inputs
export async function getUpdatecliVersion() {
const versionInput = core.getInput('version', {required: false})
const versionFile = core.getInput('version-file', {required: false})
let version = versionInput
if (!versionInput && !versionFile) {
core.info(`Set default value for version to ${DEFAULT_VERSION}`)
version = DEFAULT_VERSION
}
if (!version && versionFile) {
version = await getVersionFromFileContent(versionFile)
if (!version) {
throw new Error(`No supported version was found in file ${versionFile}`)
}
}
return version
}
export async function updatecliExtract(downloadPath, downloadUrl) {
if (downloadUrl.endsWith('.tar.gz')) {
return tool.extractTar(downloadPath)
} else if (downloadUrl.endsWith('.zip')) {
return tool.extractZip(downloadPath)
} else {
throw new Error(`Unsupported archive type: ${downloadUrl}`)
}
}
// download Updatecli retrieve updatecli binary from Github Release
export async function updatecliDownload(version) {
if (!version) {
throw new Error(`No supported version was found`)
}
const updatecliPackages = [
{
arch: 'x64',
platform: 'linux',
url: `https://github.com/updatecli/updatecli/releases/download/${version}/updatecli_Linux_x86_64.tar.gz`,
},
{
arch: 'arm64',
platform: 'linux',
url: `https://github.com/updatecli/updatecli/releases/download/${version}/updatecli_Linux_arm64.tar.gz`,
},
{
arch: 'x64',
platform: 'win32',
url: `https://github.com/updatecli/updatecli/releases/download/${version}/updatecli_Windows_x86_64.zip`,
},
{
arch: 'arm64',
platform: 'win32',
url: `https://github.com/updatecli/updatecli/releases/download/${version}/updatecli_Windows_arm64.zip`,
},
{
arch: 'x64',
platform: 'darwin',
url: `https://github.com/updatecli/updatecli/releases/download/${version}/updatecli_Darwin_x86_64.tar.gz`,
},
{
arch: 'arm64',
platform: 'darwin',
url: `https://github.com/updatecli/updatecli/releases/download/${version}/updatecli_Darwin_arm64.tar.gz`,
},
]
const updatecliPackage = updatecliPackages.find(
x => x.platform == process.platform && x.arch == process.arch
)
if (!updatecliPackage) {
throw new Error(
`Unsupported platform ${process.platform} and arch ${process.arch}`
)
}
core.info(`Downloading ${updatecliPackage.url}`)
const downloadPath = await tool.downloadTool(updatecliPackage.url)
core.debug(`Extracting file ${downloadPath} ...`)
const updatecliExtractedFolder = await updatecliExtract(
downloadPath,
updatecliPackage.url
)
core.debug(`Extracted file to ${updatecliExtractedFolder} ...`)
core.debug('Adding to the cache ...')
const cachedPath = await tool.cacheDir(
updatecliExtractedFolder,
'updatecli',
version,
process.arch
)
if (process.platform == 'linux' || process.platform == 'darwin') {
await exec.exec('chmod', ['+x', path.join(cachedPath, 'updatecli')])
}
core.addPath(cachedPath)
core.info(`Downloaded to ${cachedPath}`)
}
export async function updatecliVersion() {
core.info('Show Updatecli version')
await exec.exec('updatecli version')
}
export async function run() {
try {
const version = await getUpdatecliVersion()
await updatecliDownload(version)
await updatecliVersion()
process.exitCode = core.ExitCode.Success
} catch (error) {
core.setFailed(error.message)
}
}
export async function getVersionFromFileContent(versionFile) {
if (!versionFile) {
return
}
let versionRegExp
const versionFileName = path.basename(versionFile)
if (versionFileName == '.tool-versions') {
versionRegExp = /^(updatecli\s+)(?:\S*-)?(?<version>v(\d+)(\.\d+)(\.\d+))$/m
} else if (versionFileName) {
versionRegExp = /(?<version>(v\d+\S*))(\s|$)/
} else {
return
}
try {
const content = fs.readFileSync(versionFile).toString().trim()
let fileContent = ''
if (content.match(versionRegExp)?.groups?.version) {
fileContent = content.match(versionRegExp)?.groups?.version
}
if (!fileContent) {
return
}
core.debug(`Version from file '${fileContent}'`)
return fileContent
} catch (error) {
if (error.code === 'ENOENT') {
return
}
throw error
}
}