Skip to content

Commit 18a04ae

Browse files
Merge pull request #83 from contentstack/feat/40525
feat: added support to uninstall app from multi or all locations
2 parents 78d344c + 745707c commit 18a04ae

File tree

11 files changed

+93
-39
lines changed

11 files changed

+93
-39
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ dependabot.yml
1717
/bkp
1818
.editorconfig
1919
oclif.manifest.json
20-
*.env
20+
*.env
21+
.vscode/

.vscode/launch.json

Lines changed: 0 additions & 18 deletions
This file was deleted.

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,13 @@ Uninstall an app
155155

156156
```
157157
USAGE
158-
$ csdx app:uninstall [--org <value>] [--app-uid <value>] [--installation-uid <value>]
158+
$ csdx app:uninstall [--org <value>] [--app-uid <value>] [--installation-uid <value>] [--uninstall-all]
159159
160160
FLAGS
161161
--app-uid=<value> Provide the app UID
162162
--installation-uid=<value> Provide the installation ID of the app that needs to be uninstalled.
163163
--org=<value> Provide the organization UID
164+
--uninstall-all Please select stacks from where the app must be uninstalled.
164165
165166
DESCRIPTION
166167
Uninstall an app

src/commands/app/uninstall.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { BaseCommand } from "./base-command";
22
import { flags } from "@contentstack/cli-utilities";
3-
import { getOrg, getApp, fetchApp, getInstallation, uninstallApp } from "../../util";
3+
import { getOrg, getApp, fetchApp } from "../../util";
44
import { commonMsg, uninstallAppMsg } from "../../messages";
5+
import { UninstallAppFactory } from "../../factories/uninstall-app-factory";
56

67
export default class Uninstall extends BaseCommand<typeof Uninstall> {
78
static description = "Uninstall an app";
@@ -19,6 +20,9 @@ export default class Uninstall extends BaseCommand<typeof Uninstall> {
1920
}),
2021
'installation-uid': flags.string({
2122
description: uninstallAppMsg.INSTALLATION_UID
23+
}),
24+
'uninstall-all': flags.boolean({
25+
description: uninstallAppMsg.UNINSTALL_ALL,
2226
})
2327
}
2428

@@ -34,17 +38,14 @@ export default class Uninstall extends BaseCommand<typeof Uninstall> {
3438
} else {
3539
app = await fetchApp(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log})
3640
}
37-
41+
3842
this.flags['app-uid'] = app?.uid;
3943
appType = app?.['target_type']
4044

41-
// select installation uid to uninstall
42-
if (!this.flags['installation-uid']) {
43-
this.flags['installation-uid'] = await getInstallation(this.flags, this.sharedConfig.org, this.managementSdk, appType, {managementSdk: this.managementAppSdk, log: this.log})
44-
}
45-
46-
// uninstall app
47-
await uninstallApp(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log})
45+
const factory = new UninstallAppFactory()
46+
const strategy = factory.getStrategyInstance(this.flags['uninstall-all'])
47+
await strategy.run(this.flags, this.sharedConfig.org, this.managementSdk, {managementSdk: this.managementAppSdk, log: this.log}, appType)
48+
4849
this.log(this.$t(uninstallAppMsg.APP_UNINSTALLED, { app: app?.name || this.flags["app-uid"] }), "info")
4950

5051
} catch (error: any) {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { UninstallApp } from "../interfaces/uninstall-app";
2+
import { UninstallAll } from "../strategies/uninstall-all";
3+
import { UninstallSelected } from "../strategies/uninstall-selected";
4+
5+
export class UninstallAppFactory {
6+
public getStrategyInstance(uninstallAll: boolean): UninstallApp {
7+
let strategy: UninstallApp;
8+
if (uninstallAll) {
9+
strategy = new UninstallAll()
10+
} else {
11+
strategy = new UninstallSelected()
12+
}
13+
return strategy
14+
}
15+
}

src/interfaces/uninstall-app.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { ContentstackClient, FlagInput } from "@contentstack/cli-utilities";
2+
import { CommonOptions } from "../util";
3+
import { AppTarget } from "@contentstack/management/types/app/index"
4+
5+
export interface UninstallApp {
6+
run(flags: FlagInput, org: string, managementSdk: ContentstackClient, options: CommonOptions, appType: AppTarget): Promise<void>;
7+
}

src/messages/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ const uninstallAppMsg = {
9595
INSTALLATION_UID: "Provide the installation ID of the app that needs to be uninstalled.",
9696
NO_INSTALLATIONS_FOUND: "Cannot find any installations for this app.",
9797
APP_UNINSTALLED: "{app} uninstalled successfully.",
98-
UNINSTALLING_APP: "Uninstalling app from {type}..."
98+
UNINSTALLING_APP: "Uninstalling app from {type}...",
99+
UNINSTALL_ALL: "Please select stacks from where the app must be uninstalled.",
99100
}
100101

101102
const messages: typeof errors &

src/strategies/uninstall-all.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ContentstackClient, FlagInput } from "@contentstack/cli-utilities";
2+
import { UninstallApp } from "../interfaces/uninstall-app";
3+
import { CommonOptions, getInstallation, uninstallApp } from "../util";
4+
import { AppTarget } from "@contentstack/management/types/app/index"
5+
6+
export class UninstallAll implements UninstallApp {
7+
public async run(flags: FlagInput, org: string, managementSdk: ContentstackClient, options: CommonOptions, appType: AppTarget): Promise<void> {
8+
// get all installation uids to uninstall
9+
const installationUids = await this.getInstallations(flags, org, managementSdk, options, appType)
10+
for (const installationUid of installationUids) {
11+
await uninstallApp(flags, org, options, installationUid)
12+
}
13+
}
14+
15+
public async getInstallations(flags: FlagInput, org: string, managementSdk: ContentstackClient, options: CommonOptions, appType: AppTarget): Promise<string[]> {
16+
let installationUids: string = await getInstallation(flags, org, managementSdk, appType, options, true)
17+
return installationUids.split(',')
18+
}
19+
}

src/strategies/uninstall-selected.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { ContentstackClient, FlagInput } from "@contentstack/cli-utilities";
2+
import { UninstallApp } from "../interfaces/uninstall-app";
3+
import { CommonOptions, getInstallation, uninstallApp } from "../util";
4+
import { AppTarget } from "@contentstack/management/types/app/index"
5+
6+
export class UninstallSelected implements UninstallApp {
7+
public async run(flags: FlagInput, org: string, managementSdk: ContentstackClient, options: CommonOptions, appType: AppTarget): Promise<void> {
8+
// select installation uid to uninstall
9+
const installationUids = await this.getInstallations(flags, org, managementSdk, options, appType)
10+
for (const installationUid of installationUids) {
11+
await uninstallApp(flags, org, options, installationUid)
12+
}
13+
}
14+
15+
public async getInstallations(flags: FlagInput, org: string, managementSdk: ContentstackClient, options: CommonOptions, appType: AppTarget): Promise<string[]> {
16+
let installationUids: any = flags['installation-uid'];
17+
if (!installationUids) {
18+
installationUids = await getInstallation(flags, org, managementSdk, appType, options)
19+
}
20+
return installationUids.split(',')
21+
}
22+
}

src/util/common-utils.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,9 @@ async function getStacks(
169169
return stacks;
170170
}
171171

172-
function uninstallApp(flags: FlagInput, orgUid: string, options: CommonOptions) {
172+
function uninstallApp(flags: FlagInput, orgUid: string, options: CommonOptions, installationUid: string) {
173173
const {managementSdk} = options;
174174
const app: unknown = flags['app-uid'];
175-
const installationUid: unknown = flags['installation-uid'];
176175
return managementSdk
177176
.organization(orgUid)
178177
.app(app as string)

0 commit comments

Comments
 (0)