Skip to content

Commit 913bf57

Browse files
committed
feat: Create command to install and uninstall apt packages
1 parent aa7a322 commit 913bf57

3 files changed

Lines changed: 138 additions & 0 deletions

File tree

docs/app-apt.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# App:Apt
2+
3+
Install or uninstall apt packages for you application.
4+
5+
* [app:apt](#appapt)
6+
7+
```sh-session
8+
USAGE
9+
$ discloud apps:apt [APP_ID] -i=PACKAGE
10+
11+
ARGUMENTS
12+
APP_ID The id of your app on Discloud
13+
14+
OPTIONS
15+
-i, --install [PACKAGE] Install a package.
16+
-u, --uninstall [PACKAGE] Uninstall a package.
17+
18+
EXAMPLE
19+
$ discloud app:apt APP_ID -i=PACKAGE
20+
$ discloud app:apt APP_ID -u=PACKAGE
21+
22+
PACKAGE
23+
- canvas
24+
- puppeteer
25+
- java
26+
- ffmpeg
27+
- libgl
28+
- openssl
29+
- tools
30+
```
31+
32+
* If does not contains `APP_ID`, the CLI will show a list of your apps for you to select.

docs/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!-- markdownlint-disable MD041 -->
22

3+
* [app:apt](./app-apt.md) - Install or uninstall apt packages for you application.
34
* [apps:team](./apps-team.md) - Get team information of your applications.
45
* [apps](./apps.md) - Get information of your applications.
56
* [backups](./backups.md) - Make backup from your applications in Discloud.

src/commands/app-apt.ts

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import { RESTGetApiAppAllResult, RESTPutApiAppAptResult, Routes } from "@discloudapp/api-types/v2";
2+
import { GluegunCommand, GluegunToolbox } from "gluegun";
3+
import { exit } from "node:process";
4+
import { apidiscloud, config, RateLimit } from "../util";
5+
import { Apt } from "../util/constants";
6+
7+
export default new class AppApt implements GluegunCommand {
8+
name = "app:apt";
9+
description = "Install or uninstall apt packages for you application.";
10+
11+
async run(toolbox: GluegunToolbox) {
12+
const { parameters, print, prompt } = toolbox;
13+
14+
if (!config.data.token)
15+
return print.error("Please use login command before using this command.");
16+
17+
if (RateLimit.isLimited)
18+
return print.error(`Rate limited until: ${RateLimit.limited}`);
19+
20+
if (!parameters.first) {
21+
const spin = print.spin({
22+
text: print.colors.cyan("Fetching apps..."),
23+
});
24+
25+
const apiRes = await apidiscloud.get<RESTGetApiAppAllResult>(Routes.app("all"));
26+
27+
spin.stop();
28+
29+
if (apiRes.data)
30+
if ("apps" in apiRes.data) {
31+
const { appId } = await prompt.ask({
32+
name: "appId",
33+
message: "Choose the app",
34+
type: "select",
35+
choices: [{
36+
name: "all",
37+
message: "All apps",
38+
value: "all",
39+
}].concat(apiRes.data.apps.map(app => ({
40+
name: app.id,
41+
message: `${app.name} - ${app.id} - ${app.online ?
42+
print.colors.green("online") :
43+
print.colors.red("offline")}`,
44+
value: app.id,
45+
}))),
46+
});
47+
48+
parameters.first = appId;
49+
}
50+
51+
if (!parameters.first)
52+
return print.error("Need app id.");
53+
}
54+
55+
let method: "delete" | "put" | undefined;
56+
57+
if (Object.keys(parameters.options).length)
58+
method =
59+
(parameters.options.u ?? parameters.options.uninstall) ? "delete" :
60+
(parameters.options.i ?? parameters.options.install) ? "put" :
61+
method;
62+
63+
if (!method || !Object.keys(Apt).includes(parameters.options[method]))
64+
return print.error(
65+
"You need to use one of the options below:" +
66+
"\n -i, --install [PACKAGE] Install a package." +
67+
"\n -u, --uninstall [PACKAGE] Uninstall a package." + "\n" +
68+
"\nPACKAGES:" + "\n" +
69+
Object.keys(Apt).map(pkg => ` - ${pkg} ${Apt[<"tools">pkg]}`).join("\n"),
70+
);
71+
72+
const apt = parameters.options[method];
73+
74+
let action;
75+
switch (method) {
76+
case "delete":
77+
action = `Uninstalling ${apt} from ${parameters.first} app...`;
78+
break;
79+
case "put":
80+
action = `Installing ${apt} for ${parameters.first} app...`;
81+
break;
82+
default:
83+
action = "";
84+
break;
85+
}
86+
87+
const spin = print.spin({
88+
text: print.colors.cyan(action),
89+
});
90+
91+
const apiRes = await apidiscloud[method]<
92+
RESTPutApiAppAptResult
93+
>(Routes.appApt(parameters.first),
94+
["delete", "put"].includes(method) ? {
95+
apt,
96+
} : undefined);
97+
98+
new RateLimit(apiRes.headers);
99+
100+
if (apiRes.status)
101+
if (print.spinApiRes(apiRes, spin) > 399) return exit(apiRes.status);
102+
103+
exit(0);
104+
}
105+
};

0 commit comments

Comments
 (0)