Files
windmill/cli/user.ts
Kai Jellinghaus d7757dbe74 feat(cli): Run flows & scripts (#940)
* Enable Script Running from CLI

* Improve Script Logic & Enable Flow run

* Update README

* Fix empty input 415 unsupported media type

* Add flow execution asciicast

* Allow reading inputs

* Add --silent & print result

* Updated syntax

* Update readme

* Fix superadmin users

* Handle values correctly

* Rework input parsing to try-catch JSON

* Accept all input types

* VHS scripts

* Test add Video to Markdown

* Use GIF only

* Final revisions

* I'm not sure why this works but stackoverflow told me
https://stackoverflow.com/questions/4279611/how-to-embed-a-video-into-github-readme-md/4279746#4279746

* Also rename file?

* Use MP4

* Use GIF

* Use MP4 again

* Revert "Use MP4 again"

This reverts commit d3ed4dc28a.
2022-11-25 18:05:23 +01:00

92 lines
2.7 KiB
TypeScript

import { Command } from "https://deno.land/x/cliffy@v0.25.4/command/command.ts";
import { Table } from "https://deno.land/x/cliffy@v0.25.4/table/table.ts";
import { UserService } from "https://deno.land/x/windmill@v1.50.0/mod.ts";
import { GlobalUserInfo } from "https://deno.land/x/windmill@v1.50.0/windmill-api/index.ts";
import { passwordGenerator } from "https://deno.land/x/password_generator@latest/mod.ts"; // TODO: I think the version is called latest, but it's still pinned.
import { getContext } from "./context.ts";
import { GlobalOptions } from "./types.ts";
import { colors } from "https://deno.land/x/cliffy@v0.25.4/ansi/colors.ts";
import { Input } from "https://deno.land/x/cliffy@v0.25.4/prompt/input.ts";
async function list(opts: GlobalOptions) {
const _ = await getContext(opts);
const perPage = 10;
let page = 0;
const total: GlobalUserInfo[] = [];
while (true) {
const res = await UserService.listUsersAsSuperAdmin({ page, perPage });
total.push(...res);
page += 1;
if (res.length < perPage) {
break;
}
}
new Table()
.header(["email", "name", "company", "verified", "super admin"])
.padding(2)
.border(true)
.body(
total.map((x) => [
x.email,
x.name ?? "-",
x.company ?? "-",
x.verified ? "true" : "false",
x.super_admin ? "true" : "false",
])
)
.render();
}
async function add(
opts: GlobalOptions & {
superadmin?: boolean;
company?: string;
name?: string;
},
email: string,
password?: string
) {
const _ = await getContext(opts);
const password_final = password ?? passwordGenerator("*", 15);
await UserService.createUserGlobally({
requestBody: {
email,
password: password_final,
super_admin: opts.superadmin ?? false,
company: opts.company,
name: opts.name,
},
});
if (!password) {
console.log(colors.red("New Password: " + password_final));
}
console.log(colors.underline.green("New User Created."));
}
async function remove(opts: GlobalOptions, email: string) {
const _ = await getContext(opts);
await UserService.globalUserDelete({ email });
console.log(colors.green("Deleted User " + email));
}
const command = new Command()
.description("user related commands")
.action(list as any)
.command("add", "Create a user")
.arguments("<email:string> [password:string]")
.option("--superadmin", "Specify to make the new user superadmin.")
.option(
"--company <company:string>",
"Specify to set the company of the new user."
)
.option("--name <name:string>", "Specify to set the name of the new user.")
.action(add as any)
.command("remove", "Delete a user")
.arguments("<email:string>")
.action(remove as any);
export default command;