From e9d60d043b6906d2356786db5f6c0ae97211615f Mon Sep 17 00:00:00 2001 From: Conduitry Date: Wed, 11 Jul 2018 09:32:25 -0400 Subject: [PATCH] make calling with specific names also monitor started containers --- docker-windows-watch.js | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/docker-windows-watch.js b/docker-windows-watch.js index 3b0e383..2c4d5c6 100644 --- a/docker-windows-watch.js +++ b/docker-windows-watch.js @@ -1,6 +1,7 @@ const EventEmitter = require('events'); const { stat, watch } = require('fs'); const { request } = require('http'); +const { URLSearchParams } = require('url'); const { promisify } = require('util'); const statAsync = promisify(stat); @@ -111,20 +112,17 @@ const detachWatchers = containerId => { }; (async () => { - if (process.argv.length > 2) { - // attach watchers to specified containers - for (const container of process.argv.slice(2)) { - attachWatchers(container); - } - } else { - // attach watchers to all containers and monitor starting and stopping of containers - stream( - '/events?filters=%7B%22type%22%3A%5B%22container%22%5D%2C%22event%22%3A%5B%22start%22%2C%22die%22%5D%7D', - ).on('', ({ Action, id }) => - (Action === 'start' ? attachWatchers : detachWatchers)(id), - ); - for (const container of await api('get', '/containers/json')) { - attachWatchers(container.Id); - } + // prepare filters + const name = process.argv.slice(2).map(name => `^/${name}$`); + const [streamQuery, initQuery] = [ + { type: ['container'], event: ['start', 'die'], name }, + { name }, + ].map(filters => new URLSearchParams({ filters: JSON.stringify(filters) })); + // attach watchers to all matching containers and monitor starting and stopping of matching containers + stream(`/events?${streamQuery}`).on('', ({ Action, id }) => + (Action === 'start' ? attachWatchers : detachWatchers)(id), + ); + for (const { Id } of await api('get', `/containers/json?${initQuery}`)) { + attachWatchers(Id); } })();