Skip to content

Commit 6b83c43

Browse files
authored
Remove request to select Project form commands called from palette (#1690)
Signed-off-by: Denis Golovin <dgolovin@redhat.com>
1 parent 62bcd49 commit 6b83c43

20 files changed

+135
-200
lines changed

package-lock.json

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

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"git-transport-protocol": "^0.1.0",
6767
"globby": "^10.0.1",
6868
"js-yaml": "^3.13.1",
69+
"minimatch": "^3.0.4",
6970
"mkdirp": "^0.5.1",
7071
"openshift-rest-client": "^4.0.0",
7172
"rxjs": "^6.5.3",

src/oc.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,7 @@ export class Oc {
5050
if (message) {
5151
window.showWarningMessage(message);
5252
} else {
53-
const project = await OpenShiftItem.getOpenShiftCmdData(
54-
undefined,
55-
'Select a Project where to create a new resource',
56-
);
53+
const project = await OpenShiftItem.getOpenShiftCmdData(undefined);
5754
if (!project) return null;
5855
const result = await CliChannel.getInstance().execute(
5956
`${toolLocation} create -f ${document.fileName} --namespace ${project.getName()}`,

src/odo.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const {Collapsed} = TreeItemCollapsibleState;
3636
export interface OpenShiftObject extends QuickPickItem {
3737
getChildren(): ProviderResult<OpenShiftObject[]>;
3838
removeChild(item: OpenShiftObject): Promise<void>;
39+
addChild(item: OpenShiftObject): Promise<OpenShiftObject>;
3940
getParent(): OpenShiftObject;
4041
getName(): string;
4142
contextValue: ContextType;
@@ -60,6 +61,21 @@ export enum ContextType {
6061
COMPONENT_ROUTE = 'componentRoute'
6162
}
6263

64+
function compareNodes(a: OpenShiftObject, b: OpenShiftObject): number {
65+
if (!a.contextValue) return -1;
66+
if (!b.contextValue) return 1;
67+
const acontext = a.contextValue.includes('_') ? a.contextValue.substr(0, a.contextValue.indexOf('_')) : a.contextValue;
68+
const bcontext = b.contextValue.includes('_') ? b.contextValue.substr(0, b.contextValue.indexOf('_')) : b.contextValue;
69+
const t = acontext.localeCompare(bcontext);
70+
return t || a.label.localeCompare(b.label);
71+
}
72+
73+
function insert(array: OpenShiftObject[], item: OpenShiftObject): OpenShiftObject {
74+
const i = bs(array, item, compareNodes);
75+
array.splice(Math.abs(i)-1, 0, item);
76+
return item;
77+
}
78+
6379
export abstract class OpenShiftObjectImpl implements OpenShiftObject {
6480

6581
private explorerPath: string;
@@ -129,6 +145,11 @@ export abstract class OpenShiftObjectImpl implements OpenShiftObject {
129145
array.splice(array.indexOf(item), 1);
130146
}
131147

148+
public async addChild(item: OpenShiftObject): Promise<OpenShiftObject> {
149+
const array = await item.getChildren();
150+
return insert(array, item);
151+
}
152+
132153
getParent(): OpenShiftObject {
133154
return this.parent;
134155
}
@@ -150,13 +171,19 @@ export class OpenShiftCluster extends OpenShiftObjectImpl {
150171
}
151172

152173
async getChildren(): Promise<OpenShiftObject[]> {
153-
return [(await this.odo.getProjects()).find((prj:OpenShiftProject)=>prj.active)];
174+
const activeProject = (await this.odo.getProjects()).find((prj:OpenShiftProject)=>prj.active)
175+
return activeProject ? [activeProject] : [];
154176
}
155177

156178
public async removeChild(item: OpenShiftObject): Promise<void> {
157179
const array = await this.odo.getProjects();
158180
array.splice(array.indexOf(item), 1);
159181
}
182+
183+
public async addChild(item: OpenShiftObject): Promise<OpenShiftObject> {
184+
const array = await this.odo.getProjects();
185+
return insert(array, item);
186+
}
160187
}
161188

162189
export class OpenShiftProject extends OpenShiftObjectImpl {
@@ -336,15 +363,6 @@ export interface Odo {
336363
readonly subject: Subject<OdoEvent>;
337364
}
338365

339-
function compareNodes(a: OpenShiftObject, b: OpenShiftObject): number {
340-
if (!a.contextValue) return -1;
341-
if (!b.contextValue) return 1;
342-
const acontext = a.contextValue.includes('_') ? a.contextValue.substr(0, a.contextValue.indexOf('_')) : a.contextValue;
343-
const bcontext = b.contextValue.includes('_') ? b.contextValue.substr(0, b.contextValue.indexOf('_')) : b.contextValue;
344-
const t = acontext.localeCompare(bcontext);
345-
return t || a.label.localeCompare(b.label);
346-
}
347-
348366
class OdoModel {
349367
private parentToChildren: Map<OpenShiftObject, OpenShiftObject[]> = new Map();
350368

@@ -756,21 +774,15 @@ export class OdoImpl implements Odo {
756774
return this.odoLoginMessages.some((msg) => result.stderr.includes(msg));
757775
}
758776

759-
private insert(array: OpenShiftObject[], item: OpenShiftObject): OpenShiftObject {
760-
const i = bs(array, item, compareNodes);
761-
array.splice(Math.abs(i)-1, 0, item);
762-
return item;
763-
}
764-
765777
private async insertAndReveal(item: OpenShiftObject): Promise<OpenShiftObject> {
766778
// await OpenShiftExplorer.getInstance().reveal(this.insert(await item.getParent().getChildren(), item));
767-
this.subject.next(new OdoEventImpl('inserted', this.insert(await item.getParent().getChildren(), item), true));
779+
this.subject.next(new OdoEventImpl('inserted', await item.getParent().addChild(item), true));
768780
return item;
769781
}
770782

771783
private async insertAndRefresh(item: OpenShiftObject): Promise<OpenShiftObject> {
772784
// await OpenShiftExplorer.getInstance().refresh(this.insert(await item.getParent().getChildren(), item).getParent());
773-
this.subject.next(new OdoEventImpl('changed', this.insert(await item.getParent().getChildren(), item).getParent()));
785+
this.subject.next(new OdoEventImpl('changed', (await item.getParent().addChild(item)).getParent()));
774786
return item;
775787
}
776788

@@ -789,8 +801,7 @@ export class OdoImpl implements Odo {
789801
public async createProject(projectName: string): Promise<OpenShiftObject> {
790802
await OdoImpl.instance.execute(Command.createProject(projectName));
791803
const clusters = await this.getClusters();
792-
this.subject.next(new OdoEventImpl('inserted', clusters[0], false));
793-
return new OpenShiftProject(clusters[0], projectName, true);
804+
return this.insertAndReveal(new OpenShiftProject(clusters[0], projectName, true));
794805
}
795806

796807
public async deleteApplication(app: OpenShiftObject): Promise<OpenShiftObject> {

src/openshift/application.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ export class Application extends OpenShiftItem {
1515
@vsCommand('openshift.app.describe', true)
1616
static async describe(treeItem: OpenShiftObject): Promise<void> {
1717
const application = await Application.getOpenShiftCmdData(treeItem,
18-
"From which project you want to describe Application",
1918
"Select Application you want to describe");
2019
if (application) Application.odo.executeInTerminal(Command.describeApplication(application.getParent().getName(), application.getName()), undefined, `OpenShift: Describe '${application.getName()}' Application`);
2120
}
2221

2322
@vsCommand('openshift.app.delete', true)
2423
static async del(treeItem: OpenShiftObject): Promise<string> {
2524
const application = await Application.getOpenShiftCmdData(treeItem,
26-
"From which Project you want to delete Application",
2725
"Select Application to delete");
2826
if (application) {
2927
const appName = application.getName();

src/openshift/component.ts

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,12 @@ export class Component extends OpenShiftItem {
8080

8181
static async getOpenshiftData(context: OpenShiftObject): Promise<OpenShiftObject> {
8282
return Component.getOpenShiftCmdData(context,
83-
"In which Project you want to create a Component",
8483
"In which Application you want to create a Component"
8584
);
8685
}
8786

8887
@vsCommand('openshift.component.create')
8988
@selectTargetApplication(
90-
"In which Project you want to create a Component",
9189
"In which Application you want to create a Component"
9290
)
9391
static async create(application: OpenShiftObject): Promise<string> {
@@ -125,7 +123,6 @@ export class Component extends OpenShiftItem {
125123

126124
@vsCommand('openshift.component.delete', true)
127125
@selectTargetComponent(
128-
"From which Project do you want to delete Component",
129126
"From which Application you want to delete Component",
130127
"Select Component to delete"
131128
)
@@ -150,7 +147,6 @@ export class Component extends OpenShiftItem {
150147

151148
@vsCommand('openshift.component.undeploy', true)
152149
@selectTargetComponent(
153-
"From which Project do you want to undeploy Component",
154150
"From which Application you want to undeploy Component",
155151
"Select Component to undeploy",
156152
(target) => target.contextValue === ContextType.COMPONENT_PUSHED
@@ -202,7 +198,6 @@ export class Component extends OpenShiftItem {
202198

203199
@vsCommand('openshift.component.describe', true)
204200
@selectTargetComponent(
205-
"From which Project you want to describe Component",
206201
"From which Application you want to describe Component",
207202
"Select Component you want to describe"
208203
)
@@ -223,7 +218,6 @@ export class Component extends OpenShiftItem {
223218

224219
@vsCommand('openshift.component.log', true)
225220
@selectTargetComponent(
226-
"In which Project you want to see Log",
227221
"In which Application you want to see Log",
228222
"For which Component you want to see Log",
229223
(value: OpenShiftObject) => value.contextValue === ContextType.COMPONENT_PUSHED
@@ -242,7 +236,6 @@ export class Component extends OpenShiftItem {
242236

243237
@vsCommand('openshift.component.followLog', true)
244238
@selectTargetComponent(
245-
"In which Project you want to follow Log",
246239
"In which Application you want to follow Log",
247240
"For which Component you want to follow Log",
248241
(value: OpenShiftObject) => value.contextValue === ContextType.COMPONENT_PUSHED
@@ -291,7 +284,6 @@ export class Component extends OpenShiftItem {
291284

292285
@vsCommand('openshift.component.unlinkComponent.palette')
293286
@selectTargetComponent(
294-
'Select a Project',
295287
'Select an Application',
296288
'Select a Component',
297289
(value: OpenShiftObject) => value.contextValue === ContextType.COMPONENT_PUSHED
@@ -323,7 +315,6 @@ export class Component extends OpenShiftItem {
323315

324316
@vsCommand('openshift.component.unlinkService.palette')
325317
@selectTargetComponent(
326-
'Select a Project',
327318
'Select an Application',
328319
'Select a Component',
329320
(value: OpenShiftObject) => value.contextValue === ContextType.COMPONENT_PUSHED
@@ -349,7 +340,6 @@ export class Component extends OpenShiftItem {
349340

350341
@vsCommand('openshift.component.linkComponent')
351342
@selectTargetComponent(
352-
'Select a Project',
353343
'Select an Application',
354344
'Select a Component',
355345
(value: OpenShiftObject) => value.contextValue === ContextType.COMPONENT_PUSHED
@@ -391,7 +381,6 @@ export class Component extends OpenShiftItem {
391381

392382
@vsCommand('openshift.component.linkService')
393383
@selectTargetComponent(
394-
'Select a Project',
395384
'Select an Application',
396385
'Select a Component',
397386
(value: OpenShiftObject) => value.contextValue === ContextType.COMPONENT_PUSHED
@@ -419,7 +408,6 @@ export class Component extends OpenShiftItem {
419408

420409
@vsCommand('openshift.component.push', true)
421410
@selectTargetComponent(
422-
'In which Project you want to push the changes',
423411
'In which Application you want to push the changes',
424412
'For which Component you want to push the changes',
425413
(target) => target.contextValue === ContextType.COMPONENT_PUSHED || target.contextValue === ContextType.COMPONENT
@@ -454,7 +442,6 @@ export class Component extends OpenShiftItem {
454442

455443
@vsCommand('openshift.component.watch', true)
456444
@selectTargetComponent(
457-
'Select a Project',
458445
'Select an Application',
459446
'Select a Component you want to watch',
460447
(target) => target.contextValue === ContextType.COMPONENT_PUSHED
@@ -491,7 +478,6 @@ export class Component extends OpenShiftItem {
491478

492479
@vsCommand('openshift.component.openUrl', true)
493480
@selectTargetComponent(
494-
'Select a Project',
495481
'Select an Application',
496482
'Select a Component to open in browser',
497483
(target) => target.contextValue === ContextType.COMPONENT_PUSHED
@@ -533,8 +519,7 @@ export class Component extends OpenShiftItem {
533519

534520
@vsCommand('openshift.component.createFromLocal')
535521
@selectTargetApplication(
536-
"In which Project you want to create a Component",
537-
"In which Application you want to create a Component"
522+
"Select an Application where you want to create a Component"
538523
)
539524
static async createFromLocal(application: OpenShiftObject): Promise<string | null> {
540525
if (!application) return null;
@@ -559,7 +544,6 @@ export class Component extends OpenShiftItem {
559544

560545
static async createFromFolder(folder: Uri): Promise<string | null> {
561546
const application = await Component.getOpenShiftCmdData(undefined,
562-
"In which Project you want to create a Component",
563547
"In which Application you want to create a Component"
564548
);
565549
if (!application) return null;
@@ -582,7 +566,6 @@ export class Component extends OpenShiftItem {
582566

583567
@vsCommand('openshift.component.createFromGit')
584568
@selectTargetApplication(
585-
"In which Project you want to create a Component",
586569
"In which Application you want to create a Component"
587570
)
588571
static async createFromGit(application: OpenShiftObject): Promise<string | null> {
@@ -630,7 +613,6 @@ export class Component extends OpenShiftItem {
630613

631614
@vsCommand('openshift.component.createFromBinary')
632615
@selectTargetApplication(
633-
"In which Project you want to create a Component",
634616
"In which Application you want to create a Component"
635617
)
636618
static async createFromBinary(application: OpenShiftObject): Promise<string | null> {
@@ -670,7 +652,6 @@ export class Component extends OpenShiftItem {
670652

671653
@vsCommand('openshift.component.debug', true)
672654
@selectTargetComponent(
673-
'Select a Project',
674655
'Select an Application',
675656
'Select a Component you want to debug (showing only Components pushed to the cluster)',
676657
(value: OpenShiftObject) => value.contextValue === ContextType.COMPONENT_PUSHED

src/openshift/openshiftItem.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55

66
import { window, QuickPickItem } from 'vscode';
77
import * as validator from 'validator';
8-
import { Odo, OdoImpl, OpenShiftObject, ContextType, OpenShiftApplication } from '../odo';
8+
import { Odo, OdoImpl, OpenShiftObject, ContextType, OpenShiftApplication, OpenShiftProject } from '../odo';
99
import { OpenShiftExplorer } from '../explorer';
10+
import { VsCommandError } from '../vscommand';
1011

1112
const errorMessage = {
1213
Project: 'You need at least one Project available. Please create new OpenShift Project and try again.',
@@ -125,10 +126,15 @@ export default class OpenShiftItem {
125126
return urlList;
126127
}
127128

128-
static async getOpenShiftCmdData(treeItem: OpenShiftObject, projectPlaceholder: string, appPlaceholder?: string, compPlaceholder?: string, condition?: (value: OpenShiftObject) => boolean): Promise<OpenShiftObject | null> {
129+
static async getOpenShiftCmdData(treeItem: OpenShiftObject, appPlaceholder?: string, compPlaceholder?: string, condition?: (value: OpenShiftObject) => boolean): Promise<OpenShiftObject | null> {
129130
let context: OpenShiftObject | QuickPickCommand = treeItem;
130131
let project: OpenShiftObject;
131-
if (!context) context = await window.showQuickPick(OpenShiftItem.getProjectNames(), {placeHolder: projectPlaceholder, ignoreFocusOut: true});
132+
if (!context) {
133+
context = (await this.odo.getProjects()).find((prj:OpenShiftProject)=>prj.active);
134+
if (!context) {
135+
throw new VsCommandError(errorMessage.Project);
136+
}
137+
}
132138
if (context && context.contextValue === ContextType.PROJECT && appPlaceholder ) {
133139
project = context;
134140
context = await window.showQuickPick<OpenShiftObject | QuickPickCommand>(OpenShiftItem.getApplicationNames(project, appPlaceholder.includes('create') && compPlaceholder === undefined), {placeHolder: appPlaceholder, ignoreFocusOut: true});
@@ -165,10 +171,10 @@ function selectTargetDecoratorFactory(decorator: (...args:any[]) => Promise<Open
165171
};
166172
}
167173

168-
export function selectTargetComponent(prjPlaceHolder, appPlaceHolder, cmpPlaceHolder, condition?: (value: OpenShiftObject) => boolean) {
169-
return selectTargetDecoratorFactory(async (context) => OpenShiftItem.getOpenShiftCmdData(context, prjPlaceHolder, appPlaceHolder, cmpPlaceHolder, condition));
174+
export function selectTargetComponent(appPlaceHolder, cmpPlaceHolder, condition?: (value: OpenShiftObject) => boolean) {
175+
return selectTargetDecoratorFactory(async (context) => OpenShiftItem.getOpenShiftCmdData(context, appPlaceHolder, cmpPlaceHolder, condition));
170176
}
171177

172-
export function selectTargetApplication(prjPlaceHolder, appPlaceHolder) {
173-
return selectTargetDecoratorFactory(async (context) => OpenShiftItem.getOpenShiftCmdData(context, prjPlaceHolder, appPlaceHolder));
178+
export function selectTargetApplication(appPlaceHolder) {
179+
return selectTargetDecoratorFactory(async (context) => OpenShiftItem.getOpenShiftCmdData(context, appPlaceHolder));
174180
}

src/openshift/project.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ export class Project extends OpenShiftItem {
3737
@vsCommand('openshift.project.delete', true)
3838
static async del(context: OpenShiftObject): Promise<string> {
3939
let result: Promise<string> = null;
40-
const project = await Project.getOpenShiftCmdData(context,
41-
"Select Project to delete"
42-
);
40+
const project = await Project.getOpenShiftCmdData(context);
4341
if (project) {
4442
const value = await window.showWarningMessage(`Do you want to delete Project '${project.getName()}'?`, 'Yes', 'Cancel');
4543
if (value === 'Yes') {

0 commit comments

Comments
 (0)