Skip to content

Commit 86cc186

Browse files
add option for order categories (#161)
1 parent 1c5a315 commit 86cc186

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+206
-107
lines changed

docs/content/more/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
+++
22
title = "More..."
3-
weight = 110
3+
weight = 130
44
+++
55

66

docs/content/operate on resources/_index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
+++
22
title = "Operate On Resources"
3-
weight = 30
3+
weight = 60
44
+++
55

66
The CLI supports the ability to work with `spec` files when working with resources.<br>

docs/index.js

Lines changed: 85 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ const TEMPLATE_DIR = path.resolve(__dirname);
1313
const FILES_TO_IGNORE = ['index.js', 'content/pipelines v2/_index.md', 'content/pipelines v2/spec.md'];
1414
const baseDir = path.resolve(TEMP_DIR, './content');
1515
const ALLOW_BETA_COMMANDS = process.env.ALLOW_BETA_COMMANDS;
16+
const categoriesOrder = {
17+
authentication: 30,
18+
pipelines : 40,
19+
builds: 50,
20+
contexts : 70 ,
21+
images : 80 ,
22+
triggers : 90,
23+
environments : 100 ,
24+
compositions : 110 ,
25+
'predefined pipelines': 120,
26+
27+
};
28+
1629

1730
const hasSubCommands = async (command) => {
1831
const files = await recursive(path.resolve(__dirname, '../lib/interface/cli/commands'));
@@ -77,7 +90,26 @@ const copyTemplateToTmp = async () => {
7790
* in case the file already exists in the base docs folder it will extend it
7891
* possible extensions are: HEADER, DESCRIPTION, COMMANDS, ARGUMENTS, OPTIONS
7992
*/
80-
const createCommandFile = async (nestedCategory,docs) => {
93+
const getWeight = async (command) => {
94+
const docs = command.prepareDocs();
95+
let weight = 0;
96+
if (docs.weight) {
97+
return docs.weight;
98+
}
99+
else {
100+
let parent = command.getParentCommand();
101+
while (parent && !parent.prepareDocs().weight) {
102+
parent = parent.getParentCommand();
103+
}
104+
if (parent) {
105+
weight = parent.prepareDocs().weight;
106+
}
107+
return weight;
108+
}
109+
};
110+
111+
const createCommandFile = async (nestedCategory,command) => {
112+
const docs = command.prepareDocs();
81113
const dir = path.resolve(baseDir, `${(nestedCategory || 'undefined').toLowerCase()}`);
82114

83115
const commandFilePath = path.resolve(dir, `./${docs.title}.md`);
@@ -90,10 +122,11 @@ const createCommandFile = async (nestedCategory,docs) => {
90122

91123
// HEADER STRING
92124
let headerString;
125+
const weight = await getWeight(command);
93126
if (docs.subCategory) {
94-
headerString = `+++\ntitle = "${docs.subCategory}"\n+++\n\n`;
127+
headerString = `+++\ntitle = "${docs.subCategory}"\nweight = ${weight || 100}\n+++\n\n`;
95128
} else {
96-
headerString = `${docs.header}\n\n`;
129+
headerString = `+++\ntitle = "${docs.title}"\nweight = ${weight || 100}\n+++\n\n`;
97130
}
98131

99132
if (skeletonFileExists) {
@@ -176,17 +209,10 @@ const createCategoryFile = async (content, category) => {
176209
* updates the category main file with a specific command
177210
* possible extensions are: HEADER, COMMANDS
178211
*/
179-
const updateCategoryFileContent = async (nestedCategory,command, existingContent) => {
212+
const updateCategoryFileArray = async (nestedCategory,command, existingContent) => {
180213
const docs = command.prepareDocs();
181214
const { category } = docs;
182-
let finalCategoryFileString = existingContent || "";
183-
184-
if (!finalCategoryFileString) {
185-
const indexFile = path.resolve(baseDir, `./${(nestedCategory || 'undefined').toLowerCase()}/_index.md`);
186-
if (fs.existsSync(indexFile)) {
187-
finalCategoryFileString = fs.readFileSync(indexFile, 'utf8');
188-
}
189-
}
215+
const finalCategoryArr = existingContent || {};
190216

191217
// HEADER string
192218
const parent = command.parentCommand;
@@ -197,13 +223,12 @@ const updateCategoryFileContent = async (nestedCategory,command, existingContent
197223
title = parentdocs.subCategory;
198224
}
199225
}
200-
const headerString = `+++\ntitle = "${title}"\nweight = 100\n+++\n\n`;
201-
finalCategoryFileString = finalCategoryFileString.replace('{{HEADER}}', headerString);
202-
if (!finalCategoryFileString) {
203-
finalCategoryFileString = headerString;
204-
}
226+
const indexFile = path.resolve(baseDir, `./${(nestedCategory || 'undefined').toLowerCase()}/_index.md`);
227+
finalCategoryArr.indexPath = indexFile;
228+
229+
const headerString = `+++\ntitle = "${title}"\nweight = ${categoriesOrder[title.toLowerCase()] || 100}\n+++\n\n`;
230+
finalCategoryArr.header = headerString;
205231

206-
// COMMANDS string
207232
let commandString = '';
208233
const formattedTitle = docs.title.replace(/\s+/g, '-')
209234
.toLowerCase();
@@ -212,11 +237,40 @@ const updateCategoryFileContent = async (nestedCategory,command, existingContent
212237
commandString += `${docs.description}\n\n`;
213238
commandString += `${docs.usage}\n\n`;
214239

215-
if (finalCategoryFileString.indexOf('{{COMMANDS}}') !== -1) {
216-
finalCategoryFileString = finalCategoryFileString.replace('{{COMMANDS}}', `${commandString}\n\n{{COMMANDS}}`);
217-
} else {
218-
finalCategoryFileString += commandString;
240+
const newCmd = {};
241+
finalCategoryArr.category = category;
242+
newCmd.weight = await getWeight(command);
243+
newCmd.content = commandString;
244+
if (!finalCategoryArr.commands){
245+
finalCategoryArr.commands = [];
246+
247+
}
248+
finalCategoryArr.commands.push(newCmd);
249+
250+
251+
return finalCategoryArr;
252+
};
253+
254+
255+
/**
256+
* updates the category main file with a specific command
257+
* possible extensions are: HEADER, COMMANDS
258+
*/
259+
const updateCategoryFileContent = async (finalContent) => {
260+
let finalCategoryFileString = '';
261+
const indexFile = finalContent.indexPath;
262+
if (fs.existsSync(indexFile)) {
263+
finalCategoryFileString = fs.readFileSync(indexFile, 'utf8');
219264
}
265+
finalCategoryFileString += finalContent.header;
266+
const commandArr = finalContent.commands;
267+
commandArr.sort((a, b) => {
268+
return a.weight - b.weight;
269+
});
270+
271+
_.forEach(commandArr, (command) => {
272+
finalCategoryFileString += command.content;
273+
});
220274

221275
return finalCategoryFileString;
222276
};
@@ -259,12 +313,18 @@ const createAutomatedDocs = async () => {
259313

260314
nestedCategories[category] = await getNestedCategories(command);
261315
const nestedCategory = nestedCategories[category];
262-
categories[category] = await updateCategoryFileContent(nestedCategory,command, categories[category]);
316+
categories[category] = await updateCategoryFileArray(nestedCategory,command, categories[category]);
263317
await upsertCategoryFolder(nestedCategory);
264-
await createCommandFile(nestedCategory,docs);
318+
await createCommandFile(nestedCategory,command);
319+
}
320+
321+
let categoriesFinalContent = {};
322+
for (let command in categories) {
323+
let f = categories[command];
324+
categoriesFinalContent[f.category] = await updateCategoryFileContent(f);
265325
}
266326

267-
_.forEach(categories, async (content, category) => {
327+
_.forEach(categoriesFinalContent, async (content, category) => {
268328
await createCategoryFile(content, nestedCategories[category]);
269329
});
270330
};

lib/interface/cli/Command.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class Command {
191191
res.title = webDocs.title;
192192
res.category = webDocs.category;
193193
res.subCategory = webDocs.subCategory;
194+
res.weight = webDocs.weight;
194195
res.header = `+++\ntitle = "${res.title}"\n+++`;
195196
}
196197

lib/interface/cli/commands/auth/create-context.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ const command = new Command({
3737
webDocs: {
3838
category: 'Authentication',
3939
title: 'Create Context',
40+
weight: 20,
4041
},
4142
builder: (yargs) => {
4243
return yargs

lib/interface/cli/commands/auth/current-context.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const command = new Command({
1313
webDocs: {
1414
category: 'Authentication',
1515
title: 'Get Activated Context',
16+
weight: 30,
1617
},
1718
builder: (yargs) => {
1819
return yargs

lib/interface/cli/commands/auth/get-contexts.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const command = new Command({
1111
webDocs: {
1212
category: 'Authentication',
1313
title: 'Get Contexts',
14+
weight: 10,
1415
},
1516
builder: (yargs) => {
1617
return yargs

lib/interface/cli/commands/auth/use-context.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const command = new Command({
1414
webDocs: {
1515
category: 'Authentication',
1616
title: 'Set Active Context',
17+
weight: 40,
1718
},
1819
builder: (yargs) => {
1920
return yargs

lib/interface/cli/commands/composition/get.cmd.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const getRoot = require('../root/get.cmd');
88

99

1010
const command = new Command({
11-
command: 'compositions [id|name]',
11+
command: 'compositions [id|name..]',
1212
aliases: ['com', 'composition'],
1313
parent: getRoot,
1414
description: 'Get a specific composition or an array of compositions',
@@ -26,17 +26,19 @@ const command = new Command({
2626
.example('codefresh get compositions NAME', 'Get a specific composition');
2727
},
2828
handler: async (argv) => {
29-
const compositionId = argv.id;
29+
const compositionIds = argv.id;
3030

31-
let compositions;
31+
let compositions = [];
3232
// TODO:need to decide for one way for error handeling
33-
if (compositionId) {
34-
compositions = await composition.getCompositionByIdentifier(compositionId);
35-
specifyOutputForSingle(argv.output, compositions);
33+
if (!_.isEmpty(compositionIds)) {
34+
for (const id of compositionIds) {
35+
const currComposition = await composition.getCompositionByIdentifier(id);
36+
compositions.push(currComposition);
37+
}
3638
} else {
3739
compositions = await composition.getCompositions();
38-
specifyOutputForArray(argv.output, compositions);
3940
}
41+
specifyOutputForArray(argv.output, compositions);
4042
},
4143
});
4244

lib/interface/cli/commands/context/apply.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const command = new Command({
1515
webDocs: {
1616
category: 'Contexts',
1717
title: 'Update Context',
18+
weight: 90,
1819
},
1920
builder: (yargs) => {
2021
return yargs

lib/interface/cli/commands/context/create/createConfig.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const command = new Command({
1414
category: 'Create Context',
1515
subCategory : 'Config',
1616
title: 'Create Config Context',
17+
weight: 10,
1718
},
1819
builder: (yargs) => {
1920
yargs

lib/interface/cli/commands/context/create/createSecret.cmd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const command = new Command({
1414
category: 'Create Context',
1515
subCategory : 'Secret',
1616
title: 'Create Secret Context',
17-
17+
weight: 20,
1818
},
1919
builder: (yargs) => {
2020
yargs

lib/interface/cli/commands/context/create/createSecretYaml.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const command = new Command({
1414
category: 'Create Context',
1515
subCategory : 'Secret Yaml',
1616
title: 'Create Secret-Yaml Context',
17+
weight: 30,
1718
},
1819
builder: (yargs) => {
1920
yargs

lib/interface/cli/commands/context/create/createYaml.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const command = new Command({
1414
category: 'Create Context',
1515
subCategory : 'Yaml',
1616
title: 'Create Yaml Context',
17+
weight: 40,
1718
},
1819
builder: (yargs) => {
1920
yargs

lib/interface/cli/commands/context/create/helm-repo/types/http.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const command = new Command({
1717
category: 'Create Helm-Repository Context',
1818
subCategory: 'HTTP',
1919
title: 'From HTTP web server',
20+
weight: 10,
2021
},
2122
builder: (yargs) => {
2223
yargs

lib/interface/cli/commands/context/create/helm-repo/types/s3.cmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const command = new Command({
3333
category: 'Create Helm-Repository Context',
3434
subCategory: 'S3',
3535
title: 'From AWS S3 bucket',
36+
weight: 20,
3637
},
3738
builder: (yargs) => {
3839
yargs

lib/interface/cli/commands/context/get.cmd.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const getRoot = require('../root/get.cmd');
99

1010

1111
const command = new Command({
12-
command: 'contexts [name]',
12+
command: 'contexts [name..]',
1313
aliases: ['ctx', 'context'],
1414
parent: getRoot,
1515
description: 'Get a specific context or an array of contexts',
@@ -37,41 +37,25 @@ const command = new Command({
3737
.example('codefresh get context --type helm-repository', 'Get all helm-repository contexts');
3838
},
3939
handler: async (argv) => {
40-
//maybe we dont need to give an option to type and owner? (according to kubectl)
4140
const data = {};
41+
const names = argv.name;
4242
if (argv.type) {
4343
data.type = argv.type;
4444
}
4545
if (argv.owner) {
4646
data.owner = argv.owner;
4747
}
4848

49-
if (argv.name) {
50-
try {
51-
const singleContext = await context.getContextByName(argv.name, data.owner);
52-
specifyOutputForSingle(argv.output, singleContext);
53-
}
54-
catch (err) {
55-
const error = new CFError({
56-
cause: err,
57-
message: `get context ${argv.name} failed.`,
58-
});
59-
printError(error);
60-
}
61-
}
62-
else {
63-
try {
64-
const contextArray = await context.getContexts(data);
65-
specifyOutputForArray(argv.output, contextArray);
66-
}
67-
catch (err) {
68-
const error = new CFError({
69-
cause: err,
70-
message: 'get contexts failed',
71-
});
72-
printError(error);
49+
let contexts = [];
50+
if (!_.isEmpty(names)) {
51+
for (const name of names) {
52+
const currContext = await context.getContextByName(name, data.owner);
53+
contexts.push(currContext);
7354
}
55+
} else {
56+
contexts = await context.getContexts(data);
7457
}
58+
specifyOutputForArray(argv.output, contexts);
7559
},
7660
});
7761

0 commit comments

Comments
 (0)