Skip to content
This repository was archived by the owner on Apr 13, 2020. It is now read-only.

Commit aa4cb12

Browse files
authored
[FEATURE] Inherit value from config (#488)
* [FEATURE] Automate inheriting values into option values from config.yaml * Update commandBuilder.ts
1 parent 6b68c46 commit aa4cb12

File tree

7 files changed

+278
-68
lines changed

7 files changed

+278
-68
lines changed

docs/commands/data.json

+6
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,13 @@
175175
{
176176
"arg": "-s, --storage-account-name <storage-account-name>",
177177
"description": "Azure storage account name",
178+
"inherit": "introspection.azure.account_name",
178179
"required": true
179180
},
180181
{
181182
"arg": "-t, --storage-table-name <storage-table-name>",
182183
"description": "Azure storage table name",
184+
"inherit": "introspection.azure.table_name",
183185
"required": true
184186
},
185187
{
@@ -194,21 +196,25 @@
194196
{
195197
"arg": "--service-principal-id <service-principal-id>",
196198
"description": "Azure service principal id with `contributor` role in Azure Resource Group",
199+
"inherit": "introspection.azure.service_principal_id",
197200
"required": true
198201
},
199202
{
200203
"arg": "--service-principal-password <service-principal-password>",
201204
"description": "The Azure service principal password",
205+
"inherit": "introspection.azure.service_principal_secret",
202206
"required": true
203207
},
204208
{
205209
"arg": "--tenant-id <tenant-id>",
206210
"description": "The Azure AD tenant id of service principal",
211+
"inherit": "introspection.azure.tenant_id",
207212
"required": true
208213
},
209214
{
210215
"arg": "--subscription-id <subscription-id>",
211216
"description": "The Azure subscription id",
217+
"inherit": "introspection.azure.subscription_id",
212218
"required": true
213219
}
214220
],

docs/commands/spk.js

+38-26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable */
12
var data = null;
23
var filter = "";
34
var converter = new showdown.Converter();
@@ -8,7 +9,9 @@ var sepVersion = "@";
89
var template =
910
'<p class="cmd-title">@@main-cmd@@</p><p class="cmd-description">@@cmd-description@@</p><p>&nbsp;</p><p>Options:</p>@@options@@<p>&nbsp;</p>';
1011
var optionTemplate =
11-
'<p>@@option@@</p><p class="cmd-description">@@description@@</p><div class="line-space"></div>';
12+
'<p>@@option@@</p><p class="cmd-description">@@description@@</p>@@inherit@@<div class="line-space"></div>';
13+
var inheritTemplate =
14+
'<p class="cmd-inherit">inherit @@inherit@@ from spk config.yaml</p>';
1215
var relTemplate =
1316
'<li><a class="preserve-view button is-small has-border-none has-inner-focus has-flex-justify-content-start is-full-width has-text-wrap is-text-left">@@value@@</a></li>';
1417

@@ -19,18 +22,18 @@ function sanitize(str) {
1922
function getExistingVersions() {
2023
$.ajax({
2124
url: "releases.txt",
22-
success: function(result) {
23-
result.split("\n").forEach(function(r) {
25+
success: function (result) {
26+
result.split("\n").forEach(function (r) {
2427
var rTrim = r.trim();
2528
if (rTrim && releases.indexOf(rTrim) === -1) {
2629
releases.push(rTrim);
2730
}
2831
});
29-
releases.sort(function(a, b) {
32+
releases.sort(function (a, b) {
3033
return a > b ? -1 : 1;
3134
});
3235
},
33-
async: false
36+
async: false,
3437
});
3538
}
3639

@@ -56,8 +59,8 @@ function populateVersionList() {
5659
return a + relTemplate.replace("@@value@@", c);
5760
}, "")
5861
);
59-
oSelect.find("li").each(function(i, elm) {
60-
$(elm).on("click", function(evt) {
62+
oSelect.find("li").each(function (i, elm) {
63+
$(elm).on("click", function (evt) {
6164
evt.stopPropagation();
6265
oSelect.css("display", "none");
6366
var ver = $(this).text();
@@ -91,15 +94,27 @@ function showDetails(key) {
9194
);
9295
content = content.replace("@@cmd-description@@", cmd.description);
9396

94-
var options = (cmd.options || []).reduce(function(a, c) {
95-
a += optionTemplate
97+
var options = (cmd.options || []).reduce(function (a, c) {
98+
var o = optionTemplate
9699
.replace("@@option@@", sanitize(c.arg))
97100
.replace("@@description@@", sanitize(c.description));
101+
102+
if (c.inherit) {
103+
o = o.replace(
104+
"@@inherit@@",
105+
inheritTemplate.replace("@@inherit@@", c.inherit)
106+
);
107+
} else {
108+
o = o.replace("@@inherit@@", "");
109+
}
110+
111+
a += o;
98112
return a;
99113
}, "");
100114
options += optionTemplate
101115
.replace("@@option@@", "-h, --help")
102-
.replace("@@description@@", "output usage information");
116+
.replace("@@description@@", "output usage information")
117+
.replace("@@inherit@@", "");
103118

104119
content = content.replace("@@options@@", options);
105120

@@ -121,11 +136,11 @@ function showDetails(key) {
121136
function populateListing() {
122137
var cmdKeys = Object.keys(data);
123138
if (filter) {
124-
cmdKeys = cmdKeys.filter(function(k) {
139+
cmdKeys = cmdKeys.filter(function (k) {
125140
return k.indexOf(filter) !== -1;
126141
});
127142
}
128-
var listing = cmdKeys.reduce(function(a, c) {
143+
var listing = cmdKeys.reduce(function (a, c) {
129144
a +=
130145
"<li><a href=\"javascript:showDetails('" +
131146
c +
@@ -159,32 +174,29 @@ function populateListing() {
159174
}
160175
}
161176

162-
var subheaderItems = function() {
163-
$("#item_share").click(function(evt) {
177+
var subheaderItems = function () {
178+
$("#item_share").click(function (evt) {
164179
evt.stopPropagation();
165180
$("#sharing-menu").css("display", "block");
166181
});
167-
$("body").click(function() {
182+
$("body").click(function () {
168183
$("#sharing-menu").css("display", "none");
169184
});
170-
$("#item_contribute").click(function(evt) {
185+
$("#item_contribute").click(function (evt) {
171186
var win = window.open("https://github.com/CatalystCode/spk", "_blank");
172187
win.focus();
173188
});
174189
};
175190

176191
function loadCommands() {
177192
var url = version === "master" ? "./data.json" : "./data" + version + ".json";
178-
$.getJSON(url, function(json) {
193+
$.getJSON(url, function (json) {
179194
data = json;
180195
subheaderItems();
181196
populateListing();
182197

183-
$("#commandfilter").on("input", function() {
184-
filter = $(this)
185-
.val()
186-
.trim()
187-
.toLowerCase();
198+
$("#commandfilter").on("input", function () {
199+
filter = $(this).val().trim().toLowerCase();
188200
populateListing();
189201
});
190202
});
@@ -206,15 +218,15 @@ function showReleaseSelector(bShow) {
206218
}
207219
}
208220

209-
$(function() {
210-
$("#btnSelectRelease").on("click", function(evt) {
221+
$(function () {
222+
$("#btnSelectRelease").on("click", function (evt) {
211223
evt.stopPropagation();
212224
showReleaseSelector();
213225
});
214-
$(document.body).on("click", function() {
226+
$(document.body).on("click", function () {
215227
showReleaseSelector(false);
216228
});
217-
$(document).keyup(function(evt) {
229+
$(document).keyup(function (evt) {
218230
if (evt.keyCode === 27) {
219231
showReleaseSelector(false);
220232
}

docs/commands/styles/spk.css

+4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ a:visited {
9898
.cmd-description {
9999
margin-left: 16px;
100100
}
101+
.cmd-inherit {
102+
margin-left: 16px;
103+
color: #666;
104+
}
101105
.markdown {
102106
padding: 20px;
103107
margin-bottom: 20px;

src/commands/deployment/onboard.decorator.json

+6
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
{
77
"arg": "-s, --storage-account-name <storage-account-name>",
88
"description": "Azure storage account name",
9+
"inherit": "introspection.azure.account_name",
910
"required": true
1011
},
1112
{
1213
"arg": "-t, --storage-table-name <storage-table-name>",
1314
"description": "Azure storage table name",
15+
"inherit": "introspection.azure.table_name",
1416
"required": true
1517
},
1618
{
@@ -25,21 +27,25 @@
2527
{
2628
"arg": "--service-principal-id <service-principal-id>",
2729
"description": "Azure service principal id with `contributor` role in Azure Resource Group",
30+
"inherit": "introspection.azure.service_principal_id",
2831
"required": true
2932
},
3033
{
3134
"arg": "--service-principal-password <service-principal-password>",
3235
"description": "The Azure service principal password",
36+
"inherit": "introspection.azure.service_principal_secret",
3337
"required": true
3438
},
3539
{
3640
"arg": "--tenant-id <tenant-id>",
3741
"description": "The Azure AD tenant id of service principal",
42+
"inherit": "introspection.azure.tenant_id",
3843
"required": true
3944
},
4045
{
4146
"arg": "--subscription-id <subscription-id>",
4247
"description": "The Azure subscription id",
48+
"inherit": "introspection.azure.subscription_id",
4349
"required": true
4450
}
4551
]

src/commands/deployment/onboard.ts

+3-28
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import {
1313
build as buildCmd,
1414
exit as exitCmd,
15+
populateInheritValueFromConfig,
1516
validateForRequiredValues,
1617
} from "../../lib/commandBuilder";
1718
import { logger } from "../../logger";
@@ -50,22 +51,7 @@ export interface OnBoardConfig {
5051
* @param opts values from commander
5152
*/
5253
export const populateValues = (opts: CommandOptions): CommandOptions => {
53-
const config = Config();
54-
const azure = config.introspection ? config.introspection.azure : undefined;
55-
56-
opts.storageAccountName =
57-
opts.storageAccountName || azure?.account_name || undefined;
58-
opts.storageTableName =
59-
opts.storageTableName || azure?.table_name || undefined;
60-
opts.servicePrincipalId =
61-
opts.servicePrincipalId || azure?.service_principal_id || undefined;
62-
opts.servicePrincipalPassword =
63-
opts.servicePrincipalPassword ||
64-
azure?.service_principal_secret ||
65-
undefined;
66-
opts.tenantId = opts.tenantId || azure?.tenant_id || undefined;
67-
opts.subscriptionId =
68-
opts.subscriptionId || azure?.subscription_id || undefined;
54+
populateInheritValueFromConfig(decorator, Config(), opts);
6955
return opts;
7056
};
7157

@@ -75,18 +61,7 @@ export const populateValues = (opts: CommandOptions): CommandOptions => {
7561
* @param opts values from commander (including populated values from spk config)
7662
*/
7763
export const validateValues = (opts: CommandOptions): OnBoardConfig => {
78-
const errors = validateForRequiredValues(decorator, {
79-
servicePrincipalId: opts.servicePrincipalId,
80-
servicePrincipalPassword: opts.servicePrincipalPassword,
81-
storageAccountName: opts.storageAccountName,
82-
storageResourceGroupName: opts.storageResourceGroupName,
83-
storageTableName: opts.storageTableName,
84-
subscriptionId: opts.subscriptionId,
85-
tenantId: opts.tenantId,
86-
});
87-
if (errors.length > 0) {
88-
throw Error("Required values are missing");
89-
}
64+
validateForRequiredValues(decorator, opts, true);
9065

9166
// validateForRequiredValues already check
9267
// opts.storageAccountName and opts.storageTableName are not empty string

0 commit comments

Comments
 (0)