Skip to content

Commit

Permalink
refactor: move some logic from commands to plugin context
Browse files Browse the repository at this point in the history
  • Loading branch information
edvald committed Apr 19, 2018
1 parent ec47c72 commit b7173be
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 40 deletions.
21 changes: 5 additions & 16 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,27 +40,16 @@ export class DeployCommand extends Command<typeof deployArgs, typeof deployOpts>
ctx.log.header({ emoji: "rocket", command: "Deploy" })

const names = args.service ? args.service.split(",") : undefined
const services = await ctx.getServices(names)

const result = await deployServices(ctx, values(services), !!opts.force, !!opts["force-build"])
const result = await ctx.deployServices({
names,
force: !!opts.force,
forceBuild: !!opts["force-build"],
})

ctx.log.info("")
ctx.log.info({ emoji: "heavy_check_mark", msg: chalk.green("Done!\n") })

return result
}
}

export async function deployServices(
ctx: PluginContext,
services: Service<any>[],
force: boolean,
forceBuild: boolean,
) {
for (const service of services) {
const task = new DeployTask(ctx, service, force, forceBuild)
await ctx.addTask(task)
}

return await ctx.processTasks()
}
7 changes: 1 addition & 6 deletions src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import { join } from "path"
import { STATIC_DIR } from "../constants"
import { spawnSync } from "child_process"
import chalk from "chalk"
import { deployServices } from "./deploy"
import { values } from "lodash"
import { sleep } from "../util"

const imgcatPath = join(__dirname, "..", "..", "bin", "imgcat")
Expand All @@ -37,10 +35,7 @@ export class DevCommand extends Command {
console.log(chalk.gray.italic(` Good afternoon, Jon! Let's get your environment wired up...\n`))

await ctx.configureEnvironment()

const services = values(await ctx.getServices())

await deployServices(ctx, services, false, false)
await ctx.deployServices({})

ctx.log.info({ msg: "" })
const watchEntry = ctx.log.info({ emoji: "koala", msg: `Waiting for code changes...` })
Expand Down
23 changes: 5 additions & 18 deletions src/commands/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,21 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import Bluebird = require("bluebird")
import { mapValues } from "lodash"
import * as yaml from "js-yaml"
import { PluginContext } from "../plugin-context"
import {
EnvironmentStatusMap,
} from "../types/plugin"
ContextStatus,
PluginContext,
} from "../plugin-context"
import { Command } from "./base"
import { Service } from "../types/service"
import { highlightYaml } from "../util"

export class StatusCommand extends Command {
name = "status"
alias = "s"
help = "Outputs the status of your environment"

async action(ctx: PluginContext) {
const envStatus: EnvironmentStatusMap = await ctx.getEnvironmentStatus()
const services = await ctx.getServices()

const serviceStatus = await Bluebird.props(
mapValues(services, (service: Service<any>) => ctx.getServiceStatus(service)),
)

const status = {
providers: envStatus,
services: serviceStatus,
}
async action(ctx: PluginContext): Promise<ContextStatus> {
const status = await ctx.getStatus()
const yamlStatus = yaml.safeDump(status, { noRefs: true, skipInvalid: true })

// TODO: do a nicer print of this by default and add --yaml/--json options (maybe globally) for exporting
Expand Down
36 changes: 36 additions & 0 deletions src/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
LogEntry,
} from "./logger"
import { EntryStyle } from "./logger/types"
import { DeployTask } from "./tasks/deploy"
import {
PrimitiveMap,
} from "./types/common"
Expand Down Expand Up @@ -55,6 +56,11 @@ export type PluginContextGuard = {
readonly [P in keyof (PluginActionParams | ModuleActionParams<any>)]: (...args: any[]) => Promise<any>
}

export interface ContextStatus {
providers: EnvironmentStatusMap
services: { [name: string]: ServiceStatus }
}

export type WrappedFromGarden = Pick<Garden,
"projectName" |
"projectRoot" |
Expand Down Expand Up @@ -94,6 +100,11 @@ export interface PluginContext extends PluginContextGuard, WrappedFromGarden {
getConfig: (key: string[]) => Promise<string>
setConfig: (key: string[], value: string) => Promise<void>
deleteConfig: (key: string[]) => Promise<DeleteConfigResult>

getStatus: () => Promise<ContextStatus>
deployServices: (
params: { names?: string[], force?: boolean, forceBuild?: boolean, logEntry?: LogEntry },
) => Promise<any>
}

export function createPluginContext(garden: Garden): PluginContext {
Expand Down Expand Up @@ -264,6 +275,31 @@ export function createPluginContext(garden: Garden): PluginContext {
return res
}
},

getStatus: async () => {
const envStatus: EnvironmentStatusMap = await ctx.getEnvironmentStatus()
const services = await ctx.getServices()

const serviceStatus = await Bluebird.props(
mapValues(services, (service: Service<any>) => ctx.getServiceStatus(service)),
)

return {
providers: envStatus,
services: serviceStatus,
}
},

deployServices: async ({ names, force = false, forceBuild = false, logEntry }) => {
const services = await ctx.getServices(names)

for (const service of values(services)) {
const task = new DeployTask(ctx, service, force, forceBuild, logEntry)
await ctx.addTask(task)
}

return ctx.processTasks()
},
}

return ctx
Expand Down

0 comments on commit b7173be

Please sign in to comment.