Skip to content

Commit 9e6b95b

Browse files
committed
Added tag command to add tags to service images
1 parent 27e1886 commit 9e6b95b

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

CLI.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [`jq`](#jq)
2424
* [`jqc`](#jqc)
2525
* [`sh`](#sh)
26+
* [`tag` *[tag]*](#tag-tag)
2627

2728
<!-- tocstop -->
2829

@@ -301,3 +302,31 @@ doco.jqc() { compose-config; local JQ_CMD=(jq-tty); RUN_JQ "$@" <<<"$COMPOSED_JS
301302
doco.sh() { doco exec bash "$@"; }
302303
```
303304

305+
#### `tag` *[tag]*
306+
307+
Tag the current service's `image` with *tag*. If no *tag* is given, outputs the service's `image`.
308+
309+
If *tag* contains a `:`, it is passed to the `docker tag` command as-is. Otherwise, if it contains a `/`, `:latest` will be added to the end of it. If it contains neither a `:` nor a `/`, it is appended to the base image with a `:`.
310+
311+
That is, if a service `foo` has an `image` of `foo/bar:1.2` then:
312+
313+
* `doco foo tag bar/baz:bing` will tag the image as `bar/baz:bing`
314+
* `doco foo tag bar/baz` will tag the image as `bar/baz:latest`
315+
* `doco foo tag latest` will tag the image as `foo/bar:latest`
316+
* `doco foo tag baz` will tag the image as `foo/bar:baz`
317+
318+
Exactly one service must be selected, either explicitly or via the `--tag-default` or`--default` targets. The service must have an `image` key, or the command will fail.
319+
320+
```shell
321+
doco.tag() {
322+
require-services 1 tag || return
323+
REPLY="$(CLEAR_FILTERS; FILTER 'services[%s].image' "$REPLY"; RUN_JQ -r <"$DOCO_CONFIG")"
324+
case ${1-} in
325+
?*:*) docker tag "$REPLY" "$1" ;;
326+
*/*) docker tag "$REPLY" "$1:latest" ;;
327+
'') echo "$REPLY" ;;
328+
*) docker tag "$REPLY" "${REPLY%:*}:$1";;
329+
esac
330+
}
331+
```
332+

bin/doco

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,6 +1033,16 @@ doco.foreach() { target @current foreach doco "$@"; }
10331033
doco.jq() { local JQ_CMD=(jq-tty); RUN_JQ "$@" <"$DOCO_CONFIG"; }
10341034
doco.jqc() { compose-config; local JQ_CMD=(jq-tty); RUN_JQ "$@" <<<"$COMPOSED_JSON"; }
10351035
doco.sh() { doco exec bash "$@"; }
1036+
doco.tag() {
1037+
require-services 1 tag || return
1038+
REPLY="$(CLEAR_FILTERS; FILTER 'services[%s].image' "$REPLY"; RUN_JQ -r <"$DOCO_CONFIG")"
1039+
case ${1-} in
1040+
?*:*) docker tag "$REPLY" "$1" ;;
1041+
*/*) docker tag "$REPLY" "$1:latest" ;;
1042+
'') echo "$REPLY" ;;
1043+
*) docker tag "$REPLY" "${REPLY%:*}:$1";;
1044+
esac
1045+
}
10361046
#!/usr/bin/env bash
10371047
10381048
jq-tty() { if isatty; then tty pager -- command jq -C "$@"; else command jq "$@"; fi; }

specs/CLI-Commands.cram.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,32 @@ If stdout is a TTY, the output is paged (using `$DOCO_PAGER` or `less -FRX`) and
237237
no services specified for sh
238238
[64]
239239
~~~
240+
241+
#### `tag` *[tag]*
242+
243+
Tag the current service's `image` with *tag*. If no *tag* is given, outputs the service's `image`.
244+
245+
If *tag* contains a `:`, it is passed to the `docker tag` command as-is. Otherwise, if it contains a `/`, `:latest` will be added to the end of it. If it contains neither a `:` nor a `/`, it is appended to the base image with a `:`.
246+
247+
That is, if a service `foo` has an `image` of `foo/bar:1.2` then:
248+
249+
* `doco foo tag bar/baz:bing` will tag the image as `bar/baz:bing`
250+
* `doco foo tag bar/baz` will tag the image as `bar/baz:latest`
251+
* `doco foo tag latest` will tag the image as `foo/bar:latest`
252+
* `doco foo tag baz` will tag the image as `foo/bar:baz`
253+
254+
Exactly one service must be selected, either explicitly or via the `--tag-default` or `--default` targets. The service must have an `image` key, or the command will fail.
255+
256+
~~~sh
257+
$ doco example1 tag
258+
bash
259+
260+
$ doco example1 tag bing/bang:boom
261+
docker tag bash bing/bang:boom
262+
263+
$ doco example1 tag bing/bang
264+
docker tag bash bing/bang:latest
265+
266+
$ doco example1 tag boom
267+
docker tag bash bash:boom
268+
~~~

specs/cram-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ loco_site_config() { :;}
99
doco.--dry-run
1010

1111
# default empty compose file
12-
echo '{"version": "2.1", "services": {"example1":{}}}' >docker-compose.yml
12+
echo '{"version": "2.1", "services": {"example1":{"image":"bash"}}}' >docker-compose.yml
1313

1414
# Initialize doco in-process when run without other initialization
1515
doco() { unset -f doco; loco_main "$@"; }

0 commit comments

Comments
 (0)