Skip to content

Commit

Permalink
improvement(commands): add --skip-detail and --only-deploys flags
Browse files Browse the repository at this point in the history
This commit adds a couple of "internal" flags to the sync status and get
status commands that make it easier for Cloud and Desktop to consume
their output.

Note that the "garden get status" command will be replaced with a top
level "garden status" command and that these changes aren't really meant
to be user facing. Rather just to unblock a few things on the Cloud
side.
  • Loading branch information
eysi09 committed May 22, 2023
1 parent 6790379 commit 6130c9e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 30 deletions.
72 changes: 59 additions & 13 deletions core/src/commands/get/get-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import Bluebird from "bluebird"
import { fromPairs } from "lodash"
import { fromPairs, omit } from "lodash"
import { deepFilter } from "../../util/objects"
import { Command, CommandResult, CommandParams } from "../base"
import { ResolvedConfigGraph } from "../../graph/config-graph"
Expand All @@ -24,6 +24,7 @@ import { getRunResultSchema, RunStatusMap } from "../../plugin/handlers/Run/get-
import { DeployStatusMap, getDeployStatusSchema } from "../../plugin/handlers/Deploy/get-status"
import { ActionRouter } from "../../router/router"
import { sanitizeValue } from "../../util/logging"
import { BooleanParameter } from "../../cli/params"

// Value is "completed" if the test/task has been run for the current version.
export interface StatusCommandResult {
Expand All @@ -36,11 +37,30 @@ export interface StatusCommandResult {
}
}

const getStatusOpts = {
"skip-detail": new BooleanParameter({
help: deline`
Skip plugin specific details. Only applicable when using the --output=json|yaml option.
Useful for trimming down the output.
`,
}),
"only-deploys": new BooleanParameter({
hidden: true,
help: deline`
[INTERNAL]: Only return statuses of deploy actions. Currently only used by Cloud and Desktop apps.
Will be replaced by a new, top level \`garden status\` command.
`,
}),
}

type Opts = typeof getStatusOpts

export class GetStatusCommand extends Command {
name = "status"
help = "Outputs the full status of your project/environment and all actions."

streamEvents = true
streamEvents = false
options = getStatusOpts

outputsSchema = () =>
joi.object().keys({
Expand All @@ -59,20 +79,32 @@ export class GetStatusCommand extends Command {
printHeader(log, "Get status", "📟")
}

async action({ garden, log }: CommandParams): Promise<CommandResult<StatusCommandResult>> {
async action({ garden, log, opts }: CommandParams<{}, Opts>): Promise<CommandResult<StatusCommandResult>> {
const router = await garden.getActionRouter()
const graph = await garden.getResolvedConfigGraph({ log, emit: true })

const envStatus = await garden.getEnvironmentStatus(log)

let result: StatusCommandResult = {
providers: envStatus,
actions: await Bluebird.props({
Build: getBuildStatuses(router, graph, log),
Deploy: router.getDeployStatuses({ log, graph }),
Test: getTestStatuses(router, graph, log),
Run: getRunStatuses(router, graph, log),
}),
let result: StatusCommandResult
if (opts["only-deploys"]) {
result = {
providers: {},
actions: await Bluebird.props({
Build: {},
Deploy: router.getDeployStatuses({ log, graph }),
Test: {},
Run: {},
}),
}
} else {
const envStatus = await garden.getEnvironmentStatus(log)
result = {
providers: envStatus,
actions: await Bluebird.props({
Build: getBuildStatuses(router, graph, log),
Deploy: router.getDeployStatuses({ log, graph }),
Test: getTestStatuses(router, graph, log),
Run: getRunStatuses(router, graph, log),
}),
}
}

const deployStatuses = result.actions.Deploy
Expand All @@ -91,6 +123,20 @@ export class GetStatusCommand extends Command {
}
}

// We only skip detail for Deploy actions. Note that this is mostly used internally and that this command
// will be replaced by a top-level "garden status" command. For that one we'll probably wan to pass the
// --skip-detail flag to the plugin handlers.
if (opts["skip-detail"]) {
const deployActions = Object.entries(result.actions["Deploy"]).reduce((acc, val) => {
const [name, status] = val
const statusWithOutDetail = omit(status, "detail.detail")
acc[name] = statusWithOutDetail

return acc
}, {} as StatusCommandResult["actions"]["Deploy"])
result["actions"]["Deploy"] = deployActions
}

// TODO: we should change the status format because this will remove services called "detail"
const sanitized = sanitizeValue(deepFilter(result, (_, key) => key !== "executedAction"))

Expand Down
42 changes: 29 additions & 13 deletions core/src/commands/sync/sync-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Bluebird from "bluebird"
import chalk from "chalk"

import { StringsParameter } from "../../cli/params"
import { BooleanParameter, StringsParameter } from "../../cli/params"
import { joi } from "../../config/common"
import { printHeader } from "../../logger/util"
import { dedent, deline, naturalList } from "../../util/string"
Expand All @@ -18,7 +18,7 @@ import { createActionLog } from "../../logger/log-entry"
import { PluginEventBroker } from "../../plugin-context"
import { resolvedActionToExecuted } from "../../actions/helpers"
import { GetSyncStatusResult } from "../../plugin/handlers/Deploy/get-sync-status"
import { isEmpty } from "lodash"
import { isEmpty, omit } from "lodash"

const syncStatusArgs = {
names: new StringsParameter({
Expand All @@ -33,7 +33,18 @@ const syncStatusArgs = {
},
}),
}

const syncStatusOpts = {
"skip-detail": new BooleanParameter({
help: deline`
Skip plugin specific sync details. Only applicable when using the --output=json|yaml option.
Useful for trimming down the output.
`,
}),
}

type Args = typeof syncStatusArgs
type Opts = typeof syncStatusOpts

interface SyncStatusCommandResult {
result: {
Expand All @@ -43,13 +54,14 @@ interface SyncStatusCommandResult {
}
}

export class SyncStatusCommand extends Command<Args> {
export class SyncStatusCommand extends Command<Args, Opts> {
name = "status"
help = "Get sync statuses."

protected = true

arguments = syncStatusArgs
options = syncStatusOpts

description = dedent`
Get the current status of the configured syncs for this project.
Expand All @@ -74,9 +86,10 @@ export class SyncStatusCommand extends Command<Args> {
printHeader(log, "Getting sync statuses", "📟")
}

async action({ garden, log, args }: CommandParams<Args>): Promise<SyncStatusCommandResult> {
async action({ garden, log, args, opts }: CommandParams<Args, Opts>): Promise<SyncStatusCommandResult> {
const router = await garden.getActionRouter()
const graph = await garden.getResolvedConfigGraph({ log, emit: true })
const skipDetail = opts["skip-detail"]

// We default to getting the sync status for all actions
const names = args.names || ["*"]
Expand Down Expand Up @@ -109,15 +122,18 @@ export class SyncStatusCommand extends Command<Args> {
log: actionLog,
})
const executedAction = resolvedActionToExecuted(action, { status })
const syncStatus = (
await router.deploy.getSyncStatus({
log: actionLog,
action: executedAction,
monitor: false,
graph,
events,
})
).result
const syncStatus = omit(
(
await router.deploy.getSyncStatus({
log: actionLog,
action: executedAction,
monitor: false,
graph,
events,
})
).result,
skipDetail ? "detail" : ""
)

const syncs = syncStatus.syncs
if (!syncs || syncs.length === 0) {
Expand Down
10 changes: 8 additions & 2 deletions core/test/unit/src/commands/get/get-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ describe("GetStatusCommand", () => {
const { result } = await garden.runCommand({
command,
args: {},
opts: {},
opts: {
"skip-detail": false,
"only-deploys": false,
},
})

expect(result).to.eql({
Expand Down Expand Up @@ -178,7 +181,10 @@ describe("GetStatusCommand", () => {
garden,
log,
args: {},
opts: withDefaultGlobalOpts({}),
opts: withDefaultGlobalOpts({
"skip-detail": false,
"only-deploys": false,
}),
})

const logMessages = getLogMessages(log, (l) => l.level === LogLevel.warn)
Expand Down
14 changes: 12 additions & 2 deletions docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -2512,8 +2512,13 @@ modules:

#### Usage

garden get status
garden get status [options]

#### Options

| Argument | Alias | Type | Description |
| -------- | ----- | ---- | ----------- |
| `--skip-detail` | | boolean | Skip plugin specific details. Only applicable when using the --output&#x3D;json|yaml option. Useful for trimming down the output.

#### Outputs

Expand Down Expand Up @@ -3534,14 +3539,19 @@ Examples:

#### Usage

garden sync status [names]
garden sync status [names] [options]

#### Arguments

| Argument | Required | Description |
| -------- | -------- | ----------- |
| `names` | No | The name(s) of the Deploy(s) to get the sync status for (skip to get status from all Deploys in the project). You may specify multiple names, separated by space.

#### Options

| Argument | Alias | Type | Description |
| -------- | ----- | ---- | ----------- |
| `--skip-detail` | | boolean | Skip plugin specific sync details. Only applicable when using the --output&#x3D;json|yaml option. Useful for trimming down the output.


### garden test
Expand Down

0 comments on commit 6130c9e

Please sign in to comment.