How iomarmochtar/gitlab-token-updater works
Project Type and Purpose
• This is a CLI-based token-rotation/management tool written in Go. It’s neither a full-stack web app nor a library but an infrastructure automation utility that reads a YAML config, interacts with the GitLab API, and optionally executes local shell hooks to update secrets or environment variables.
High-Level File-Structure Insights
• Top-level:
• main.go / app/app.go: CLI entrypoints
• pkg/: core logic modules (config loader, GitLab client, shell executor)
• examples/: sample configs and scripts showing extension points
• test/: fixtures and mocks for unit tests
• .github/, .devcontainer/, Dockerfile, Makefile, goreleaser.yml: CI/CD and packaging
• Pattern: a layered, modular design separating CLI parsing, configuration, domain logic (rotation), and external interactions.
Components
a. CLI Layer (main)
• Parses flags (-c, --dry-run, etc.)
• Initializes subcomponents
b. Configuration Module
• pkg/config: reads YAML (with env substitutions), validates and constructs in-memory config objects
c. Rotation Engine
• app/app.go: iterates over manage_tokens entries, decides when to rotate
d. GitLab API Client
• pkg/gitlab: calls read and rotate endpoints (uses Personal Access Token)
e. Hook Executor
• pkg/shell: runs exec_cmd hooks
• update_var logic (via GitLab client) for update_var hooks
f. External Interfaces
• GitLab instance (external REST API)
• Local shell / external scripts (examples/scripts)
• CI/CD variables store (via update_var hooks)
g. Packaging & Deploy
• Docker container (Dockerfile)
• goreleaser / Makefile (build pipelines)
Relationships and Data Flow
• User invokes CLI → CLI Layer reads config → Configuration Module
• Rotation Engine consumes config → for each token:
• Query GitLab API → determine expiry
• If due, call GitLab API to rotate token
• Pass new token to Hook Executor:
– update_var hooks → GitLab API update variable
– exec_cmd hooks → spawn shell with env var GL_NEW_TOKEN
• Errors are collected and reported at end (strict vs non-strict mode).
• Dry-run branch: calls GitLab read endpoints only.
• Force branch: bypasses expiration check.
Architectural Patterns & Principles
• Separation of concerns: CLI, config parsing, business logic, external interactions.
• Dependency injection for testing: pkg/gitlab and pkg/shell can be mocked (see test/mocks).
• Hook/Plugin-style extensibility: arbitrary shell commands and variable-update hooks.
• Immutable config objects with environment-variable interpolation.
Essential Details to Capture
• Test/mocks layer to illustrate testability—optional but helpful for maintainers.