Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ TagIt supports configuration through:

- **CLI flags** (`--consul-addr`, `--service-id`, `--script`, `--tag-prefix`, `--interval`, `--token`)
- **Config file** with `--config` (default: `$HOME/.tagit.yaml`)
- **Environment variables** (Viper automatic binding)
- **Environment variables** using `TAGIT_*` names

Example `~/.tagit.yaml`:

Expand All @@ -123,7 +123,9 @@ interval: "5s"
token: "your-consul-token"
```

Note: `run` and `cleanup` use inherited root flags, while `systemd` defines and validates its own flags.
Configuration precedence is: CLI flags, `TAGIT_*` environment variables, config file values, then CLI flag defaults.
Environment variable names replace `-` with `_`: `TAGIT_CONSUL_ADDR`, `TAGIT_SERVICE_ID`, `TAGIT_SCRIPT`, `TAGIT_TAG_PREFIX`, `TAGIT_INTERVAL`, and `TAGIT_TOKEN`.
The `run`, `cleanup`, and `systemd` commands resolve shared TagIt invocation values through the same validation path; `systemd` also requires `--user` and `--group`.

## Examples

Expand Down
47 changes: 1 addition & 46 deletions cmd/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ limitations under the License.
package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/ncode/tagit/pkg/consul"
"github.com/ncode/tagit/pkg/tagit"
"github.com/spf13/cobra"
)

Expand All @@ -30,46 +24,7 @@ var cleanupCmd = &cobra.Command{
Use: "cleanup",
Short: "cleanup removes all services with the tag prefix from a given consul service",
RunE: func(cmd *cobra.Command, args []string) error {
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
Level: slog.LevelInfo,
}))

consulAddr := cmd.InheritedFlags().Lookup("consul-addr").Value.String()
token := cmd.InheritedFlags().Lookup("token").Value.String()

consulClient, err := consul.CreateClient(consulAddr, token)
if err != nil {
logger.Error("Failed to create Consul client", "error", err)
return err
}

serviceID := cmd.InheritedFlags().Lookup("service-id").Value.String()
if serviceID == "" {
logger.Error("Service ID is required")
return fmt.Errorf("service-id is required")
}
tagPrefix := cmd.InheritedFlags().Lookup("tag-prefix").Value.String()

t := tagit.New(
consulClient,
&tagit.CmdExecutor{},
serviceID,
"", // script is not needed for cleanup
0, // interval is not needed for cleanup
tagPrefix,
logger,
)

logger.Info("Starting tag cleanup", "serviceID", serviceID, "tagPrefix", tagPrefix)

err = t.CleanupTags()
if err != nil {
logger.Error("Failed to clean up tags", "error", err)
return fmt.Errorf("failed to clean up tags: %w", err)
}

logger.Info("Tag cleanup completed successfully")
return nil
return cleanupCommand(cmd, commandDeps{})
},
}

Expand Down
Loading