Switch from .json to .jsonc (and jsonc-parser) for ssh://<config> files

pull/13/head
Kelvin Schoofs 7 years ago
parent 2122de89d3
commit 808ac03a2a

@ -146,7 +146,7 @@
}, },
"jsonValidation": [ "jsonValidation": [
{ {
"fileMatch": "*.sshfs.json", "fileMatch": "*.sshfs.jsonc",
"url": "https://raw.githubusercontent.com/SchoofsKelvin/vscode-sshfs/c58a5432afb77f3dd97951245821eea589963227/configschema.json" "url": "https://raw.githubusercontent.com/SchoofsKelvin/vscode-sshfs/c58a5432afb77f3dd97951245821eea589963227/configschema.json"
} }
] ]

@ -33,7 +33,7 @@ export function activate(context: vscode.ExtensionContext) {
registerCommand('sshfs.new', async () => { registerCommand('sshfs.new', async () => {
const name = await vscode.window.showInputBox({ placeHolder: 'Name for the new SSH file system', validateInput: manager.invalidConfigName.bind(manager) }); const name = await vscode.window.showInputBox({ placeHolder: 'Name for the new SSH file system', validateInput: manager.invalidConfigName.bind(manager) });
if (name) vscode.window.showTextDocument(vscode.Uri.parse(`ssh://<config>/${name}.json`)); if (name) vscode.window.showTextDocument(vscode.Uri.parse(`ssh://<config>/${name}.sshfs.jsonc`), { preview: false });
}); });
registerCommand('sshfs.connect', (name?: string) => pickAndClick(manager.commandConnect, name, false)); registerCommand('sshfs.connect', (name?: string) => pickAndClick(manager.commandConnect, name, false));

@ -1,5 +1,7 @@
import { readFile } from 'fs'; import { readFile } from 'fs';
import { parse as parseJsonc, ParseError } from 'jsonc-parser';
import * as path from 'path';
import { Client, ConnectConfig } from 'ssh2'; import { Client, ConnectConfig } from 'ssh2';
import * as vscode from 'vscode'; import * as vscode from 'vscode';
@ -31,25 +33,32 @@ function createTreeItem(manager: Manager, name: string): vscode.TreeItem {
}; };
} }
const defaultConfig: FileSystemConfig = ({
name: undefined!, root: '/', host: 'localhost', port: 22,
username: 'root', password: 'CorrectHorseBatteryStaple',
});
function createConfigFs(manager: Manager): SSHFileSystem { function createConfigFs(manager: Manager): SSHFileSystem {
return { return {
...EMPTY_FILE_SYSTEM, ...EMPTY_FILE_SYSTEM,
authority: '<config>', authority: '<config>',
stat: (uri: vscode.Uri) => ({ type: vscode.FileType.File, ctime: 0, mtime: 0, size: 0 } as vscode.FileStat), stat: (uri: vscode.Uri) => ({ type: vscode.FileType.File, ctime: 0, mtime: 0, size: 0 } as vscode.FileStat),
readFile: (uri: vscode.Uri) => { readFile: async (uri: vscode.Uri) => {
const name = uri.path.substring(1, uri.path.length - 11); const name = uri.path.substring(1, uri.path.length - 12);
let config = manager.getConfig(name) || defaultConfig; let config = manager.getConfig(name);
let str;
if (config) {
str = JSON.stringify(config, undefined, 4);
str = `// If you haven't already, associate .jsonc files with "JSON with Comments (jsonc)"\n${str}`;
} else {
str = await toPromise<string>(cb => readFile(path.resolve(__dirname, '../resources/defaultConfig.jsonc'), 'utf-8', cb));
}
config = { ...config, name: undefined! }; config = { ...config, name: undefined! };
return new Uint8Array(new Buffer(JSON.stringify(config, undefined, 4))); return new Uint8Array(new Buffer(str));
}, },
writeFile: (uri: vscode.Uri, content: Uint8Array) => { writeFile: (uri: vscode.Uri, content: Uint8Array) => {
const name = uri.path.substring(1, uri.path.length - 11); const name = uri.path.substring(1, uri.path.length - 12);
try { const errors: ParseError[] = [];
const config = JSON.parse(new Buffer(content).toString()); const config = parseJsonc(new Buffer(content).toString(), errors);
if (!config || errors.length) {
vscode.window.showErrorMessage(`Couldn't parse this config as JSON`);
return;
}
config.name = name; config.name = name;
const loc = manager.updateConfig(name, config); const loc = manager.updateConfig(name, config);
let dialog: Thenable<string | undefined>; let dialog: Thenable<string | undefined>;
@ -63,9 +72,6 @@ function createConfigFs(manager: Manager): SSHFileSystem {
throw new Error(`This isn't supposed to happen! Config location was '${loc}' somehow`); throw new Error(`This isn't supposed to happen! Config location was '${loc}' somehow`);
} }
dialog.then(o => o === 'Connect' && manager.commandReconnect(name)); dialog.then(o => o === 'Connect' && manager.commandReconnect(name));
} catch (e) {
vscode.window.showErrorMessage(`Couldn't parse this config as JSON`);
}
}, },
} as any; } as any;
} }
@ -315,7 +321,7 @@ export class Manager implements vscode.FileSystemProvider, vscode.TreeDataProvid
this.onDidChangeTreeDataEmitter.fire(); this.onDidChangeTreeDataEmitter.fire();
} }
public async commandConfigure(name: string) { public async commandConfigure(name: string) {
vscode.window.showTextDocument(vscode.Uri.parse(`ssh://<config>/${name}.sshfs.json`), { preview: false }); vscode.window.showTextDocument(vscode.Uri.parse(`ssh://<config>/${name}.sshfs.jsonc`), { preview: false });
} }
public commandConfigDelete(name: string) { public commandConfigDelete(name: string) {
this.commandDisconnect(name); this.commandDisconnect(name);

Loading…
Cancel
Save