# iomarmochtar/gitlab-token-updater architecture

> Architecture diagram of the GitHub repository iomarmochtar/gitlab-token-updater, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/iomarmochtar/gitlab-token-updater
- Repository: https://github.com/iomarmochtar/gitlab-token-updater

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Layers
    subgraph "CLI Layer" 
        CLI["CLI Layer"]:::internal
    end
    subgraph "Config Module"
        CFG["Configuration Module"]:::internal
    end
    subgraph "Rotation Engine"
        ENG["Rotation Engine"]:::internal
    end
    subgraph "External Interactions"
        GLCLIENT["GitLab API Client"]:::internal
        SHELL["Hook Executor"]:::internal
    end
    subgraph "External Systems"
        GITLAB["GitLab REST API"]:::external
        LOCALSH["Local Shell / Scripts"]:::scripts
        CICDVAR["CI/CD Variables Store"]:::external
    end
    subgraph "Packaging & Deployment"
        DOCKER["Dockerfile"]:::pack
        MAKE["Makefile"]:::pack
        GOREL["goreleaser.yml"]:::pack
        CIREL[".github/workflows/release.yml"]:::pack
        CITES[".github/workflows/test.yml"]:::pack
    end
    subgraph "Testing Fixtures & Mocks"
        FIX["test/fixtures"]:::test
        MCK_GL["test/mocks/gitlab/gitlab.go"]:::test
        MCK_SH["test/mocks/shell/shell.go"]:::test
    end

    %% Flow
    CLI -->|"load flags (-c, --dry-run, --force, --strict)"| CFG
    CFG -->|"parsed config"| ENG
    ENG -->|"GET token info / POST rotate"| GLCLIENT
    GLCLIENT -->|"REST calls"| GITLAB
    ENG -->|"exec_cmd hooks"| SHELL
    ENG -->|"update_var hooks"| SHELL
    SHELL -->|"spawn shell with GL_NEW_TOKEN"| LOCALSH
    SHELL -->|"update variable via API"| CICDVAR

    %% Decision
    ENG -->|"due? (force/dry-run branch)"| DEC{"Due for rotation?"}:::decision
    DEC -->|yes| ENG
    DEC -->|no| ENG

    %% Packaging flows
    MAKE --> GOREL
    GOREL --> DOCKER
    CIREL --> MAKE
    CITES --> MAKE

    %% Click Events - CLI Layer
    click CLI "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/main.go"
    click CLI "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/main_test.go"
    click CLI "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/app/app.go"
    click CLI "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/app/app_test.go"

    %% Click Events - Config Module
    click CFG "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/pkg/config/config.go"
    click CFG "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/pkg/config/helpers.go"
    click CFG "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/pkg/config/yaml.go"
    click CFG "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/pkg/config/config_test.go"
    click CFG "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/pkg/config/yaml_test.go"

    %% Click Events - Rotation Engine
    click ENG "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/app/app.go"

    %% Click Events - GitLab API Client
    click GLCLIENT "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/pkg/gitlab/gitlab.go"

    %% Click Events - Hook Executor
    click SHELL "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/pkg/shell/shell.go"
    click SHELL "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/pkg/shell/shell_test.go"

    %% Click Events - Examples / Extension Points
    click LOCALSH "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/examples/main_config.yml"
    click LOCALSH "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/examples/internal.yml"
    click LOCALSH "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/examples/scripts/update_secret_manager.sh"
    click LOCALSH "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/examples/.gitlab-ci.yml"

    %% Click Events - Testing Fixtures & Mocks
    click FIX "https://github.com/iomarmochtar/gitlab-token-updater/tree/main/test/fixtures"
    click FIX "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/test/fixtures/sample_script.sh"
    click MCK_GL "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/test/mocks/gitlab/gitlab.go"
    click MCK_SH "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/test/mocks/shell/shell.go"

    %% Click Events - Packaging & Deployment
    click DOCKER "https://github.com/iomarmochtar/gitlab-token-updater/tree/main/Dockerfile"
    click MAKE "https://github.com/iomarmochtar/gitlab-token-updater/tree/main/Makefile"
    click GOREL "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/goreleaser.yml"
    click CIREL "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/.github/workflows/release.yml"
    click CITES "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/.github/workflows/test.yml"
    click CITES "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/scripts/install_goreleaser.sh"
    click CITES "https://github.com/iomarmochtar/gitlab-token-updater/blob/main/scripts/set_version.sh"

    %% Styles
    classDef internal fill:#D0E6FF,stroke:#005B9F,color:#003366;
    classDef external fill:#DFFFD6,stroke:#3C8031,color:#274E13;
    classDef scripts fill:#FFE6C7,stroke:#D66A00,color:#663800;
    classDef pack fill:#F3E6FF,stroke:#7E3FB2,color:#3E1F5B;
    classDef test fill:#F2F2F2,stroke:#AAAAAA,color:#555555;
    classDef decision fill:#FFF5D6,stroke:#D69600,color:#7A4F00;
```
