Skip to content

Commit

Permalink
feat(core): add get linked-repos command
Browse files Browse the repository at this point in the history
This command renders a list of all linked remote project sources and
modules.
  • Loading branch information
thsig committed Feb 24, 2020
1 parent 5647522 commit 5145d3f
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/reference/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,15 @@ Just try it.

garden get eysi

### garden get linked-repos

Outputs a list of all linked remote sources and modules for this project.


##### Usage

garden get linked-repos

### garden get outputs

Resolves and returns the outputs of the project.
Expand Down
53 changes: 53 additions & 0 deletions garden-service/src/commands/get/get-linked-repos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2018-2020 Garden Technologies, Inc. <[email protected]>
*
* 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 chalk from "chalk"
import { sortBy } from "lodash"
import { Command, CommandParams, CommandResult } from "../base"
import { LinkedSource } from "../../config-store"
import { printHeader } from "../../logger/util"
import { getLinkedSources } from "../../util/ext-source-util"
import { renderTable } from "../../util/string"

const getLinkedReposArguments = {}

type Args = typeof getLinkedReposArguments

export class GetLinkedReposCommand extends Command {
name = "linked-repos"
help = "Outputs a list of all linked remote sources and modules for this project."

async action({ garden, log, headerLog }: CommandParams<Args>): Promise<CommandResult<LinkedSource[]>> {
printHeader(headerLog, "List linked modules and sources", "open_book")

const linkedProjectSources = sortBy(await getLinkedSources(garden, "project"), (s) => s.name)
const linkedModuleSources = sortBy(await getLinkedSources(garden, "module"), (s) => s.name)

const linkedSources = [...linkedProjectSources, ...linkedModuleSources]

log.info("")

if (linkedSources.length === 0) {
log.info(chalk.white("No linked sources or modules found for this project."))
} else {
const linkedSourcesWithType = [
...linkedProjectSources.map((s) => ({ ...s, type: "source" })),
...linkedModuleSources.map((s) => ({ ...s, type: "module" })),
]

const rows = [
[chalk.bold("Name:"), chalk.bold("Type:"), chalk.bold("Path:")],
...linkedSourcesWithType.map((s) => [chalk.cyan.bold(s.name), chalk.cyan.bold(s.type), s.path.trim()]),
]

log.info(renderTable(rows))
}

return { result: linkedSources }
}
}
2 changes: 2 additions & 0 deletions garden-service/src/commands/get/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { GetTasksCommand } from "./get-tasks"
import { GetTaskResultCommand } from "./get-task-result"
import { GetTestResultCommand } from "./get-test-result"
import { GetDebugInfoCommand } from "./get-debug-info"
import { GetLinkedReposCommand } from "./get-linked-repos"
import { GetOutputsCommand } from "./get-outputs"

export class GetCommand extends Command {
Expand All @@ -26,6 +27,7 @@ export class GetCommand extends Command {
GetGraphCommand,
GetConfigCommand,
GetEysiCommand,
GetLinkedReposCommand,
GetOutputsCommand,
GetSecretCommand,
GetStatusCommand,
Expand Down
97 changes: 97 additions & 0 deletions garden-service/test/unit/src/commands/get/get-linked-repos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2018-2020 Garden Technologies, Inc. <[email protected]>
*
* 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 { expect } from "chai"
import { join } from "path"
import {
resetLocalConfig,
getDataDir,
withDefaultGlobalOpts,
makeExtModuleSourcesGarden,
makeExtProjectSourcesGarden,
TestGarden,
} from "../../../../helpers"
import { LinkSourceCommand } from "../../../../../src/commands/link/source"
import { LinkModuleCommand } from "../../../../../src/commands/link/module"
import { GetLinkedReposCommand } from "../../../../../src/commands/get/get-linked-repos"

describe("GetLinkedReposCommand", () => {
let garden: TestGarden

afterEach(async () => {
await resetLocalConfig(garden.gardenDirPath)
})

it("should list all linked project sources in the project", async () => {
garden = await makeExtProjectSourcesGarden()
const log = garden.log
const sourcesDir = getDataDir("test-project-local-module-sources")
const linkSourceCmd = new LinkSourceCommand()
const sourceNames = ["source-a", "source-b", "source-c"]
for (const sourceName of sourceNames) {
await linkSourceCmd.action({
garden,
log,
headerLog: log,
footerLog: log,
args: { source: sourceName, path: join(sourcesDir, sourceName) },
opts: withDefaultGlobalOpts({}),
})
}

const getLinkedReposCommand = new GetLinkedReposCommand()
const results = await getLinkedReposCommand.action({
garden,
log,
headerLog: log,
footerLog: log,
args: {},
opts: withDefaultGlobalOpts({}),
})

const expected = sourceNames.map((name) => {
return { name, path: join(sourcesDir, name) }
})

expect(results.result).to.eql(expected)
})

it("should list all linked modules in the project", async () => {
garden = await makeExtModuleSourcesGarden()
const log = garden.log
const sourcesDir = getDataDir("test-project-local-project-sources")
const linkModuleCmd = new LinkModuleCommand()
const sourceNames = ["module-a", "module-b", "module-c"]
for (const moduleName of sourceNames) {
await linkModuleCmd.action({
garden,
log,
headerLog: log,
footerLog: log,
args: { module: moduleName, path: join(sourcesDir, moduleName) },
opts: withDefaultGlobalOpts({}),
})
}

const getLinkedReposCommand = new GetLinkedReposCommand()
const results = await getLinkedReposCommand.action({
garden,
log,
headerLog: log,
footerLog: log,
args: {},
opts: withDefaultGlobalOpts({}),
})

const expected = sourceNames.map((name) => {
return { name, path: join(sourcesDir, name) }
})

expect(results.result).to.eql(expected)
})
})

0 comments on commit 5145d3f

Please sign in to comment.