generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofile-manager.ts
104 lines (80 loc) · 2.3 KB
/
profile-manager.ts
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
95
96
97
98
99
100
101
102
103
104
/**
* Copyright © 2022-2024, Oracle and/or its affiliates.
* This software is licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
import * as yaml from 'js-yaml';
import * as vscode from 'vscode';
import { log } from './logger';
/**
* A ProfileManager provides functionality to manage publisher profiles.
*/
export interface ProfileManager {
/**
* Allow user to edit the profile in an editor.
*/
edit(): Thenable<vscode.TextEditor>
/**
* Return all the profiles defined.
*/
all(): Promise<Profile[]>
/**
* Return the active profile selected in the profile yaml.
*/
active(): Promise<Profile>
/**
* Test whether profiles yaml is present.
*/
isPresent(): Promise<boolean>
/**
* Register a callback to be called when profiles yaml is updated.
*/
onUpdate(callback: Callback): void
dispose(): void
}
export declare type Callback = (content: string) => void;
export declare type ProfilesYaml = {
active: string;
profiles: ProfileYaml[];
};
export declare type ProfileYaml = {
name: string;
publisherId: string;
host: string;
integrationInstance: string;
auth?: any;
};
export function loadProfileYaml(content: string): ProfilesYaml {
try {
let ret = yaml.load(content) as ProfilesYaml;
return ret;
} catch (err) {
log.error(`cannot parse profiles yaml.`);
throw new Error('cannot parse profiles yaml');
}
}
export class Profile {
readonly name: string;
readonly publisherId: string;
readonly host: string;
readonly integrationInstance: string;
readonly auth: any;
readonly active: boolean = false;
constructor(name: string, publisherId: string, host: string, integrationInstance: string, auth: any, active: boolean) {
this.name = name;
this.publisherId = publisherId;
this.host = host;
this.integrationInstance = integrationInstance;
this.auth = auth;
this.active = active;
}
/**
* Tests if the profile is ready to use.
*/
isComplete(): boolean {
return !!this.host && !!this.integrationInstance && this.isValidAuthentication();
}
private isValidAuthentication(): boolean {
if (!this.auth) return true;
return !!this.auth.tokenUrl && !!this.auth.clientId && !!this.auth.clientSecret && !!this.auth.scope;
}
}