@faable/faable 4.32.0 → 4.33.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -29,6 +29,7 @@ npm i -g @faable/faable
29
29
  | --- | --- | --- |
30
30
  | `faable login` / `whoami` / `logout` | Session management (device flow; CI uses OIDC, no login needed) | [Authentication](https://faable.com/docs/cli#authentication) |
31
31
  | `faable project` | The project commands act on: `list`, `use`, `current`, `clear` (or `--project` on any command) | [Projects](https://faable.com/docs/cli#projects) |
32
+ | `faable organization` | Group projects under a company: `list`, `get`, `create`, `projects add`/`rm`, `admins add`/`rm` (`org` for short) | [Organizations](https://faable.com/docs/cli#organizations) |
32
33
  | `faable deploy` | `launch` (the default: a bare `faable deploy` deploys the current directory), then `status`, `logs`, `deployments`, `inspect`, `trigger`, `redeploy`, `cancel`, `traffic`, `usage`, `quota`, `open` | [Deployment](https://faable.com/docs/cli#deployment) |
33
34
  | `faable deploy apps` | `list`, `get`, `create` (from a GitHub repository, first deploy included), `set` (branch, root directory, deploy mode) | [Apps](https://faable.com/docs/cli#apps) |
34
35
  | `faable deploy secrets` | Environment variables: `list`, `set` (`KEY=VALUE`, `--env-file`, or `-f -` from stdin), `rm` | [Secrets](https://faable.com/docs/cli#secrets) |
@@ -192,6 +192,31 @@ class FaableApi {
192
192
  }
193
193
  }));
194
194
  }
195
+ // ---- organizations (a person's session: api keys are refused) ----
196
+ async listOrganizations() {
197
+ return data(this.client.get(`/organization`));
198
+ }
199
+ async getOrganization(id) {
200
+ return data(this.client.get(`/organization/${id}`));
201
+ }
202
+ async createOrganization(name, project_id) {
203
+ return data(this.client.post(`/organization`, {
204
+ name,
205
+ project_id
206
+ }));
207
+ }
208
+ async addOrganizationProject(id, project_id) {
209
+ return data(this.client.post(`/organization/${id}/project`, { project_id }));
210
+ }
211
+ async removeOrganizationProject(id, project_id) {
212
+ return data(this.client.delete(`/organization/${id}/project/${project_id}`));
213
+ }
214
+ async addOrganizationAdmin(id, user_id) {
215
+ return data(this.client.post(`/organization/${id}/admin`, { user_id }));
216
+ }
217
+ async removeOrganizationAdmin(id, user_id) {
218
+ return data(this.client.delete(`/organization/${id}/admin/${user_id}`));
219
+ }
195
220
  async getProject(project_id) {
196
221
  return data(this.client.get(`/project/${project_id}`));
197
222
  }
@@ -0,0 +1,193 @@
1
+ import { requireApi } from '../../api/context.js';
2
+ import { requireProject, resolveProjectRef } from '../../api/project.js';
3
+ import { CliError } from '../../lib/errors.js';
4
+ import { confirm } from '../../lib/interactive.js';
5
+ import { print_json, table_lines, print, json_option } from '../../lib/listing.js';
6
+ import { log } from '../../log.js';
7
+
8
+ // `faable organization` — the companies that group projects. An organization
9
+ // gives nobody access to its projects and is not billed. The api decides who
10
+ // may: its admins and the projects' owners, or staff; a person, never an API
11
+ // key (`Organizations are managed by people`).
12
+ const ID_RE = /^organization_[0-9a-f]{24}$/;
13
+ const resolveOrganization = async (api, ref) => {
14
+ if (ID_RE.test(ref))
15
+ return ref;
16
+ const wanted = ref.toLowerCase();
17
+ const { results } = await api.listOrganizations();
18
+ const matches = results.filter(o => o.name.toLowerCase() === wanted || o.slug.toLowerCase() === wanted);
19
+ if (matches.length === 1)
20
+ return matches[0].id;
21
+ throw new CliError('not_found', matches.length === 0
22
+ ? `No organization named "${ref}". See them with: faable organization list`
23
+ : `"${ref}" is more than one organization: use its id.`);
24
+ };
25
+ const project_names = (o) => o.projects.map(p => p.name).join(', ') || '-';
26
+ const organization_list = {
27
+ command: 'list',
28
+ describe: 'List the organizations you administer or have a project in',
29
+ builder: yargs => json_option(yargs).showHelpOnFail(false),
30
+ handler: async (args) => {
31
+ const { api } = await requireApi();
32
+ const { results } = await api.listOrganizations();
33
+ if (args.json)
34
+ return print_json(results);
35
+ if (results.length === 0)
36
+ return log.info('📭 No organizations.');
37
+ for (const line of table_lines(['ID', 'NAME', 'ADMIN', 'PROJECTS'], results.map(o => [
38
+ o.id,
39
+ o.name,
40
+ o.is_admin ? 'yes' : 'no',
41
+ project_names(o)
42
+ ]))) {
43
+ print(line);
44
+ }
45
+ }
46
+ };
47
+ const organization_get = {
48
+ command: 'get <organization>',
49
+ describe: 'Show an organization: its projects and admins',
50
+ builder: yargs => json_option(yargs)
51
+ .positional('organization', { type: 'string', demandOption: true })
52
+ .showHelpOnFail(false),
53
+ handler: async (args) => {
54
+ const { api } = await requireApi();
55
+ const org = await api.getOrganization(await resolveOrganization(api, args.organization));
56
+ if (args.json)
57
+ return print_json(org);
58
+ print(`${org.name} (${org.id})`);
59
+ print(`Projects: ${project_names(org)}`);
60
+ print(`Admins: ${org.admins?.join(', ') || '-'}`);
61
+ }
62
+ };
63
+ const organization_create = {
64
+ command: 'create <name>',
65
+ describe: 'Create an organization from a project (--project or the active one); you become its admin',
66
+ builder: yargs => json_option(yargs)
67
+ .positional('name', { type: 'string', demandOption: true })
68
+ .example('$0 organization create "Acme SL" --project acme-web', '')
69
+ .showHelpOnFail(false),
70
+ handler: async (args) => {
71
+ const { api } = await requireApi();
72
+ const { id } = await requireProject(api);
73
+ const org = await api.createOrganization(args.name, id);
74
+ if (args.json)
75
+ return print_json(org);
76
+ log.info(`✅ Organization ${org.name} (${org.id})`);
77
+ }
78
+ };
79
+ const project_positionals = (yargs) => yargs
80
+ .positional('organization', { type: 'string', demandOption: true })
81
+ .positional('project_ref', {
82
+ type: 'string',
83
+ demandOption: true,
84
+ describe: 'Project id, name or slug'
85
+ });
86
+ const projects_add = {
87
+ command: 'add <organization> <project_ref>',
88
+ describe: 'Put a project in the organization (moves it from any other)',
89
+ builder: yargs => project_positionals(yargs).showHelpOnFail(false),
90
+ handler: async (args) => {
91
+ const { api } = await requireApi();
92
+ const id = await resolveOrganization(api, args.organization);
93
+ const project = await resolveProjectRef(api, args.project_ref);
94
+ await api.addOrganizationProject(id, project);
95
+ log.info(`✅ ${args.project_ref} is in ${args.organization}`);
96
+ }
97
+ };
98
+ const projects_rm = {
99
+ command: 'rm <organization> <project_ref>',
100
+ describe: 'Take a project out of the organization: it becomes personal',
101
+ builder: yargs => project_positionals(yargs)
102
+ .option('yes', {
103
+ type: 'boolean',
104
+ alias: 'y',
105
+ description: 'Skip the confirmation prompt'
106
+ })
107
+ .showHelpOnFail(false),
108
+ handler: async (args) => {
109
+ const { api } = await requireApi();
110
+ const id = await resolveOrganization(api, args.organization);
111
+ const project = await resolveProjectRef(api, args.project_ref);
112
+ const go = await confirm({
113
+ message: `Take ${args.project_ref} out of ${args.organization}?`,
114
+ yes: args.yes
115
+ });
116
+ if (!go)
117
+ return log.info('Cancelled.');
118
+ await api.removeOrganizationProject(id, project);
119
+ log.info(`✅ ${args.project_ref} is no longer in ${args.organization}`);
120
+ }
121
+ };
122
+ const organization_projects = {
123
+ command: 'projects',
124
+ describe: "Add or remove an organization's projects",
125
+ builder: yargs => yargs
126
+ .command(projects_add)
127
+ .command(projects_rm)
128
+ .demandCommand(1)
129
+ .showHelpOnFail(false),
130
+ handler: () => { }
131
+ };
132
+ const admin_positionals = (yargs) => yargs
133
+ .positional('organization', { type: 'string', demandOption: true })
134
+ .positional('user_id', { type: 'string', demandOption: true });
135
+ const admins_add = {
136
+ command: 'add <organization> <user_id>',
137
+ describe: 'Make someone an admin (gives no access to its projects)',
138
+ builder: yargs => admin_positionals(yargs).showHelpOnFail(false),
139
+ handler: async (args) => {
140
+ const { api } = await requireApi();
141
+ const id = await resolveOrganization(api, args.organization);
142
+ await api.addOrganizationAdmin(id, args.user_id);
143
+ log.info(`✅ ${args.user_id} administers ${args.organization}`);
144
+ }
145
+ };
146
+ const admins_rm = {
147
+ command: 'rm <organization> <user_id>',
148
+ describe: 'Remove an admin (an organization keeps at least one)',
149
+ builder: yargs => admin_positionals(yargs)
150
+ .option('yes', {
151
+ type: 'boolean',
152
+ alias: 'y',
153
+ description: 'Skip the confirmation prompt'
154
+ })
155
+ .showHelpOnFail(false),
156
+ handler: async (args) => {
157
+ const { api } = await requireApi();
158
+ const id = await resolveOrganization(api, args.organization);
159
+ const go = await confirm({
160
+ message: `Remove ${args.user_id} as admin of ${args.organization}?`,
161
+ yes: args.yes
162
+ });
163
+ if (!go)
164
+ return log.info('Cancelled.');
165
+ await api.removeOrganizationAdmin(id, args.user_id);
166
+ log.info(`✅ ${args.user_id} no longer administers ${args.organization}`);
167
+ }
168
+ };
169
+ const organization_admins = {
170
+ command: 'admins',
171
+ describe: "Add or remove an organization's admins",
172
+ builder: yargs => yargs
173
+ .command(admins_add)
174
+ .command(admins_rm)
175
+ .demandCommand(1)
176
+ .showHelpOnFail(false),
177
+ handler: () => { }
178
+ };
179
+ const organization = {
180
+ command: ['organization', 'org'],
181
+ describe: 'Group projects under a company (list, get, create, projects, admins)',
182
+ builder: yargs => yargs
183
+ .command(organization_list)
184
+ .command(organization_get)
185
+ .command(organization_create)
186
+ .command(organization_projects)
187
+ .command(organization_admins)
188
+ .demandCommand(1)
189
+ .showHelpOnFail(false),
190
+ handler: () => { }
191
+ };
192
+
193
+ export { organization };
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ import { link_deprecated } from './commands/link/index.js';
6
6
  import { login } from './commands/login/index.js';
7
7
  import { logout } from './commands/logout/index.js';
8
8
  import { mcp } from './commands/mcp/index.js';
9
+ import { organization } from './commands/organization/index.js';
9
10
  import { project } from './commands/project/index.js';
10
11
  import { upgrade } from './commands/upgrade/index.js';
11
12
  import { whoami } from './commands/whoami/index.js';
@@ -94,6 +95,7 @@ yg.scriptName('faable')
94
95
  .command(deploy)
95
96
  .command(auth)
96
97
  .command(project)
98
+ .command(organization)
97
99
  .command(login)
98
100
  .command(logout)
99
101
  .command(whoami)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@faable/faable",
3
- "version": "4.32.0",
3
+ "version": "4.33.0",
4
4
  "main": "dist/index.js",
5
5
  "license": "MIT",
6
6
  "author": "Marc Pomar <marc@faable.com>",