44[ ![ License] ( https://img.shields.io/badge/License-Apache_2.0-blue.svg )] ( https://opensource.org/licenses/Apache-2.0 )
55[ ![ codecov] ( https://codecov.io/gh/ncode/tagit/graph/badge.svg?token=ISXEH274YD )] ( https://codecov.io/gh/ncode/tagit )
66
7- TagIt is a tool that updates Consul service registration tags with outputs of a script. It copies the current service registration and appends the output of the script line by line as tags, while keeping the original tags.
7+ TagIt is a tool that updates Consul service registration tags with script output.
8+ It keeps the existing registration, replaces managed tags that match ` tag-prefix ` (using the ` prefix-value ` format), and preserves non-prefixed tags.
89
910## Table of Contents
1011
@@ -15,7 +16,10 @@ TagIt is a tool that updates Consul service registration tags with outputs of a
1516 - [ Cleanup Command] ( #cleanup-command )
1617 - [ Systemd Command] ( #systemd-command )
1718- [ How It Works] ( #how-it-works )
19+ - [ Configuration] ( #configuration )
1820- [ Examples] ( #examples )
21+ - [ Development] ( #development )
22+ - [ Troubleshooting] ( #troubleshooting )
1923- [ Contributing] ( #contributing )
2024- [ License] ( #license )
2125
@@ -35,9 +39,9 @@ Here are some scenarios where TagIt can be useful:
3539To install TagIt, use the following commands:
3640
3741``` bash
38- $ git clone https://github.com/ncode/tagit
39- $ cd tagit
40- $ go build
42+ git clone https://github.com/ncode/tagit
43+ cd tagit
44+ go build -o tagit .
4145```
4246
4347## Usage
@@ -49,23 +53,37 @@ TagIt provides three main commands: `run`, `cleanup`, and `systemd`.
4953The ` run ` command starts TagIt and continuously updates the tags based on the script output:
5054
5155``` bash
52- $ ./tagit run --consul-addr=127.0.0.1:8500 --service-id=my-service1 --script=./examples/tagit/example.sh --interval=5s --tag-prefix=tagit
56+ ./tagit run \
57+ --consul-addr=127.0.0.1:8500 \
58+ --service-id=my-service1 \
59+ --script=/tmp/tagit-query.sh \
60+ --interval=5s \
61+ --tag-prefix=tagit
5362```
5463
5564### Cleanup Command
5665
5766The ` cleanup ` command removes all tags with the specified prefix from the service:
5867
5968``` bash
60- $ ./tagit cleanup --consul-addr=127.0.0.1:8500 --service-id=my-service1 --tag-prefix=tagit
69+ ./tagit cleanup \
70+ --consul-addr=127.0.0.1:8500 \
71+ --service-id=my-service1 \
72+ --tag-prefix=tagit
6173```
6274
6375### Systemd Command
6476
6577The ` systemd ` command generates a systemd service file for TagIt:
6678
6779``` bash
68- ./tagit systemd --service-id=my-service1 --script=./examples/tagit/example.sh --tag-prefix=tagit --interval=5s --user=tagit --group=tagit
80+ ./tagit systemd \
81+ --service-id=my-service1 \
82+ --script=/tmp/tagit-query.sh \
83+ --tag-prefix=tagit \
84+ --interval=5s \
85+ --user=tagit \
86+ --group=tagit
6987```
7088
7189This command will output a systemd service file that you can use to run TagIt as a system service.
@@ -76,16 +94,37 @@ TagIt interacts with Consul as follows:
7694
7795``` mermaid
7896sequenceDiagram
79- participant tagit
80- participant consul
81- loop execute script on interval
82- tagit->>consul: Query service with ID
83- consul->>tagit: Return current service registration
84- tagit->>tagit: Execute script and process output
85- tagit->>consul: Update service registration with new tags
86- end
97+ participant tagit
98+ participant consul
99+ loop execute script on interval
100+ tagit->>consul: Query service with ID
101+ consul->>tagit: Return current service registration
102+ tagit->>tagit: Execute script and process output
103+ tagit->>consul: Update service registration with new tags
104+ end
87105```
88106
107+ ## Configuration
108+
109+ TagIt supports configuration through:
110+
111+ - ** CLI flags** (` --consul-addr ` , ` --service-id ` , ` --script ` , ` --tag-prefix ` , ` --interval ` , ` --token ` )
112+ - ** Config file** with ` --config ` (default: ` $HOME/.tagit.yaml ` )
113+ - ** Environment variables** (Viper automatic binding)
114+
115+ Example ` ~/.tagit.yaml ` :
116+
117+ ``` yaml
118+ consul-addr : " 127.0.0.1:8500"
119+ service-id : " my-service1"
120+ script : " /path/to/tagit-query.sh"
121+ tag-prefix : " tagit"
122+ interval : " 5s"
123+ token : " your-consul-token"
124+ ` ` `
125+
126+ Note: ` run` and `cleanup` use inherited root flags, while `systemd` defines and validates its own flags.
127+
89128# # Examples
90129
91130Here's an example of how to test TagIt :
@@ -95,26 +134,141 @@ Here's an example of how to test TagIt:
95134 consul agent -dev &
96135 ` ` `
97136
98- 2 . Register a service with Consul:
137+ 2. Create a script that emits dynamic tags (whitespace-separated output) :
138+ ` ` ` bash
139+ cat >/tmp/tagit-query.sh <<'EOF'
140+ #!/usr/bin/env sh
141+ echo primary replica
142+ EOF
143+ chmod +x /tmp/tagit-query.sh
144+ ` ` `
145+
146+ 3. Register a service with Consul :
99147 ` ` ` bash
100- curl --request PUT --data @examples/consul/my-service1.json http://127.0.0.1:8500/v1/agent/service/register
148+ cat >/tmp/my-service1.json <<'EOF'
149+ {
150+ "ID": "my-service1",
151+ "Name": "my-service1",
152+ "Address": "127.0.0.1",
153+ "Port": 8080,
154+ "Tags": ["static"]
155+ }
156+ EOF
157+ curl --request PUT --data @/tmp/my-service1.json http://127.0.0.1:8500/v1/agent/service/register
101158 ` ` `
102159
103- 3 . Run TagIt:
160+ 4 . Run TagIt :
104161 ` ` ` bash
105- ./tagit run --consul-addr=127.0.0.1:8500 --service-id=my-service1 --script=./examples/tagit/example.sh --interval=5s --tag-prefix=tagit
162+ ./tagit run \
163+ --consul-addr=127.0.0.1:8500 \
164+ --service-id=my-service1 \
165+ --script=/tmp/tagit-query.sh \
166+ --interval=5s \
167+ --tag-prefix=tagit
106168 ` ` `
107169
108- 4 . Generate a systemd service file:
170+ 5 . Generate a systemd service file :
109171 ` ` ` bash
110- ./tagit systemd --service-id=my-service1 --script=./examples/tagit/example.sh --tag-prefix=tagit --interval=5s --user=tagit --group=tagit > /etc/systemd/system/tagit-my-service1.service
172+ ./tagit systemd \
173+ --service-id=my-service1 \
174+ --script=/tmp/tagit-query.sh \
175+ --tag-prefix=tagit \
176+ --interval=5s \
177+ --user=tagit \
178+ --group=tagit > /etc/systemd/system/tagit-my-service1.service
111179 ` ` `
112180
113- 5 . Clean up the tags:
181+ 6 . Clean up the tags :
114182 ` ` ` bash
115- ./tagit cleanup --consul-addr=127.0.0.1:8500 --service-id=my-service1 --tag-prefix=tagit
183+ ./tagit cleanup \
184+ --consul-addr=127.0.0.1:8500 \
185+ --service-id=my-service1 \
186+ --tag-prefix=tagit
116187 ` ` `
117188
189+ # # Development
190+
191+ Use the Makefile targets for local development and testing :
192+
193+ - Build the binary :
194+ ` ` ` bash
195+ make build
196+ ` ` `
197+
198+ - Run unit tests :
199+ ` ` ` bash
200+ make test
201+ ` ` `
202+
203+ - Run integration tests using Docker Compose (`docker-compose.test.yml`) :
204+ ` ` ` bash
205+ make consul-up
206+ make test-integration
207+ make consul-down
208+ ` ` `
209+
210+ - Run all tests including integration with local Consul lifecycle :
211+ ` ` ` bash
212+ make test-all
213+ ` ` `
214+
215+ # # Troubleshooting
216+
217+ # ## Common issues
218+
219+ - ` service-id is required`
220+ - Provide `--service-id` in CLI args or in `~/.tagit.yaml`.
221+
222+ - ` script is required` / `interval is required and cannot be empty or zero`
223+ - ` --script` must be present.
224+ - ` --interval` must be a valid duration (for example `5s`, `30s`, `1m`), not `0` or empty.
225+
226+ - ` failed to create Consul client`
227+ - Verify `--consul-addr` points to a reachable Consul instance (default `127.0.0.1:8500`).
228+ - Confirm Consul is running before retrying.
229+
230+ - `error getting service : ...`
231+ - Confirm the service exists in Consul with the exact `service-id`.
232+
233+ - ` error running script` or command parse/exec errors
234+ - ` script` is executed through shell tokenization; ensure quoting is correct.
235+ - Example with spaces : ` --script='echo primary replica'` .
236+
237+ - No visible updates in Consul
238+ - TagIt is idempotent by design; if managed tags are already up-to-date, it skips the write.
239+
240+ - ` script execution timed out after 30s`
241+ - The default command timeout is 30 seconds.
242+ - Reduce script runtime or shorten work done inside the script.
243+
244+ # ## `systemd` command tips
245+
246+ - Missing required flag errors (for example : ` required flag(s) "group" not set` )
247+ - `systemd` requires : ` --service-id` , `--script`, `--tag-prefix`, `--interval`, `--user`, `--group`.
248+
249+ - ` failed to execute template`
250+ - Re-check generated values and ensure flags are valid before saving output.
251+
252+ # ## Quick checks
253+
254+ - Verify Consul leader endpoint :
255+ ` ` ` bash
256+ curl --silent http://127.0.0.1:8500/v1/status/leader
257+ ` ` `
258+
259+ - Verify service exists in Consul :
260+ ` ` ` bash
261+ curl --silent http://127.0.0.1:8500/v1/agent/services | jq '."my-service1"'
262+ ` ` `
263+
264+ - Validate cleanup command behavior :
265+ ` ` ` bash
266+ ./tagit cleanup \
267+ --consul-addr=127.0.0.1:8500 \
268+ --service-id=my-service1 \
269+ --tag-prefix=tagit
270+ ` ` `
271+
118272# # Contributing
119273
120274Contributions to TagIt are welcome! Please feel free to submit a Pull Request.
0 commit comments