-
Notifications
You must be signed in to change notification settings - Fork 267
/
logout.ts
99 lines (81 loc) · 3.25 KB
/
logout.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (C) 2018-2023 Garden Technologies, Inc. <info@garden.io>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { Command, CommandParams, CommandResult } from "./base"
import { printHeader } from "../logger/util"
import { CloudApi, getGardenCloudDomain } from "../cloud/api"
import { dedent, deline } from "../util/string"
import { getCloudDistributionName } from "../util/util"
import { ConfigurationError } from "../exceptions"
import { ProjectConfig } from "../config/project"
import { findProjectConfig } from "../config/base"
import { BooleanParameter } from "../cli/params"
export const logoutOpts = {
"disable-project-check": new BooleanParameter({
help: deline`Disables the check that this is run from within a Garden Project. Logs you out from the default Garden Cloud domain`,
defaultValue: false,
}),
}
type Opts = typeof logoutOpts
export class LogOutCommand extends Command<{}, Opts> {
name = "logout"
help = "Log out of Garden Cloud."
override noProject = true
override description = dedent`
Logs you out of Garden Cloud.
`
override options = logoutOpts
override printHeader({ log }) {
printHeader(log, "Log out", "☁️")
}
async action({ garden, log, opts }: CommandParams): Promise<CommandResult> {
// The Cloud API is missing from the Garden class for commands with noProject
// so we initialize it with a cloud domain derived from `getGardenCloudDomain`.
let projectConfig: ProjectConfig | undefined = undefined
const forceProjectCheck = !opts["disable-project-check"]
if (forceProjectCheck) {
projectConfig = await findProjectConfig({ log, path: garden.projectRoot })
// Fail if this is not run within a garden project
if (!projectConfig) {
throw new ConfigurationError({
message: `Not a project directory (or any of the parent directories): ${garden.projectRoot}`,
})
}
}
const cloudDomain: string | undefined = getGardenCloudDomain(projectConfig?.domain)
const distroName = getCloudDistributionName(cloudDomain)
try {
// The Enterprise API is missing from the Garden class for commands with noProject
// so we initialize it here.
const token = await garden.globalConfigStore.get("clientAuthTokens", cloudDomain)
if (!token) {
log.info({ msg: `You're already logged out from ${cloudDomain}.` })
return {}
}
const cloudApi = await CloudApi.factory({
log,
cloudDomain,
skipLogging: true,
globalConfigStore: garden.globalConfigStore,
})
if (!cloudApi) {
return {}
}
await cloudApi.post("token/logout", { headers: { Cookie: `rt=${token?.refreshToken}` } })
cloudApi.close()
} catch (err) {
const msg = dedent`
The following issue occurred while logging out from ${distroName} (your session will be cleared regardless): ${err}\n
`
log.warn(msg)
} finally {
await CloudApi.clearAuthToken(log, garden.globalConfigStore, cloudDomain)
log.info({ msg: `Successfully logged out from ${cloudDomain}.` })
}
return {}
}
}