-
Notifications
You must be signed in to change notification settings - Fork 507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide docker commands in command pallet #24
Merged
Merged
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
ca1759d
docker command integration
kieferrm 148bb02
don't filter images
kieferrm 24029ce
stop all containers
kieferrm c199e55
don't leave stopped containers around
kieferrm 02d5348
first cut compose-support
kieferrm 54424a4
support docker-compose.something.yml files
kieferrm fc8c52b
support remove of images
kieferrm cad179d
bug fix
kieferrm e8906a2
fix build
chrisdias 28b00ff
optimize!
chrisdias 77ffcac
naming
chrisdias 46922eb
add config options
chrisdias 7cbe79d
add push
chrisdias d7fbeec
fix launch config to get debugging working
kieferrm 5354a10
dockerfile in root of workpace
chrisdias a081d16
add interactive run
chrisdias 4d3917e
Merge branch 'feature/commands' of https://github.com/kieferrm/vscode…
chrisdias 0f6c4ec
temp extension
chrisdias 991e9fa
move version back to 0.0.1
chrisdias 40def28
change identity
chrisdias 36a29c4
refactoring
kieferrm 0475b76
make it work on windows
chrisdias d36aa58
remove publisher hack
chrisdias ab9a6f3
two more windows fixes
chrisdias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import vscode = require('vscode'); | ||
|
||
|
||
function hasWorkspaceFolder() : boolean { | ||
return vscode.workspace.rootPath ? true : false; | ||
} | ||
|
||
function getDockerFileUris(): Thenable<vscode.Uri[]>{ | ||
if (!hasWorkspaceFolder()) { | ||
return Promise.resolve(null); | ||
} | ||
return Promise.resolve(vscode.workspace.findFiles('**/[dD]ocker[fF]ile', null, 9999, null)); | ||
} | ||
|
||
interface Item extends vscode.QuickPickItem { | ||
path: string, | ||
file: string | ||
} | ||
|
||
function createItem(uri: vscode.Uri) : Item { | ||
let length = vscode.workspace.rootPath.length; | ||
let label = uri.path.substr(length); | ||
return <Item> { | ||
label: label, | ||
description: null, | ||
path: '.' + label.substr(0, label.length - '/dockerfile'.length), | ||
file: '.' + label | ||
}; | ||
} | ||
|
||
function computeItems(uris: vscode.Uri[]) : vscode.QuickPickItem[] { | ||
let items : vscode.QuickPickItem[] = []; | ||
for (let i = 0; i < uris.length; i++) { | ||
items.push(createItem(uris[i])); | ||
} | ||
return items; | ||
} | ||
|
||
export function buildImage() { | ||
getDockerFileUris().then(function (uris: vscode.Uri[]) { | ||
if (!uris || uris.length == 0) { | ||
vscode.window.showInformationMessage('Couldn\'t find any dockerfile in your workspace.'); | ||
} else { | ||
let items: vscode.QuickPickItem[] = computeItems(uris); | ||
vscode.window.showQuickPick(items, { placeHolder: 'Choose Dockerfile to build' }).then(function(selectedItem : Item) { | ||
if (selectedItem) { | ||
|
||
let imageName = selectedItem.path.split('/').pop().toLowerCase(); | ||
if (imageName === '.') { | ||
imageName = vscode.workspace.rootPath.split('/').pop().toLowerCase(); | ||
} | ||
|
||
let configOptions: vscode.WorkspaceConfiguration = vscode.workspace.getConfiguration('docker'); | ||
|
||
let defaultRegistryPath = configOptions.get('defaultRegistryPath', ''); | ||
if (defaultRegistryPath.length > 0) { | ||
imageName = defaultRegistryPath + '/' + imageName; | ||
} | ||
|
||
let defaultRegistry = configOptions.get('defaultRegistry', ''); | ||
if (defaultRegistry.length > 0) { | ||
imageName = defaultRegistry + '/' + imageName; | ||
} | ||
|
||
let terminal: vscode.Terminal = vscode.window.createTerminal('Docker'); | ||
terminal.sendText(`docker build -f ${selectedItem.file} -t ${imageName} ${selectedItem.path}`); | ||
terminal.show(); | ||
} | ||
}); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import vscode = require('vscode'); | ||
|
||
|
||
function hasWorkspaceFolder() : boolean { | ||
return vscode.workspace.rootPath ? true : false; | ||
} | ||
|
||
function getDockerComposeFileUris(): Thenable<vscode.Uri[]>{ | ||
if (!hasWorkspaceFolder()) { | ||
return Promise.resolve(null); | ||
} | ||
return Promise.resolve(vscode.workspace.findFiles('{**/[dD]ocker-[cC]ompose.*.yml,**/[dD]ocker-[cC]ompose.yml}', null, 9999, null)); | ||
} | ||
|
||
interface Item extends vscode.QuickPickItem { | ||
path: string, | ||
file: string | ||
} | ||
|
||
function createItem(uri: vscode.Uri) : Item { | ||
let length = vscode.workspace.rootPath.length; | ||
let label = uri.path.substr(length); | ||
let slashIndex = label.lastIndexOf('/'); | ||
return <Item> { | ||
label: label, | ||
description: null, | ||
path: '.' + label.substr(0, slashIndex), | ||
file: label.substr(slashIndex + 1) | ||
}; | ||
} | ||
|
||
function computeItems(uris: vscode.Uri[]) : vscode.QuickPickItem[] { | ||
let items : vscode.QuickPickItem[] = []; | ||
for (let i = 0; i < uris.length; i++) { | ||
items.push(createItem(uris[i])); | ||
} | ||
return items; | ||
} | ||
|
||
export function compose(command: string, message: string) { | ||
getDockerComposeFileUris().then(function (uris: vscode.Uri[]) { | ||
if (!uris || uris.length == 0) { | ||
vscode.window.showInformationMessage('Couldn\'t find any docker-compose file in your workspace.'); | ||
} else { | ||
let items: vscode.QuickPickItem[] = computeItems(uris); | ||
vscode.window.showQuickPick(items, { placeHolder: `Choose Docker Compose file ${message}` }).then(function(selectedItem : Item) { | ||
if (selectedItem) { | ||
let terminal: vscode.Terminal = vscode.window.createTerminal('Docker Compose'); | ||
terminal.sendText(`cd ${selectedItem.path}; docker-compose -f ${selectedItem.file} ${command}`); | ||
terminal.show(); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
export function composeUp() { | ||
compose('up', 'to bring up'); | ||
} | ||
|
||
export function composeDown() { | ||
compose('down', 'to take down'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import vscode = require('vscode'); | ||
import {ContainerItem, quickPickContainer} from './utils/quick-pick-container'; | ||
|
||
export function openShellContainer() { | ||
quickPickContainer().then(function (selectedItem: ContainerItem) { | ||
if (selectedItem) { | ||
let terminal = vscode.window.createTerminal(`sh ${selectedItem.label}`); | ||
terminal.sendText(`docker exec -it ${selectedItem.ids[0]} /bin/sh`); | ||
terminal.show(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import vscode = require('vscode'); | ||
import {ImageItem, quickPickImage} from './utils/quick-pick-image'; | ||
|
||
export function pushImage() { | ||
quickPickImage().then(function (selectedItem: ImageItem) { | ||
if (selectedItem) { | ||
let terminal = vscode.window.createTerminal(selectedItem.label); | ||
terminal.sendText(`docker push ${selectedItem.label}`); | ||
terminal.show(); | ||
}; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {docker} from './utils/docker-endpoint'; | ||
import {ImageItem, quickPickImage} from './utils/quick-pick-image'; | ||
|
||
|
||
export function removeImage() { | ||
quickPickImage().then(function (selectedItem: ImageItem) { | ||
if (selectedItem) { | ||
let image = docker.getImage(selectedItem.id); | ||
image.remove({ force: true }, function (err, data) { | ||
// console.log("Removed - error: " + err); | ||
// console.log("Removed - data: " + data); | ||
}); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import vscode = require('vscode'); | ||
import {ContainerItem, quickPickContainer} from './utils/quick-pick-container'; | ||
|
||
|
||
export function showLogsContainer() { | ||
quickPickContainer().then(function (selectedItem: ContainerItem) { | ||
if (selectedItem) { | ||
let terminal = vscode.window.createTerminal(selectedItem.label); | ||
terminal.sendText(`docker logs ${selectedItem.ids[0]}`); | ||
terminal.show(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import vscode = require('vscode'); | ||
import {ImageItem, quickPickImage} from './utils/quick-pick-image'; | ||
|
||
|
||
function doStartContainer(interactive: boolean) { | ||
quickPickImage().then(function (selectedItem: ImageItem) { | ||
if (selectedItem) { | ||
let option = interactive ? '-it' : ''; | ||
let terminal = vscode.window.createTerminal(selectedItem.label); | ||
terminal.sendText(`docker run ${option} --rm ${selectedItem.label}`); | ||
terminal.show(); | ||
} | ||
}); | ||
} | ||
|
||
export function startContainer() { | ||
doStartContainer(false); | ||
} | ||
|
||
export function startContainerInteractive() { | ||
doStartContainer(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {docker} from './utils/docker-endpoint'; | ||
import {ContainerItem, quickPickContainer} from './utils/quick-pick-container'; | ||
|
||
|
||
export function stopContainer() { | ||
quickPickContainer(true).then(function (selectedItem: ContainerItem) { | ||
if (selectedItem) { | ||
for (let i = 0; i < selectedItem.ids.length; i++) { | ||
let container = docker.getContainer(selectedItem.ids[i]); | ||
container.stop(function (err, data) { | ||
// console.log("Stopped - error: " + err); | ||
// console.log("Stopped - data: " + data); | ||
}); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import * as Docker from 'dockerode'; | ||
|
||
|
||
class DockerClient { | ||
|
||
private endPoint:Docker; | ||
|
||
constructor() { | ||
this.endPoint = new Docker({ socketPath: '/var/run/docker.sock' }); | ||
} | ||
|
||
public getContainerDescriptors(): Thenable<Docker.ContainerDesc[]>{ | ||
return new Promise((resolve, reject) => { | ||
this.endPoint.listContainers((err, containers) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(containers); | ||
}); | ||
}); | ||
}; | ||
|
||
public getImageDescriptors(): Thenable<Docker.ImageDesc[]>{ | ||
return new Promise((resolve, reject) => { | ||
this.endPoint.listImages((err, images) => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
return resolve(images); | ||
}); | ||
}); | ||
}; | ||
|
||
public getContainer(id: string): Docker.Container { | ||
return this.endPoint.getContainer(id); | ||
} | ||
|
||
public getImage(id:string): Docker.Image { | ||
return this.endPoint.getImage(id); | ||
} | ||
} | ||
|
||
export const docker = new DockerClient(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import * as Docker from 'dockerode'; | ||
import {docker} from './docker-endpoint'; | ||
import vscode = require('vscode'); | ||
|
||
|
||
export interface ContainerItem extends vscode.QuickPickItem { | ||
ids: string[] | ||
} | ||
|
||
function createItem(container: Docker.ContainerDesc) : ContainerItem { | ||
return <ContainerItem> { | ||
label: container.Image, | ||
description: container.Status, | ||
ids: [ container.Id ] | ||
}; | ||
} | ||
|
||
function computeItems(containers: Docker.ContainerDesc[], includeAll: boolean) : ContainerItem[] { | ||
|
||
let allIds: string[] = []; | ||
|
||
let items : ContainerItem[] = []; | ||
for (let i = 0; i < containers.length; i++) { | ||
let item = createItem(containers[i]); | ||
allIds.push(item.ids[0]); | ||
items.push(item); | ||
} | ||
|
||
if (includeAll && allIds.length > 0) { | ||
items.unshift(<ContainerItem> { | ||
label: 'All Containers', | ||
description: 'Stops all running containers', | ||
ids: allIds | ||
}); | ||
} | ||
|
||
return items; | ||
} | ||
|
||
export function quickPickContainer(includeAll: boolean = false) : Thenable<ContainerItem>{ | ||
return docker.getContainerDescriptors().then(containers => { | ||
if (!containers || containers.length == 0) { | ||
vscode.window.showInformationMessage('There are no running docker containers.'); | ||
return Promise.resolve(null); | ||
} else { | ||
let items: ContainerItem[] = computeItems(containers, includeAll); | ||
return vscode.window.showQuickPick(items, { placeHolder: 'Choose Container' }); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import * as Docker from 'dockerode'; | ||
import {docker} from './docker-endpoint'; | ||
import vscode = require('vscode'); | ||
|
||
|
||
|
||
export interface ImageItem extends vscode.QuickPickItem { | ||
id: string, | ||
} | ||
|
||
function createItem(image: Docker.ImageDesc) : ImageItem { | ||
return <ImageItem> { | ||
label: image.RepoTags[0] || '<none>', | ||
description: null, | ||
id: image.Id | ||
}; | ||
} | ||
|
||
function computeItems(images: Docker.ImageDesc[]) : ImageItem[] { | ||
let items : ImageItem[] = []; | ||
for (let i = 0; i < images.length; i++) { | ||
items.push(createItem(images[i])); | ||
} | ||
return items; | ||
} | ||
|
||
export function quickPickImage() : Thenable<ImageItem> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense to allow removing all images in one command, similarly to how the stop container command works? That would be nice to not have to remember how to type |
||
return docker.getImageDescriptors().then(images => { | ||
if (!images || images.length == 0) { | ||
vscode.window.showInformationMessage('There are no docker images yet. Try Build first.'); | ||
return Promise.resolve(null); | ||
} else { | ||
let items: ImageItem[] = computeItems(images); | ||
return vscode.window.showQuickPick(items, { placeHolder: 'Choose image' }); | ||
} | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to omit the
socketPath
, and instead allow theDOCKER_HOST
env var to determine what this extension connects to? That way, a dev could use these VS Code commands even if they had configured their workstation to manage a remote host (e.g. via docker-machine).