1+ import { l10n } from "vscode" ;
2+ import { instance } from "../instantiate" ;
3+ import IBMi from "./IBMi" ;
4+ import { ConnectionProfile } from "./types" ;
5+
6+ export async function updateConnectionProfile ( profile : ConnectionProfile , options ?: { newName ?: string , delete ?: boolean } ) {
7+ const config = instance . getConnection ( ) ?. getConfig ( ) ;
8+ if ( config ) {
9+ const profiles = config . connectionProfiles ;
10+ const index = profiles . findIndex ( p => p . name === profile . name ) ;
11+
12+ if ( options ?. delete ) {
13+ if ( index < 0 ) {
14+ throw new Error ( l10n . t ( "Profile {0} not found for deletion." , profile . name ) ) ;
15+ }
16+ profiles . splice ( index , 1 ) ;
17+ }
18+ else {
19+ profile . name = options ?. newName || profile . name ;
20+ profiles [ index < 0 ? profiles . length : index ] = profile ;
21+ }
22+
23+ if ( isActiveProfile ( profile ) ) {
24+ //Only update the setLibraryListCommand in the current config since the editor is the only place it can be changed
25+ config . setLibraryListCommand = profile . setLibraryListCommand ;
26+ }
27+
28+ await IBMi . connectionManager . update ( config ) ;
29+ }
30+ }
31+
32+ /**
33+ * @returns ann arry of {@link ConnectionProfile} stored in the config; except the default profile (with a blank name), only used internally
34+ */
35+ export function getConnectionProfiles ( ) {
36+ const config = instance . getConnection ( ) ?. getConfig ( ) ;
37+ if ( config ) {
38+ return config . connectionProfiles . filter ( profile => Boolean ( profile . name ) ) ;
39+ }
40+ else {
41+ throw new Error ( l10n . t ( "Not connected to an IBM i" ) ) ;
42+ }
43+ }
44+
45+ export function getConnectionProfile ( profileName : string ) {
46+ return getConnectionProfiles ( ) . filter ( p => p . name === profileName ) . at ( 0 ) ;
47+ }
48+
49+ export function getDefaultProfile ( ) {
50+ const config = instance . getConnection ( ) ?. getConfig ( ) ;
51+ if ( config ) {
52+ let defaultProfile = config . connectionProfiles . filter ( profile => ! profile . name ) . at ( 0 ) ;
53+ if ( ! defaultProfile ) {
54+ defaultProfile = {
55+ name : '' ,
56+ homeDirectory : '' ,
57+ ifsShortcuts : [ ] ,
58+ currentLibrary : '' ,
59+ objectFilters : [ ] ,
60+ customVariables : [ ] ,
61+ libraryList : [ ]
62+ } ;
63+
64+ config . connectionProfiles . push ( defaultProfile ) ;
65+ }
66+
67+ return defaultProfile ;
68+ }
69+ else {
70+ throw new Error ( l10n . t ( "Not connected to an IBM i" ) ) ;
71+ }
72+ }
73+
74+ export function assignProfile ( fromProfile : ConnectionProfile , toProfile : ConnectionProfile ) {
75+ toProfile . homeDirectory = fromProfile . homeDirectory ;
76+ toProfile . currentLibrary = fromProfile . currentLibrary ;
77+ toProfile . libraryList = fromProfile . libraryList ;
78+ toProfile . objectFilters = fromProfile . objectFilters ;
79+ toProfile . ifsShortcuts = fromProfile . ifsShortcuts ;
80+ toProfile . customVariables = fromProfile . customVariables ;
81+ toProfile . setLibraryListCommand = fromProfile . setLibraryListCommand ;
82+ return toProfile ;
83+ }
84+
85+ export function cloneProfile ( fromProfile : ConnectionProfile , newName : string ) : ConnectionProfile {
86+ return assignProfile ( fromProfile , { name : newName } as ConnectionProfile ) ;
87+ }
88+
89+ export function isActiveProfile ( profile : ConnectionProfile ) {
90+ return instance . getConnection ( ) ?. getConfig ( ) . currentProfile === profile . name ;
91+ }
0 commit comments