Skip to content

Commit

Permalink
improvement: log namespace and cloud URL
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 authored and thsig committed Sep 16, 2021
1 parent 39d72d2 commit c22b47b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 22 deletions.
6 changes: 6 additions & 0 deletions core/src/cli/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import { DashboardEventStream } from "../server/dashboard-event-stream"
import { GardenPluginCallback } from "../types/plugin/plugin"
import { renderError } from "../logger/renderers"
import { EnterpriseApi } from "../enterprise/api"
import chalk = require("chalk")

export async function makeDummyGarden(root: string, gardenOpts: GardenOpts) {
const environments = gardenOpts.environmentName
Expand Down Expand Up @@ -236,13 +237,18 @@ ${renderCommands(commands)}

contextOpts.persistent = persistent
const { streamEvents, streamLogEntries } = command
// Print header log before we know the namespace to prevent content from
// jumping.
// TODO: Link to Cloud namespace page here.
const nsLog = headerLog.info(chalk.gray(`Running in namespace ...\n`))

do {
try {
if (command.noProject) {
garden = await makeDummyGarden(root, contextOpts)
} else {
garden = await Garden.factory(root, contextOpts)
nsLog.setState(`${chalk.gray(`Running in namespace ${chalk.white.bold(garden.namespace)}\n`)}`)

if (processRecord) {
// Update the db record for the process
Expand Down
8 changes: 1 addition & 7 deletions core/src/commands/enterprise/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { GetProjectResponse, SecretResponse, UserResponse } from "@garden-io/platform-api-types"
import { EnterpriseApi } from "../../enterprise/api"
import { SecretResponse, UserResponse } from "@garden-io/platform-api-types"
import { dedent } from "../../util/string"

import { LogEntry } from "../../logger/log-entry"
Expand Down Expand Up @@ -61,11 +60,6 @@ export const noApiMsg = (action: string, resource: string) => dedent`
Unable to ${action} ${resource}. Make sure the project is configured for Garden Enterprise and that you're logged in.
`

export async function getProject(api: EnterpriseApi, projectUid: string) {
const res = await api.get<GetProjectResponse>(`/projects/uid/${projectUid}`)
return res.data
}

export function makeUserFromResponse(user: UserResponse): UserResult {
return {
id: user.id,
Expand Down
11 changes: 2 additions & 9 deletions core/src/commands/enterprise/secrets/secrets-create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,7 @@ import { readFile } from "fs-extra"

import { printHeader } from "../../../logger/util"
import { Command, CommandParams, CommandResult } from "../../base"
import {
ApiCommandError,
getProject,
handleBulkOperationResult,
makeSecretFromResponse,
noApiMsg,
SecretResult,
} from "../helpers"
import { ApiCommandError, handleBulkOperationResult, makeSecretFromResponse, noApiMsg, SecretResult } from "../helpers"
import { dedent, deline } from "../../../util/string"
import { StringsParameter, PathParameter, IntegerParameter, StringParameter } from "../../../cli/params"
import { StringMap } from "../../../config/common"
Expand Down Expand Up @@ -125,7 +118,7 @@ export class SecretsCreateCommand extends Command<Args, Opts> {
throw new ConfigurationError(noApiMsg("create", "secrets"), {})
}

const project = await getProject(api, api.projectId)
const project = await api.getProject()
let environmentId: number | undefined

if (envName) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/commands/enterprise/secrets/secrets-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { ListSecretsResponse } from "@garden-io/platform-api-types"
import { printHeader } from "../../../logger/util"
import { dedent, deline, renderTable } from "../../../util/string"
import { Command, CommandParams, CommandResult } from "../../base"
import { applyFilter, getProject, makeSecretFromResponse, noApiMsg, SecretResult } from "../helpers"
import { applyFilter, makeSecretFromResponse, noApiMsg, SecretResult } from "../helpers"
import chalk from "chalk"
import { sortBy } from "lodash"
import { StringsParameter } from "../../../cli/params"
Expand Down Expand Up @@ -61,7 +61,7 @@ export class SecretsListCommand extends Command<{}, Opts> {
throw new ConfigurationError(noApiMsg("list", "secrets"), {})
}

const project = await getProject(api, api.projectId)
const project = await api.getProject()

const q = stringify({ projectId: project.id })
const res = await api.get<ListSecretsResponse>(`/secrets?${q}`)
Expand Down
4 changes: 2 additions & 2 deletions core/src/commands/enterprise/users/users-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { GetAllUsersResponse } from "@garden-io/platform-api-types"
import { printHeader } from "../../../logger/util"
import { dedent, deline, renderTable } from "../../../util/string"
import { Command, CommandParams, CommandResult } from "../../base"
import { applyFilter, getProject, makeUserFromResponse, noApiMsg, UserResult } from "../helpers"
import { applyFilter, makeUserFromResponse, noApiMsg, UserResult } from "../helpers"
import chalk from "chalk"
import { sortBy } from "lodash"
import { StringsParameter } from "../../../cli/params"
Expand Down Expand Up @@ -55,7 +55,7 @@ export class UsersListCommand extends Command<{}, Opts> {
throw new ConfigurationError(noApiMsg("list", "users"), {})
}

const project = await getProject(api, api.projectId)
const project = await api.getProject()
// Make a best effort VCS provider guess. We should have an API endpoint for this or return with the response.
const vcsProviderTitle = project.repositoryUrl.includes("github.com")
? "GitHub"
Expand Down
12 changes: 12 additions & 0 deletions core/src/enterprise/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { add, sub, isAfter } from "date-fns"
import { isObject } from "lodash"
import { deline } from "../util/string"
import chalk from "chalk"
import { GetProjectResponse } from "@garden-io/platform-api-types"

// If a GARDEN_AUTH_TOKEN is present and Garden is NOT running from a workflow runner pod,
// switch to ci-token authentication method.
Expand Down Expand Up @@ -182,6 +183,12 @@ export class EnterpriseApi {
}

enterpriseLog?.setSuccess({ msg: chalk.green("Done"), append: true })
try {
const project = await api.getProject()
enterpriseLog?.info({ symbol: "info", msg: `Visit project at ${api.domain}/projects/${project.id}` })
} catch (err) {
log.debug(`Getting project from API failed with error: err.message`)
}
return api
}

Expand Down Expand Up @@ -438,6 +445,11 @@ export class EnterpriseApi {
})
}

async getProject() {
const res = await this.get<GetProjectResponse>(`/projects/uid/${this.projectId}`)
return res.data
}

/**
* Checks with the backend whether the provided client auth token is valid.
*/
Expand Down
2 changes: 0 additions & 2 deletions core/src/garden.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1195,8 +1195,6 @@ export class Garden {
domain: this.enterpriseDomain,
}
}

//endregion
}

export async function resolveGardenParams(currentDirectory: string, opts: GardenOpts): Promise<GardenParams> {
Expand Down

0 comments on commit c22b47b

Please sign in to comment.