forked from little-core-labs/netrc-creds
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
78 lines (61 loc) · 2.2 KB
/
index.js
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
const core = require('@actions/core')
const assert = require('nanoassert')
const fs = require('fs')
const exec = require('child_process').exec
const path = require('path')
const os = require('os')
const machine = core.getInput('machine')
if (machine) assert(typeof machine === 'string', 'machine input must be a string')
const login = core.getInput('login')
if (login) assert(typeof login === 'string', 'login input must be a string')
const password = core.getInput('password')
if (password) assert(typeof password === 'string', 'password input must be a string')
const credsInput = core.getInput('creds')
const creds = credsInput ? JSON.parse(credsInput) : []
assert(Array.isArray(creds), 'creds input must be an array')
assert(creds || machine || login || password, 'you must pass a machine,login,password combo, or a creds JSON field.')
let credsString = ''
if (machine || login || password) {
assert(password != null, 'password must be defined')
assert(login != null, 'login must be defined')
assert(machine != null, 'machine must be defined')
let credString = ''
credString += `machine ${machine}\n`
credString += `login ${login}\n`
credString += `password ${password}\n`
credString += '\n'
credsString += credString
}
creds.forEach(cred => {
assert(cred.machine, 'cred has a machine field')
assert(cred.login, 'cred has a login field')
assert(cred.password, 'cred has a password field')
let credString = ''
credString += `machine ${cred.machine}\n`
credString += `login ${cred.login}\n`
credString += `password ${cred.password}\n`
credString += '\n'
credsString += credString
})
const netrc = path.resolve(os.homedir(), '.netrc')
exec(`touch ${netrc}`, (error, stdout, stderr) => {
console.log(stdout)
console.error(stderr)
if (error !== null) {
console.error(`exec error: ${error}`)
}
exec(`chmod 600 ${netrc}`, (error, stdout, stderr) => {
console.log(stdout)
console.error(stderr)
if (error !== null) {
console.error(`exec error: ${error}`)
}
fs.appendFile(netrc, credsString, err => {
if (err) {
console.error(err)
return core.setFailed(err.message)
}
console.log('wrote credentials to ~/.netrc')
})
})
})