vscode-kubernetes-tools / vscode-kubernetes-tools-api

A NPM package for building Kubernetes extensions for Visual Studio Code
Other
9 stars 15 forks source link

API entry point and first interface #3

Closed itowlson closed 5 years ago

itowlson commented 5 years ago

This provides utility functions and JSDoc for:

This is a starting point only and is intended to support convenient incremental development by allowing components to be versioned separately. Additional conceptual documentation is required but this probably belongs on the main extension repo - thoughts?

NOTE: This does not prevent (and is not meant to try to prevent) people getting to the API directly via the vscode.extensions API. It's just a convenient wrapper that people will be able to download from NPM in order to get type declarations, JSDoc, etc. (And I haven't been able to find a way to generate that from the k8s extension codebase directly, hence the second repo.)

Example usage:

import * as vscode from 'vscode';
import * as k8s from 'vscode-kubernetes-tools-api';
import { ClusterProviderV1 as cp } from 'vscode-kubernetes-tools-api';

export async function activate(context: vscode.ExtensionContext) {
    const cp = await k8s.extension.clusterProvider.v1;

    if (!cp.available) {
        console.log("Unable to register provider: " + cp.reason);
        return;
    }

    cp.api.register({
        id: 'faux',
        displayName: 'Faux Cluster',
        supportedActions: ['configure', 'create'],
        next: onNext
    });
}

function onNext(w: cp.Wizard, action: cp.ClusterProviderAction, message: any): void {
    const sendingStep = message[cp.SENDING_STEP_KEY];
    if (sendingStep === cp.SELECT_CLUSTER_TYPE_STEP_ID) {
        w.showPage(`<h1>You selected ${action} and I obey!!!!</h1>
        <p><pre>${JSON.stringify(message, undefined, 2)}</pre></p>
        <p><form id='${cp.WIZARD_FORM_NAME}'>
        <input type='hidden' name='clusterType' value='faux' />
        Something: <input type='text' name='something' />
        <p><button onclick='${cp.NEXT_PAGE}'>MAKE IT SO &gt;</button></p>
        </form><p>
        `);
    } else {
        w.showPage(`<h1>My work here is done</h1>
        <p><pre>${JSON.stringify(message, undefined, 2)}</pre></p>
        `);
    }
}