Skip to content

Commit

Permalink
feat(containers): add CLI flags to allow filtering containers list (#118
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kopiro authored and lirantal committed Sep 12, 2019
1 parent ca6e1f9 commit affa675
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,17 @@ It's also possible to provide command line options for dockly to customize the d
| Param | Type | Description |
| --- | --- | --- |
| -s or --socketPath | string | Docker socket to connect to |
| --containerFilters | string | String to apply to filter shown containers |
| -h or --help | null | Display help |
| -v or --version | null | Display version information |

### `--containerFilters`

This is a string that could be used to filter the shown containers;
its format is in the x-www-form-urlencoded style and the filters you could apply are listed here: [https://docs.docker.com/engine/api/v1.37/#operation/ContainerList](https://docs.docker.com/engine/api/v1.37/#operation/ContainerList)

Example: `--containerFilters="name=test&status=running"` to only show *running* container which name match *test*.

# Docker Support

## Run from docker
Expand Down
5 changes: 5 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ function Cli () {
alias: 'v',
type: Boolean,
description: 'Display version'
},
{
name: 'containerFilters',
type: String,
description: 'Filter containers'
}
]
}
Expand Down
26 changes: 21 additions & 5 deletions src/dockerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@
const DockerLib = require('dockerode')

class Util {
constructor (connection) {
if (typeof connection !== 'object') {
constructor (config) {
if (typeof config !== 'object') {
throw new Error('Error: docker connection string is faulty, please review command line arguments.')
}

// If a socketPath was explicitly specified, attempt connection based on it
if (connection.socketPath) {
this.dockerCon = new DockerLib(connection)
if (config.socketPath) {
this.dockerCon = new DockerLib({
socketPath: config.socketPath
})
} else {
this.dockerCon = new DockerLib()
}

if (config.containerFilters) {
this.containerFilters = JSON.stringify(config.containerFilters.split('&').reduce((carry, e) => {
let [left, right] = e.split('=')
right = right.split(',')
carry[left] = right
return carry
}, {}))
} else {
this.containerFilters = ''
}
}

ping (cb) {
Expand All @@ -39,7 +52,10 @@ class Util {
}

listContainers (cb) {
this.dockerCon.listContainers({ 'all': true }, function (err, containers) {
this.dockerCon.listContainers({
'all': true,
'filters': this.containerFilters
}, function (err, containers) {
if (err) {
return cb(err, {})
}
Expand Down

0 comments on commit affa675

Please sign in to comment.