Initial version of setup-ossutil

This commit is contained in:
Rimo
2020-01-15 16:36:04 +08:00
parent df25824a4d
commit e0feee7e7c
12 changed files with 4415 additions and 309 deletions

78
src/installer.ts Normal file
View File

@@ -0,0 +1,78 @@
import * as core from '@actions/core'
import * as tc from '@actions/tool-cache'
import * as path from 'path'
const TOOL_NAME = 'ossutil'
/**
* Get ossutil ready for use
* @param version the version of ossutil
*/
export async function getOssutil(version: string): Promise<void> {
let toolPath = tc.find(TOOL_NAME, version)
if (!toolPath) {
// download, extract, cache
toolPath = await acquireOssutil(version)
}
core.info(`ossutil is cached under ${toolPath}`)
core.addPath(toolPath)
}
/**
* Acquire ossutil and install it into the tool cache
* @param version the version of ossutil to acquire
* @returns the path to the tool directory
*/
async function acquireOssutil(version: string): Promise<string> {
// download
let downloadFile: string
try {
const downloadUrl = getDownloadUrl(version)
core.info(`Downloading ossutil from: ${downloadUrl}`)
downloadFile = await tc.downloadTool(downloadUrl)
} catch (error) {
core.error(error)
throw new Error('Failed to download ossutil')
}
core.debug(`ossutil downloaded to: ${downloadFile}`)
// extract (if needed)
let toolFile = downloadFile
if (process.platform === 'win32') {
const extractFolder = await tc.extractZip(downloadFile)
toolFile = path.join(extractFolder, 'ossutil64', 'ossutil64.exe')
core.debug(`ossutil extracted to: ${toolFile}`)
}
// cache
const fileName = process.platform === 'win32' ? 'ossutil.exe' : 'ossutil'
const toolPath = await tc.cacheFile(toolFile, fileName, TOOL_NAME, version)
core.debug(`ossutil cached to: ${toolPath}`)
return toolPath
}
/**
* Get the URL of the specific version of ossutil
* @param version the version of ossutil to download
* @returns the URL
*/
function getDownloadUrl(version: string): string {
let downloadUrl = `http://gosspublic.alicdn.com/ossutil/${version}/`
switch (process.platform) {
case 'linux':
downloadUrl += 'ossutil64'
break
case 'win32':
downloadUrl += 'ossutil64.zip'
break
case 'darwin':
downloadUrl += 'ossutilmac64'
break
default:
throw new Error(`Unknown platform ${process.platform}`)
}
return downloadUrl
}

View File

@@ -1,19 +0,0 @@
import * as core from '@actions/core'
import {wait} from './wait'
async function run(): Promise<void> {
try {
const ms: string = core.getInput('milliseconds')
core.debug(`Waiting ${ms} milliseconds ...`)
core.debug(new Date().toTimeString())
await wait(parseInt(ms, 10))
core.debug(new Date().toTimeString())
core.setOutput('time', new Date().toTimeString())
} catch (error) {
core.setFailed(error.message)
}
}
run()

32
src/setup-ossutil.ts Normal file
View File

@@ -0,0 +1,32 @@
import * as core from '@actions/core'
import * as exec from '@actions/exec'
import * as installer from './installer'
async function run(): Promise<void> {
try {
// download
const version = core.getInput('ossutil-version')
await installer.getOssutil(version)
// config
const endpoint = core.getInput('endpoint')
const accessKeyId = core.getInput('access-key-id')
const accessKeySecret = core.getInput('access-key-secret')
const stsToken = core.getInput('sts-token')
const args = [
'--endpoint',
endpoint,
'--access-key-id',
accessKeyId,
'--access-key-secret',
accessKeySecret,
'--sts-token',
stsToken
]
await exec.exec('ossutil', args)
} catch (error) {
core.setFailed(error.message)
}
}
run()

View File

@@ -1,9 +0,0 @@
export async function wait(milliseconds: number): Promise<string> {
return new Promise(resolve => {
if (isNaN(milliseconds)) {
throw new Error('milliseconds not a number')
}
setTimeout(() => resolve('done!'), milliseconds)
})
}