Skip to content

Commit

Permalink
chore: move Garden Cloud utils to own module (#5435)
Browse files Browse the repository at this point in the history
* chore: move Garden Cloud utils to own module

* chore: better type-safety

* test: add missing tests for `getCloudDistributionName`

* refactor: type for Cloud log section name

* test: add missing tests for `getCloudLogSectionName`

* chore: renamed new files to follow the naming convention
  • Loading branch information
vvagaytsev authored Nov 23, 2023
1 parent 783cc66 commit a7c7816
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 56 deletions.
3 changes: 2 additions & 1 deletion core/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import { resolve, join } from "path"
import fsExtra from "fs-extra"
const { pathExists } = fsExtra
import { getBuiltinCommands } from "../commands/commands.js"
import { shutdown, getPackageVersion, getCloudDistributionName } from "../util/util.js"
import { getCloudDistributionName } from "../util/cloud.js"
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"
Expand Down
3 changes: 2 additions & 1 deletion core/src/cloud/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import type {
GetProjectResponse,
ListProjectsResponse,
} from "@garden-io/platform-api-types"
import { getCloudDistributionName, getCloudLogSectionName, getPackageVersion } from "../util/util.js"
import { getCloudDistributionName, getCloudLogSectionName } from "../util/cloud.js"
import { getPackageVersion } from "../util/util.js"
import type { CommandInfo } from "../plugin-context.js"
import type { ClientAuthToken, GlobalConfigStore } from "../config-store/global.js"
import { add } from "date-fns"
Expand Down
9 changes: 2 additions & 7 deletions core/src/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,8 @@ import type { Log } from "../logger/log-entry.js"
import type { LoggerType, LoggerBase, LoggerConfigBase, LogLevel } from "../logger/logger.js"
import { eventLogLevel } from "../logger/logger.js"
import { printFooter } from "../logger/util.js"
import {
getCloudDistributionName,
getCloudLogSectionName,
getDurationMsec,
getPackageVersion,
userPrompt,
} from "../util/util.js"
import { getCloudDistributionName, getCloudLogSectionName } from "../util/cloud.js"
import { getDurationMsec, getPackageVersion, userPrompt } from "../util/util.js"
import { renderOptions, renderCommands, renderArguments, cliStyles, optionsWithAliasValues } from "../cli/helpers.js"
import type { GlobalOptions, ParameterValues, ParameterObject } from "../cli/params.js"
import { globalOptions } from "../cli/params.js"
Expand Down
2 changes: 1 addition & 1 deletion core/src/commands/cloud/secrets/secrets-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import { dedent, deline } from "../../../util/string.js"
import { IntegerParameter, PathParameter, StringParameter, StringsParameter } from "../../../cli/params.js"
import type { StringMap } from "../../../config/common.js"
import dotenv from "dotenv"
import { getCloudDistributionName } from "../../../util/util.js"
import type { CloudProject } from "../../../cloud/api.js"
import { getCloudDistributionName } from "../../../util/cloud.js"

export const secretsCreateArgs = {
secrets: new StringsParameter({
Expand Down
2 changes: 1 addition & 1 deletion core/src/commands/cloud/secrets/secrets-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { SecretResult } from "../helpers.js"
import { applyFilter, makeSecretFromResponse, noApiMsg } from "../helpers.js"
import { sortBy } from "lodash-es"
import { StringsParameter } from "../../../cli/params.js"
import { getCloudDistributionName } from "../../../util/util.js"
import { getCloudDistributionName } from "../../../util/cloud.js"
import type { CloudApi, CloudProject } from "../../../cloud/api.js"
import type { Log } from "../../../logger/log-entry.js"
import { styles } from "../../../logger/styles.js"
Expand Down
2 changes: 1 addition & 1 deletion core/src/commands/cloud/secrets/secrets-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import type { CloudProject } from "../../../cloud/api.js"
import type { StringMap } from "../../../config/common.js"
import { CloudApiError, CommandError, ConfigurationError, GardenError } from "../../../exceptions.js"
import { printHeader } from "../../../logger/util.js"
import { getCloudDistributionName } from "../../../util/cloud.js"
import { dedent, deline } from "../../../util/string.js"
import { getCloudDistributionName } from "../../../util/util.js"
import type { CommandParams, CommandResult } from "../../base.js"
import { Command } from "../../base.js"
import type { ApiCommandError, SecretResult } from "../helpers.js"
Expand Down
2 changes: 1 addition & 1 deletion core/src/commands/cloud/users/users-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import type { UserResult } from "../helpers.js"
import { applyFilter, makeUserFromResponse, noApiMsg } from "../helpers.js"
import { sortBy } from "lodash-es"
import { StringsParameter } from "../../../cli/params.js"
import { getCloudDistributionName } from "../../../util/util.js"
import type { CloudProject } from "../../../cloud/api.js"
import { styles } from "../../../logger/styles.js"
import { getCloudDistributionName } from "../../../util/cloud.js"

export const usersListOpts = {
"filter-names": new StringsParameter({
Expand Down
2 changes: 1 addition & 1 deletion core/src/commands/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import type { Log } from "../logger/log-entry.js"
import { ConfigurationError, TimeoutError, InternalError, CloudApiError } from "../exceptions.js"
import { AuthRedirectServer } from "../cloud/auth.js"
import type { EventBus } from "../events/events.js"
import { getCloudDistributionName } from "../util/util.js"
import type { ProjectConfig } from "../config/project.js"
import { findProjectConfig } from "../config/base.js"
import { BooleanParameter } from "../cli/params.js"
import { getCloudDistributionName } from "../util/cloud.js"
import { deline } from "../util/string.js"

const loginTimeoutSec = 60
Expand Down
2 changes: 1 addition & 1 deletion core/src/commands/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type { CommandParams, CommandResult } from "./base.js"
import { Command } from "./base.js"
import { printHeader } from "../logger/util.js"
import { CloudApi, getGardenCloudDomain } from "../cloud/api.js"
import { getCloudDistributionName } from "../util/cloud.js"
import { dedent, deline } from "../util/string.js"
import { getCloudDistributionName } from "../util/util.js"
import { ConfigurationError } from "../exceptions.js"
import type { ProjectConfig } from "../config/project.js"
import { findProjectConfig } from "../config/base.js"
Expand Down
3 changes: 2 additions & 1 deletion core/src/commands/serve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import { printEmoji, printHeader } from "../logger/util.js"
import { dedent } from "../util/string.js"
import type { CommandLine } from "../cli/command-line.js"
import { GardenInstanceManager } from "../server/instance-manager.js"
import { getCloudDistributionName, sleep } from "../util/util.js"
import { getCloudDistributionName } from "../util/cloud.js"
import { sleep } from "../util/util.js"
import type { Log } from "../logger/log-entry.js"
import { findProjectConfig } from "../config/base.js"
import { CloudApiTokenRefreshError, getGardenCloudDomain } from "../cloud/api.js"
Expand Down
12 changes: 2 additions & 10 deletions core/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,8 @@ import {
defaultNamespace,
defaultEnvironment,
} from "./config/project.js"
import {
findByName,
pickKeys,
getPackageVersion,
getNames,
findByNames,
duplicatesByKey,
getCloudDistributionName,
getCloudLogSectionName,
} from "./util/util.js"
import { getCloudDistributionName, getCloudLogSectionName } from "./util/cloud.js"
import { findByName, pickKeys, getPackageVersion, getNames, findByNames, duplicatesByKey } from "./util/util.js"
import type { GardenError } from "./exceptions.js"
import {
ConfigurationError,
Expand Down
43 changes: 43 additions & 0 deletions core/src/util/cloud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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 { DEFAULT_GARDEN_CLOUD_DOMAIN } from "../constants.js"

export type CloudDistroName = "Cloud Dashboard" | "Garden Enterprise" | "Garden Cloud"

/**
* Returns "Garden Cloud" if domain matches https://<some-subdomain>.app.garden,
* otherwise "Garden Enterprise".
*
* TODO: Return the distribution type from the API and store on the CloudApi class.
*/
export function getCloudDistributionName(domain: string): CloudDistroName {
if (domain === DEFAULT_GARDEN_CLOUD_DOMAIN) {
return "Cloud Dashboard"
}

// TODO: consider using URL object instead.
if (!domain.match(/^https:\/\/.+\.app\.garden$/i)) {
return "Garden Enterprise"
}

return "Garden Cloud"
}

export type CloudLogSectionName = "cloud-dashboard" | "garden-cloud" | "garden-enterprise"

export function getCloudLogSectionName(distroName: CloudDistroName): CloudLogSectionName {
if (distroName === "Cloud Dashboard") {
return "cloud-dashboard"
} else if (distroName === "Garden Cloud") {
return "garden-cloud"
} else if (distroName === "Garden Enterprise") {
return "garden-enterprise"
} else {
return distroName satisfies never
}
}
31 changes: 1 addition & 30 deletions core/src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { isAbsolute, relative } from "node:path"
import type { Readable } from "stream"
import { Writable } from "stream"
import type { PrimitiveMap } from "../config/common.js"
import { DEFAULT_GARDEN_CLOUD_DOMAIN, DOCS_BASE_URL, gardenEnv } from "../constants.js"
import { DOCS_BASE_URL, gardenEnv } from "../constants.js"
import {
ChildProcessError,
InternalError,
Expand Down Expand Up @@ -119,35 +119,6 @@ export function getPackageVersion(): string {
return version
}

type CloudDistroName = "Cloud Dashboard" | "Garden Enterprise" | "Garden Cloud"

/**
* Returns "Garden Cloud" if domain matches https://<some-subdomain>.app.garden,
* otherwise "Garden Enterprise".
*
* TODO: Return the distribution type from the API and store on the CloudApi class.
*/
export function getCloudDistributionName(domain: string): CloudDistroName {
if (domain === DEFAULT_GARDEN_CLOUD_DOMAIN) {
return "Cloud Dashboard"
}

if (!domain.match(/^https:\/\/.+\.app\.garden$/i)) {
return "Garden Enterprise"
}
return "Garden Cloud"
}

export function getCloudLogSectionName(distroName: CloudDistroName): string {
if (distroName === "Cloud Dashboard") {
return "cloud-dashboard"
} else if (distroName === "Garden Cloud") {
return "garden-cloud"
} else {
return "garden-enterprise"
}
}

export async function sleep(msec: number) {
return new Promise((resolve) => setTimeout(resolve, msec))
}
Expand Down
56 changes: 56 additions & 0 deletions core/test/unit/src/util/cloud.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 { describe } from "mocha"
import { getCloudDistributionName, getCloudLogSectionName } from "../../../../src/util/cloud.js"
import { DEFAULT_GARDEN_CLOUD_DOMAIN } from "../../../../src/constants.js"
import { expect } from "chai"

describe("garden-cloud", () => {
describe("getCloudDistributionName", () => {
context(`when domain name is ${DEFAULT_GARDEN_CLOUD_DOMAIN}`, () => {
it(`returns "Cloud Dashboard" for ${DEFAULT_GARDEN_CLOUD_DOMAIN}`, () => {
expect(getCloudDistributionName(DEFAULT_GARDEN_CLOUD_DOMAIN)).to.eql("Cloud Dashboard")
})
})

context("when top-level domain is .garden", () => {
context("when 2nd level domain is .app", () => {
it(`returns "Garden Cloud" for https urls`, () => {
expect(getCloudDistributionName("https://backend.app.garden")).to.eql("Garden Cloud")
})
})

context("when 2nd level domain is not .app", () => {
it(`returns "Garden Enterprise" for https urls`, () => {
expect(getCloudDistributionName("https://backend.demo.garden")).to.eql("Garden Enterprise")
})
})
})

context("when domain is something else", () => {
it(`returns "Garden Enterprise" for https urls`, () => {
expect(getCloudDistributionName("https://app.garden-proxy.net")).to.eql("Garden Enterprise")
})
})
})

describe("getCloudLogSectionName", () => {
it(`returns "cloud-dashboard" for "Cloud Dashboard"`, () => {
expect(getCloudLogSectionName("Cloud Dashboard")).to.eql("cloud-dashboard")
})

it(`returns "garden-cloud" for "Garden Cloud"`, () => {
expect(getCloudLogSectionName("Garden Cloud")).to.eql("garden-cloud")
})

it(`returns "garden-enterprise" for "Garden Enterprise"`, () => {
expect(getCloudLogSectionName("Garden Enterprise")).to.eql("garden-enterprise")
})
})
})

0 comments on commit a7c7816

Please sign in to comment.