Skip to content

Commit

Permalink
fix: login/logout using the configured domain (#4050)
Browse files Browse the repository at this point in the history
* fix: login/logout using the configured domain

* fix: use findProjectConfig instead of cli which is not always available

* fix: added login/logout to dev help listing

* fix: remove hidden flag on login/logout

* docs: adding login/logout to reference docs
  • Loading branch information
mkhq authored Apr 11, 2023
1 parent d4f3ef0 commit 1e444e7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 37 deletions.
41 changes: 20 additions & 21 deletions core/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import { ConfigurationError, InternalError, TimeoutError } from "../exceptions"
import { AuthRedirectServer } from "../cloud/auth"
import { EventBus } from "../events"
import { getCloudDistributionName } from "../util/util"
import { ProjectResource } from "../config/project"
import { findProjectConfig } from "../config/base"

const loginTimeoutSec = 60

export class LoginCommand extends Command {
name = "login"
help = "Log in to Garden Cloud."
hidden = true

/**
* Since we're logging in, we don't want to resolve e.g. the project config (since it may use secrets, which are
Expand All @@ -37,27 +38,25 @@ export class LoginCommand extends Command {
printHeader(headerLog, "Login", "☁️")
}

async action({ cli, garden, log }: CommandParams): Promise<CommandResult> {
// The Enterprise API is missing from the Garden class for commands with noProject
// so we initialize it here.
const globalConfigStore = garden.globalConfigStore

let configuredDomain = garden.cloudDomain

if (!configuredDomain) {
const projectConfig = await cli?.getProjectConfig(log, garden.projectRoot)

// Fail if this is not run within a garden project
if (!projectConfig) {
throw new ConfigurationError(
`Not a project directory (or any of the parent directories): ${garden.projectRoot}`,
{
root: garden.projectRoot,
}
)
}
async action({ garden, log }: CommandParams): Promise<CommandResult> {
// NOTE: The Cloud API is missing from the Garden class for commands with noProject
// so we initialize it here. noProject also make sure that the project config is not
// initialized in the garden class, so we need to read it in here to get the cloud
// domain.
const projectConfig: ProjectResource | undefined = await findProjectConfig(log, garden.projectRoot)

// Fail if this is not run within a garden project
if (!projectConfig) {
throw new ConfigurationError(
`Not a project directory (or any of the parent directories): ${garden.projectRoot}`,
{
root: garden.projectRoot,
}
)
}

const globalConfigStore = garden.globalConfigStore

// Garden works by default without Garden Cloud. In order to use cloud, a domain
// must be known to cloud for any command needing a logged in user.
//
Expand All @@ -68,7 +67,7 @@ export class LoginCommand extends Command {
//
// If the fallback was used, we rely on the token to decide if the Cloud API instance
// should use the default domain or not. The token lifecycle ends on logout.
let cloudDomain: string = getGardenCloudDomain(configuredDomain)
let cloudDomain: string = getGardenCloudDomain(projectConfig?.domain)

const distroName = getCloudDistributionName(cloudDomain)

Expand Down
29 changes: 13 additions & 16 deletions core/src/commands/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import { CloudApi, getGardenCloudDomain } from "../cloud/api"
import { dedent } from "../util/string"
import { getCloudDistributionName } from "../util/util"
import { ConfigurationError } from "../exceptions"
import { ProjectResource } from "../config/project"
import { findProjectConfig } from "../config/base"

export class LogOutCommand extends Command {
name = "logout"
help = "Log out of Garden Cloud."
hidden = true
noProject = true

description = dedent`
Expand All @@ -27,26 +28,22 @@ export class LogOutCommand extends Command {
printHeader(headerLog, "Log out", "☁️")
}

async action({ cli, garden, log }: CommandParams): Promise<CommandResult> {
async action({ garden, log }: CommandParams): Promise<CommandResult> {
// The Enterprise API is missing from the Garden class for commands with noProject
// so we initialize it here.
let configuredDomain = garden.cloudDomain
const projectConfig: ProjectResource | undefined = await findProjectConfig(log, garden.projectRoot)

if (!configuredDomain) {
const projectConfig = await cli?.getProjectConfig(log, garden.projectRoot)

// Fail if this is not run within a garden project
if (!projectConfig) {
throw new ConfigurationError(
`Not a project directory (or any of the parent directories): ${garden.projectRoot}`,
{
root: garden.projectRoot,
}
)
}
// Fail if this is not run within a garden project
if (!projectConfig) {
throw new ConfigurationError(
`Not a project directory (or any of the parent directories): ${garden.projectRoot}`,
{
root: garden.projectRoot,
}
)
}

const cloudDomain: string | undefined = getGardenCloudDomain(configuredDomain)
const cloudDomain: string | undefined = getGardenCloudDomain(projectConfig?.domain)

const distroName = getCloudDistributionName(cloudDomain)

Expand Down
24 changes: 24 additions & 0 deletions docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -3010,6 +3010,30 @@ sources:
path:
```

### garden login

**Log in to Garden Cloud.**

Logs you in to Garden Cloud. Subsequent commands will have access to cloud features.

#### Usage

garden login



### garden logout

**Log out of Garden Cloud.**

Logs you out of Garden Cloud.

#### Usage

garden logout



### garden logs

**Retrieves the most recent logs for the specified Deploy(s).**
Expand Down

0 comments on commit 1e444e7

Please sign in to comment.