Files
windmill/cli/resource-type.ts
Kai Jellinghaus 31ac1ddf39 feat(cli): improved setup & allow workspace in base url & refactor workspaces/remotes to unify (#966)
* Setup V2 & Allow Workspace in base url

* Handle login conflict information

* Rework workspace & remote logic

* Add login logic

* Add token storage logic

* 🚀 finish refactor

* :Fix Pull

* Remove setup

* Add create-token

* Remove legacy typesc

* Fix change

* Fix warns

* fix warning

* Update README

* Switch to new workspace by default

* Update demo video

* Update Images

* remove duplicate

* Change wording

* Add to main README

* Fix main readme

* Fix videos
2022-12-01 22:56:57 +01:00

90 lines
2.5 KiB
TypeScript

// deno-lint-ignore-file no-explicit-any
import { Command } from "https://deno.land/x/cliffy@v0.25.4/command/command.ts";
import { ResourceService } from "https://deno.land/x/windmill@v1.50.0/mod.ts";
import { GlobalOptions } from "./types.ts";
import { colors } from "https://deno.land/x/cliffy@v0.25.4/ansi/colors.ts";
import { requireLogin, resolveWorkspace } from "./context.ts";
import { Table } from "https://deno.land/x/cliffy@v0.25.4/table/table.ts";
type ResourceTypeFile = {
schema?: any;
description?: string;
};
export async function pushResourceType(
workspace: string,
filePath: string,
name: string,
) {
const data: ResourceTypeFile = JSON.parse(await Deno.readTextFile(filePath));
if (
await ResourceService.existsResourceType({
workspace: workspace,
path: name,
})
) {
console.log(colors.yellow("Updating existing resource type..."));
await ResourceService.updateResourceType({
workspace: workspace,
path: name,
requestBody: {
description: data.description,
schema: data.schema,
},
});
} else {
console.log(colors.yellow("Creating new resource type..."));
await ResourceService.createResourceType({
workspace: workspace,
requestBody: {
name: name,
description: data.description,
schema: data.schema,
workspace_id: workspace,
},
});
}
}
type PushOptions = GlobalOptions;
async function push(opts: PushOptions, filePath: string, name: string) {
const fstat = await Deno.stat(filePath);
if (!fstat.isFile) {
throw new Error("file path must refer to a file.");
}
const workspace = await resolveWorkspace(opts);
await requireLogin(opts);
console.log(colors.bold.yellow("Pushing resource..."));
await pushResourceType(workspace.workspaceId, filePath, name);
console.log(colors.bold.underline.green("Resource successfully pushed"));
}
async function list(opts: GlobalOptions) {
const workspace = await resolveWorkspace(opts);
await requireLogin(opts);
const res = await ResourceService.listResourceType({
workspace: workspace.workspaceId,
});
new Table()
.header(["Workspace", "Name"])
.padding(2)
.border(true)
.body(res.map((x) => [x.workspace_id ?? "Global", x.name]))
.render();
}
const command = new Command()
.description("resource type related commands")
.action(list as any)
.command(
"push",
"push a local resource spec. This overrides any remote versions.",
)
.arguments("<file_path:string> <name:string>")
.action(push as any);
export default command;