Skip to content

Commit c1e4716

Browse files
authored
Merge pull request #12 from IBM/refactor_functions
Refactor run method into functions
2 parents d1293bb + 4b8fb53 commit c1e4716

File tree

3 files changed

+169
-129
lines changed

3 files changed

+169
-129
lines changed

dist/index.js

Lines changed: 84 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.js

Lines changed: 84 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,80 +12,100 @@ function colorize(string, color) {
1212
return `\u001b[${code};1m${string}\u001b[0m`
1313
}
1414

15-
/**
16-
* The main function for the action.
17-
* @returns {Promise<void>} Resolves when the action is complete.
18-
*/
19-
async function run() {
20-
try {
21-
core.startGroup('Installing IBM Cloud CLI')
22-
if (process.platform == 'win32') {
23-
await exec.exec(`powershell -command "iex (New-Object Net.WebClient).DownloadString('https://clis.cloud.ibm.com/install/powershell')"`)
24-
// Add to PATH for the current step
25-
process.env.PATH += ';C:\\Program Files\\IBM\\Cloud\\bin'
26-
// Add to GITHUB_PATH for future steps
27-
await exec.exec(`powershell -command "Add-Content $env:GITHUB_PATH 'C:\\Program Files\\IBM\\Cloud\\bin'"`)
28-
} else if (process.platform == 'darwin') {
29-
await exec.exec('/bin/bash -c "curl -fsSL https://clis.cloud.ibm.com/install/osx | sh"')
30-
} else {
31-
await exec.exec('/bin/bash -c "curl -fsSL https://clis.cloud.ibm.com/install/linux | sh"')
32-
}
33-
core.endGroup()
34-
35-
core.startGroup('Disable version checking')
36-
await exec.exec('ibmcloud', ['config', '--check-version=false'])
37-
core.endGroup()
15+
async function installCLI() {
16+
core.startGroup('Installing IBM Cloud CLI')
17+
if (process.platform == 'win32') {
18+
await exec.exec(`powershell -command "iex (New-Object Net.WebClient).DownloadString('https://clis.cloud.ibm.com/install/powershell')"`)
19+
// Add to PATH for the current step
20+
process.env.PATH += ';C:\\Program Files\\IBM\\Cloud\\bin'
21+
// Add to GITHUB_PATH for future steps
22+
await exec.exec(`powershell -command "Add-Content $env:GITHUB_PATH 'C:\\Program Files\\IBM\\Cloud\\bin'"`)
23+
} else if (process.platform == 'darwin') {
24+
await exec.exec('/bin/bash -c "curl -fsSL https://clis.cloud.ibm.com/install/osx | sh"')
25+
} else {
26+
await exec.exec('/bin/bash -c "curl -fsSL https://clis.cloud.ibm.com/install/linux | sh"')
27+
}
28+
core.endGroup()
29+
}
3830

39-
// Capture the version output
40-
let version = ''
41-
await exec.exec('ibmcloud', ['--version'], {
42-
listeners: {stdout: (data) => { version += data.toString() }}
43-
});
44-
version = version.match(/\d+\.\d+\.\d+/)?.[0]
45-
core.setOutput('version', version)
31+
async function disableVersionChecking() {
32+
core.startGroup('Disable version checking')
33+
await exec.exec('ibmcloud', ['config', '--check-version=false'])
34+
core.endGroup()
35+
}
4636

47-
const plugins = core.getInput('plugins').replace('\n', ' ').replace(',', ' ').split(' ').map(p => p.trim()).filter(p => p)
48-
if (plugins.length > 0) {
49-
core.startGroup('Installing IBM Cloud CLI plugins')
50-
await exec.exec('ibmcloud', ['plugin', 'install', ...plugins])
51-
core.endGroup()
52-
await exec.exec('ibmcloud', ['plugin', 'list'])
53-
}
37+
async function captureVersion() {
38+
let version = ''
39+
await exec.exec('ibmcloud', ['--version'], {
40+
listeners: {stdout: (data) => { version += data.toString() }}
41+
});
42+
version = version.match(/\d+\.\d+\.\d+/)?.[0]
43+
core.setOutput('version', version)
44+
}
5445

55-
core.startGroup('Set API endpoint')
56-
const api = core.getInput('api')
57-
await exec.exec('ibmcloud', ['api', api])
46+
async function installPlugins() {
47+
const plugins = core.getInput('plugins').replace('\n', ' ').replace(',', ' ').split(' ').map(p => p.trim()).filter(p => p)
48+
if (plugins.length > 0) {
49+
core.startGroup('Installing IBM Cloud CLI plugins')
50+
await exec.exec('ibmcloud', ['plugin', 'install', ...plugins])
5851
core.endGroup()
52+
await exec.exec('ibmcloud', ['plugin', 'list'])
53+
}
54+
}
55+
56+
async function setApiEndpoint() {
57+
core.startGroup('Set API endpoint')
58+
const api = core.getInput('api')
59+
await exec.exec('ibmcloud', ['api', api])
60+
core.endGroup()
61+
}
5962

60-
const apiKey = core.getInput('api_key')
63+
async function login() {
64+
const apiKey = core.getInput('api_key')
65+
if (apiKey.length > 0) {
66+
const api = core.getInput('api')
6167
const region = core.getInput('region')
6268
const group = core.getInput('group')
63-
if (apiKey.length > 0) {
64-
core.startGroup('Login to IBM Cloud')
6569

66-
// Mimic the ibmcloud login output since we will run it silent later
67-
core.info(`[command]/usr/local/bin/ibmcloud login -r ${region} -g ${group}`)
68-
core.info(`API endpoint: ${colorize(api, 'cyan')}`)
69-
core.info('Logging in with API key from environment variable...')
70-
core.info('Authenticating...')
70+
core.startGroup('Login to IBM Cloud')
7171

72-
try {
73-
await exec.exec('ibmcloud', ['login', '-r', region, '-g', group], {
74-
env: {
75-
'IBMCLOUD_API_KEY': apiKey,
76-
...process.env
77-
},
78-
silent: true // Intentionally suppress the output to avoid the ibmcloud target at the end of login
79-
})
80-
core.info(colorize('OK', 'green'))
81-
} catch(e) {
82-
core.info(colorize('FAILED', 'red'))
83-
core.info('Unable to authenticate')
84-
throw e
85-
} finally {
86-
core.endGroup()
87-
}
72+
// Mimic the ibmcloud login output since we will run it silent later
73+
core.info(`[command]/usr/local/bin/ibmcloud login -r ${region} -g ${group}`)
74+
core.info(`API endpoint: ${colorize(api, 'cyan')}`)
75+
core.info('Logging in with API key from environment variable...')
76+
core.info('Authenticating...')
77+
78+
try {
79+
await exec.exec('ibmcloud', ['login', '-r', region, '-g', group], {
80+
env: {
81+
'IBMCLOUD_API_KEY': apiKey,
82+
...process.env
83+
},
84+
silent: true // Intentionally suppress the output to avoid the ibmcloud target at the end of login
85+
})
86+
core.info(colorize('OK', 'green'))
87+
} catch(e) {
88+
core.info(colorize('FAILED', 'red'))
89+
core.info('Unable to authenticate')
90+
throw e
91+
} finally {
92+
core.endGroup()
8893
}
94+
}
95+
}
96+
97+
/**
98+
* The main function for the action.
99+
* @returns {Promise<void>} Resolves when the action is complete.
100+
*/
101+
async function run() {
102+
try {
103+
await installCLI()
104+
await disableVersionChecking()
105+
await captureVersion()
106+
await installPlugins()
107+
await setApiEndpoint()
108+
await login()
89109
} catch (error) {
90110
// Fail the workflow run if an error occurs
91111
core.setFailed(error.message)

0 commit comments

Comments
 (0)