Skip to content

Commit

Permalink
Merge pull request #1975 from codefori/cleanup/connection_manager_api
Browse files Browse the repository at this point in the history
Cleanup/connection_manager_api
  • Loading branch information
sebjulliand authored May 14, 2024
2 parents 1aacb59 + e1974fd commit 7664f4b
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 225 deletions.
27 changes: 18 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -964,36 +964,42 @@
"command": "code-for-ibmi.connect",
"title": "New Connection",
"category": "IBM i",
"icon": "$(add)"
"icon": "$(add)",
"enablement": "code-for-ibmi:editingConnection !== true"
},
{
"command": "code-for-ibmi.connectTo",
"title": "Connect to IBM i",
"category": "IBM i",
"icon": "$(debug-start)"
"icon": "$(debug-start)",
"enablement": "code-for-ibmi:editingConnection !== true"
},
{
"command": "code-for-ibmi.connectToPrevious",
"title": "Connect to Previous IBM i",
"category": "IBM i",
"icon": "$(remote)"
"icon": "$(remote)",
"enablement": "code-for-ibmi:editingConnection !== true"
},
{
"command": "code-for-ibmi.connectToAndReload",
"title": "Connect and Reload Server Settings",
"category": "IBM i"
"category": "IBM i",
"enablement": "code-for-ibmi:editingConnection !== true"
},
{
"command": "code-for-ibmi.refreshConnections",
"title": "Refresh",
"category": "IBM i",
"icon": "$(refresh)"
"icon": "$(refresh)",
"enablement": "code-for-ibmi:editingConnection !== true"
},
{
"command": "code-for-ibmi.sortConnections",
"title": "Sort",
"category": "IBM i",
"icon": "$(list-ordered)"
"icon": "$(list-ordered)",
"enablement": "code-for-ibmi:editingConnection !== true"
},
{
"command": "code-for-ibmi.showAdditionalSettings",
Expand All @@ -1008,17 +1014,20 @@
{
"command": "code-for-ibmi.renameConnection",
"title": "Rename...",
"category": "IBM i"
"category": "IBM i",
"enablement": "code-for-ibmi:editingConnection !== true"
},
{
"command": "code-for-ibmi.deleteConnection",
"title": "Delete...",
"category": "IBM i"
"category": "IBM i",
"enablement": "code-for-ibmi:editingConnection !== true"
},
{
"command": "code-for-ibmi.copyConnection",
"title": "Copy...",
"category": "IBM i"
"category": "IBM i",
"enablement": "code-for-ibmi:editingConnection !== true"
},
{
"command": "code-for-ibmi.disconnect",
Expand Down
75 changes: 74 additions & 1 deletion src/api/Configuration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os from "os";
import * as vscode from 'vscode';
import { DeploymentMethod } from '../typings';
import { ConnectionData, DeploymentMethod } from '../typings';
import { FilterType } from './Filter';

export type SourceDateMode = "edit" | "diff";
Expand Down Expand Up @@ -29,6 +29,79 @@ export namespace GlobalConfiguration {
}
}

export interface StoredConnection {
index: number,
data: ConnectionData
};

const getPasswordKey = (connectionName:string) => `${connectionName}_password`;

export namespace ConnectionManager {
export function getByName(name: string): StoredConnection | undefined {
const connections = getAll();
const index = connections.findIndex(conn => conn.name === name);
if (index !== -1) {
return { index, data: connections[index] };
}
}

export function sort() {
const connections = getAll();
connections.sort((a, b) => a.name.localeCompare(b.name));
return GlobalConfiguration.set(`connections`, connections);
}

export function getAll(): ConnectionData[] {
return GlobalConfiguration.get<ConnectionData[]>(`connections`) || [];
}

function setAll(connections: ConnectionData[]) {
return GlobalConfiguration.set(`connections`, connections);
}

export async function storeNew(data: ConnectionData): Promise<StoredConnection> {
const connections = getAll();
const newId = connections.length;
connections.push(data);
await setAll(connections);
return { index: newId, data };
}

export function deleteByName(name: string) {
const connections = getAll();
const index = connections.findIndex(conn => conn.name === name);
if (index !== -1) {
connections.splice(index, 1);
return setAll(connections);
}
}

export function updateByIndex(index: number, data: ConnectionData) {
const connections = getAll();
connections[index] = data;

// Remove possible password from any connection
connections.forEach(conn => delete conn.password);

return GlobalConfiguration.set(`connections`, connections);
}

export function getStoredPassword(context: vscode.ExtensionContext, connectionName: string) {
const connectionKey = getPasswordKey(connectionName);
return context.secrets.get(connectionKey);
}

export function setStoredPassword(context: vscode.ExtensionContext, connectionName: string, password: string) {
const connectionKey = getPasswordKey(connectionName);
return context.secrets.store(connectionKey, password);
}

export function deleteStoredPassword(context: vscode.ExtensionContext, connectionName: string) {
const connectionKey = getPasswordKey(connectionName);
return context.secrets.delete(connectionKey);
}
}

export namespace ConnectionConfiguration {
export interface Parameters extends ConnectionProfile {
host: string;
Expand Down
6 changes: 4 additions & 2 deletions src/api/Tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,15 +363,17 @@ export namespace Tools {
activeContexts.set(context, 0);
}
else {
activeContexts.set(context, stack++);
stack++;
activeContexts.set(context, stack);
}
return await task();
}
finally {
let stack = activeContexts.get(context);
if (stack !== undefined) {
if (stack) {
activeContexts.set(context, stack--);
stack--;
activeContexts.set(context, stack);
}
else {
await vscode.commands.executeCommand(`setContext`, context, undefined);
Expand Down
3 changes: 2 additions & 1 deletion src/api/debug/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { getEnvConfig } from "../local/env";
import * as certificates from "./certificates";
import { DEBUG_CONFIG_FILE, getDebugServiceDetails, resetDebugServiceDetails } from "./config";
import * as server from "./server";
import { ConnectionManager } from "../Configuration";

const debugExtensionId = `IBM.ibmidebug`;

Expand Down Expand Up @@ -176,7 +177,7 @@ export async function initialize(context: ExtensionContext) {
const getPassword = async () => {
const connection = instance.getConnection();

let password = await context.secrets.get(`${connection!.currentConnectionName}_password`);
let password = await ConnectionManager.getStoredPassword(context, connection!.currentConnectionName);

if (!password) {
password = temporaryPassword;
Expand Down
8 changes: 3 additions & 5 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { CustomUI } from "./api/CustomUI";
import { instance, loadAllofExtension } from './instantiate';

import { CompileTools } from "./api/CompileTools";
import { ConnectionConfiguration, GlobalConfiguration, onCodeForIBMiConfigurationChange } from "./api/Configuration";
import { ConnectionConfiguration, ConnectionManager, GlobalConfiguration, onCodeForIBMiConfigurationChange } from "./api/Configuration";
import IBMi from "./api/IBMi";
import { GlobalStorage } from "./api/Storage";
import { Tools } from "./api/Tools";
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function activate(context: ExtensionContext): Promise<CodeForIBMi>

await loadAllofExtension(context);
const checkLastConnections = () => {
const connections = (GlobalConfiguration.get<ConnectionData[]>(`connections`) || []);
const connections = ConnectionManager.getAll();
const lastConnections = (GlobalStorage.get().getLastConnections() || []).filter(lc => connections.find(c => c.name === lc.name));
GlobalStorage.get().setLastConnections(lastConnections);
commands.executeCommand(`setContext`, `code-for-ibmi:hasPreviousConnection`, lastConnections.length > 0);
Expand Down Expand Up @@ -72,7 +72,7 @@ export async function activate(context: ExtensionContext): Promise<CodeForIBMi>
}

if (savePassword && connectionData.password) {
await context.secrets.store(`${connectionData.name}_password`, `${connectionData.password}`);
await ConnectionManager.setStoredPassword(context, connectionData.name, connectionData.password);
}

return (await new IBMi().connect(connectionData, undefined, reloadSettings)).success;
Expand Down Expand Up @@ -119,8 +119,6 @@ export async function activate(context: ExtensionContext): Promise<CodeForIBMi>
]);
});

await fixLoginSettings();

return { instance, customUI: () => new CustomUI(), deployTools: DeployTools, evfeventParser: parseErrors, tools: Tools };
}

Expand Down
5 changes: 2 additions & 3 deletions src/instantiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Tools } from './api/Tools';
import path, { dirname } from 'path';
import * as vscode from "vscode";
import { CompileTools } from './api/CompileTools';
import { ConnectionConfiguration, DefaultOpenMode, GlobalConfiguration, onCodeForIBMiConfigurationChange } from "./api/Configuration";
import { ConnectionConfiguration, ConnectionManager, DefaultOpenMode, GlobalConfiguration, onCodeForIBMiConfigurationChange } from "./api/Configuration";
import Instance from "./api/Instance";
import { Search } from "./api/Search";
import { Terminal } from './api/Terminal';
Expand Down Expand Up @@ -625,8 +625,7 @@ export async function loadAllofExtension(context: vscode.ExtensionContext) {
throw new Error(`Password request denied for extension ${displayName}.`);
}

const connectionKey = `${instance.getConnection()!.currentConnectionName}_password`;
const storedPassword = await context.secrets.get(connectionKey);
const storedPassword = await ConnectionManager.getStoredPassword(context, instance.getConnection()!.currentConnectionName);

if (storedPassword) {
let isAuthed = storage.getExtensionAuthorisation(extension) !== undefined;
Expand Down
20 changes: 5 additions & 15 deletions src/sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { env } from "process";
import querystring from "querystring";
import { commands, ExtensionContext, Uri, window } from "vscode";
import { ConnectionConfiguration, GlobalConfiguration } from "./api/Configuration";
import { ConnectionConfiguration, ConnectionManager, GlobalConfiguration } from "./api/Configuration";
import { Tools } from "./api/Tools";
import { instance } from "./instantiate";
import { t } from "./locale";
Expand Down Expand Up @@ -62,21 +62,11 @@ export async function registerUriHandler(context: ExtensionContext) {
await initialSetup(connectionData.username);

if (save) {
let existingConnections: ConnectionData[] | undefined = GlobalConfiguration.get(`connections`);
const existingConnection = ConnectionManager.getByName(connectionData.name);

if (existingConnections) {
const existingConnection = existingConnections.find(item => item.name === host);

if (!existingConnection) {
// New connection!
existingConnections.push({
...connectionData,
password: undefined, // Removes the password from the object
});

await context.secrets.store(`${host}_password`, pass);
await GlobalConfiguration.set(`connections`, existingConnections);
}
if (!existingConnection) {
// New connection!
await ConnectionManager.storeNew(connectionData);
}
}

Expand Down
Loading

0 comments on commit 7664f4b

Please sign in to comment.