Skip to content

Commit

Permalink
feat: add config field and override param for require login
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhq committed Dec 7, 2023
1 parent c72e809 commit 35f59ef
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
35 changes: 24 additions & 11 deletions core/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { shutdown, getPackageVersion } from "../util/util.js"
import type { Command, CommandResult, BuiltinArgs } from "../commands/base.js"
import { CommandGroup } from "../commands/base.js"
import type { GardenError } from "../exceptions.js"
import { PluginError, toGardenError } from "../exceptions.js"
import { PluginError, RuntimeError, toGardenError } from "../exceptions.js"
import type { GardenOpts } from "../garden.js"
import { Garden, makeDummyGarden } from "../garden.js"
import { getRootLogger, getTerminalWriterType, LogLevel, parseLogLevel, RootLogger } from "../logger/logger.js"
Expand Down Expand Up @@ -276,6 +276,29 @@ ${renderCommands(commands)}
throw err
}
}

// If there is an ID in the project config and the user is not logged in (no cloudApi)
// 0.13 => check if login is required based on the `requireLogin` config value
if (!cloudApi && config && config.id) {
log.info("")

// fallback to false if no variables are set
// TODO-0.14: requireLogin should default to true
const isLoginRequired = (gardenEnv.GARDEN_REQUIRE_LOGIN_OVERRIDE ?? config.requireLogin) || false
if (isLoginRequired) {
log.warn(dedent`
You are running this in a project with a Garden Cloud ID and logging in is required.
Please log in via the ${styles.command("garden login")} command.`)
throw new RuntimeError({ message: "" })
} else {
log.warn(
`Warning: You are not logged in into Garden Cloud. Please log in via the ${styles.command(
"garden login"
)} command.`
)
}
log.info("")
}
}

const commandInfo = {
Expand Down Expand Up @@ -333,16 +356,6 @@ ${renderCommands(commands)}

gardenLog.info(`Running in environment ${styles.highlight(`${garden.environmentName}.${garden.namespace}`)}`)

if (!cloudApi && garden.projectId) {
log.info("")
log.warn(
`Warning: You are not logged in into Garden Cloud. Please log in via the ${styles.command(
"garden login"
)} command.`
)
log.info("")
}

if (processRecord) {
// Update the db record for the process
await globalConfigStore.update("activeProcesses", String(processRecord.pid), {
Expand Down
8 changes: 8 additions & 0 deletions core/src/config/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export interface ProjectConfig extends BaseGardenResource {
path: string
id?: string
domain?: string
requireLogin?: boolean
configPath?: string
proxy?: ProxyConfig
defaultEnvironment: string
Expand Down Expand Up @@ -321,6 +322,13 @@ export const projectSchema = createSchema({
.uri()
.meta({ internal: true })
.description("The domain to use for cloud features. Should be the full API/backend URL."),
// TODO: Refer to enterprise documentation for more details.
requireLogin: joi
.boolean()
.default(false)
.meta({ internal: true })
.description("Whether the project requires login to Garden Cloud."),

// Note: We provide a different schema below for actual validation, but need to define it this way for docs
// because joi.alternatives() isn't handled well in the doc generation.
environments: joi
Expand Down
1 change: 1 addition & 0 deletions core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,5 @@ export const gardenEnv = {
.required(false)
.default("https://get.garden.io/releases")
.asUrlString(),
GARDEN_REQUIRE_LOGIN_OVERRIDE: env.get("GARDEN_REQUIRE_LOGIN_OVERRIDE").required(false).asBool(),
}

0 comments on commit 35f59ef

Please sign in to comment.