-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
94 lines (75 loc) · 2.08 KB
/
script.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require("./utils/env")
const fs = require('fs');
const yaml = require('js-yaml')
const axios = require('axios');
try {
validateArguments()
const application_arg = process.argv[2]
const file_arg = process.argv[3]
const { id, name, description, preffix, variables } = getApplicationYaml(application_arg, file_arg)
const obj = buildRequestVariables(preffix, variables)
updateVariable(id, name, description, obj)
} catch(e) {
console.error(e)
}
function validateArguments() {
let application_arg = process.argv[2]
let file_arg = process.argv[3]
if(!application_arg) {
throw new Error('Application argument is required.')
}
if(!file_arg) {
throw new Error('Yaml file argument is required.')
}
}
function getApplicationYaml(applicationName, file_arg) {
try {
const yamlFilePath = './libraries/' + applicationName + '/' + file_arg + '.yaml'
const libYamlContents = fs.readFileSync(yamlFilePath, 'utf8')
return yaml.load(libYamlContents)
} catch(e) {
console.log(e)
process.exit()
}
}
function buildRequestVariables(preffix, variables) {
let obj = {};
for(const variable of variables) {
let name = ''
if(variable.disablePreffix) {
name = variable.name
} else {
name = preffix ? preffix + variable.name : variable.name
}
obj[name] = {
value: variable.isSecret ? null : variable.value
}
if(variable.isSecret) {
obj[name].isSecret = true
}
}
return obj
}
function updateVariable(id, name, description, variables) {
const obj = {
name,
variables
}
if(description) {
obj['description'] = description
}
const tokenBase64 = Buffer.from(`${process.env.AZURE_TOKEN}:`, 'utf8').toString('base64')
const headers = {
'Authorization': `Basic ${tokenBase64}`
}
const options = {
headers
}
console.log(obj)
axios.put(`https://${process.env.AZURE_DNS}/${process.env.AZURE_PROJECT}/_apis/distributedtask/variablegroups/${id}?api-version=5.0-preview.1`, obj, options)
.then(res => {
if(res.status === 200) {
console.log('Variables updated!')
}
})
}