# gerardog/gsudo architecture

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

- Interactive diagram: https://gitdiagram.com/gerardog/gsudo
- Repository: https://github.com/gerardog/gsudo

## Overview

##### Main Components
- Command-Line Interface (CLI) Layer: This is the entry point. The executable (gsudo.exe) and its corresponding aliases (e.g., “sudo”) provide the user interface. This layer processes command-line arguments (for both CMD and PowerShell), handling options such as “-n” (new window), “-w” (wait), and the “!!” repeat command.
- Command Parser & Dispatcher: Within the “src/gsudo/Commands” and “src/gsudo/Helpers” directories, the commands get parsed and dispatched. This component interprets the intended operation (e.g., elevation request, credentials cache activation, etc.) and directs the workflow.
- Elevation & Privilege Management Unit: Handles elevation logic (including UAC pop-up triggering and credential caching) and interacts with Windows’ native APIs. Look in “src/gsudo/ElevationRequest.cs”, “src/gsudo/Tokens”, and “src/gsudo/CredentialsCache” for these functionalities.
- Process Creation & Host Management: The project organizes elevated process handling through “ProcessHosts” (found in “src/gsudo/ProcessHosts”) and renders output via “ProcessRenderers”. This layer manages launching a new elevated shell or running commands in the current console.
- Native Integration & Pseudo Console: The “Native” and “PseudoConsole” folders contain wrappers for making native Windows API calls to handle tasks such as console management and token handling.
- Build, Packaging, and Deployment: The “build” directory contains PowerShell scripts (build, test, sign, release for Chocolatey, Nuget, and Scoop packaging) which indicate a CI/CD pipeline with GitHub workflows. These components build and distribute the tool.
- Testing & Documentation: The “src/gsudo.Tests”, “src/gsudo.Wrappers.Tests”, “docs”, and “sample-scripts” directories show how the project is validated, documented, and how usage examples are provided.

##### Relationships and Data Flow
- The input goes to the Command Parser, which inspects the arguments (and, if needed, picks up environment settings from configuration files).
- Depending on the command, the tool either:
– Launches an elevated process by invoking the Process Creation Unit
– Triggers the credential cache manager for repeated permissions elevation,
– Or dispatches a command to run in the current shell.
- The Elevation Unit interacts with Windows’ UAC mechanisms, optionally interfacing with native modules (in “Native” and “PseudoConsole”) to perform tasks like launching a new window or directly rendering output.
- Build and packaging components tie into external package managers and deployment scripts (Chocolatey, Nuget, Scoop) as seen in the “build” directory.

##### Architectural Patterns and Key Design Principles
- Layered Architecture: Separation between the CLI interface, command parsing, elevation logic, and process control layers.
- Modularity: Individual directories for Commands, Helpers, Native integrations, and Process Management suggest a modular design.
- Cross-Shell and Multipurpose Support: The design ensures support for multiple shells (CMD, PowerShell, WSL, etc.) with specific wrappers and helper functions.
- Extensibility: The inclusion of tests, sample scripts, and clear separation of concerns indicates an architecture that is designed for easy extension and integration with various Windows elevation scenarios.

##### Technologies and Frameworks
- .NET (targeting net9.0 as per the README) and usage of NativeAOT for fast startup and reduced memory.
- Use of PowerShell scripting for build automation and command testing.
- Integration with Windows native APIs through interop calls in “Native” modules.
- GitHub Actions for CI (workflows in the .github folder) and various packaging technologies (Chocolatey, Nuget, and Scoop).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Input Layer
    subgraph "User Input"
        CLI1["CLI: gsudo.exe"]:::cli
        CLI2["CLI: Alias for sudo.exe"]:::cli
    end

    %% Command Processing Layer
    subgraph "Command Processing"
        CP1["Commands"]:::parser
        CP2["Helpers (CommandLineParser)"]:::parser
    end

    %% Elevation & Process Management Layer
    subgraph "Elevation & Process Management"
        EP1["Elevation Request"]:::elevation
        EP2["Credential Cache"]:::elevation
        EP3["Tokens"]:::elevation
        PH1["Process Hosts"]:::process
        PR1["Process Renderers"]:::process
    end

    %% Native Integration Layer
    subgraph "Native Integrations"
        NI1["Native Integration"]:::native
        NI2["Pseudo Console"]:::native
    end

    %% Build and Deployment Layer
    subgraph "Build & Deployment"
        BD1["Build Scripts"]:::build
        BD2["Chocolatey Packaging"]:::build
        BD3["Nuget Packaging"]:::build
        BD4["Scoop Packaging"]:::build
        BD5["CI/CD Workflows"]:::build
    end

    %% Testing & Documentation Layer
    subgraph "Testing & Documentation"
        TD1["Tests: gsudo.Tests"]:::testing
        TD2["Tests: Wrappers.Tests"]:::testing
        TD3["Documentation"]:::testing
        TD4["Sample Scripts"]:::testing
    end

    %% Flow connections
    CLI1 -->|"parsesArgs"| CP1
    CLI1 -->|"parsesArgs"| CP2
    CLI2 -->|"parsesArgs"| CP1
    CLI2 -->|"parsesArgs"| CP2

    CP1 -->|"dispatches"| EP1
    CP2 -->|"dispatches"| EP1

    EP1 -->|"uses"| EP2
    EP1 -->|"uses"| EP3
    EP1 -->|"initiates"| PH1
    PH1 -->|"rendersOutput"| PR1

    EP1 -->|"calls"| NI1
    EP1 -->|"calls"| NI2

    BD1 -->|"triggers"| BD2
    BD1 -->|"triggers"| BD3
    BD1 -->|"triggers"| BD4
    BD1 -->|"integrates"| BD5

    BD5 -->|"automates"| TD1
    BD5 -->|"automates"| TD2

    %% Legend connection to Command Processing to indicate testing verifies command parsing
    TD1 -->|"verifies"| CP1

    %% Legend Subgraph
    subgraph "Legend"
        L1["CLI Layer (Blue)"]:::cli
        L2["Command Parser (Green)"]:::parser
        L3["Elevation Unit (Red)"]:::elevation
        L4["Process Management (Yellow)"]:::process
        L5["Native Integration (Purple)"]:::native
        L6["Build/Packaging (Light Periwinkle)"]:::build
        L7["Testing & Docs (Light Brown)"]:::testing
    end

    %% Click Events for CLI Layer
    click CLI1 "https://github.com/gerardog/gsudo/blob/master/src/gsudo/Program.cs"
    click CLI2 "https://github.com/gerardog/gsudo/blob/master/build/Chocolatey/gsudo/Bin/gsudo.exe.ignore"

    %% Click Events for Command Parser & Dispatcher
    click CP1 "https://github.com/gerardog/gsudo/tree/master/src/gsudo/Commands"
    click CP2 "https://github.com/gerardog/gsudo/blob/master/src/gsudo/Helpers/CommandLineParser.cs"

    %% Click Events for Elevation & Privilege Management Unit
    click EP1 "https://github.com/gerardog/gsudo/blob/master/src/gsudo/ElevationRequest.cs"
    click EP2 "https://github.com/gerardog/gsudo/tree/master/src/gsudo/CredentialsCache"
    click EP3 "https://github.com/gerardog/gsudo/tree/master/src/gsudo/Tokens"

    %% Click Events for Process Creation & Host Management
    click PH1 "https://github.com/gerardog/gsudo/tree/master/src/gsudo/ProcessHosts"
    click PR1 "https://github.com/gerardog/gsudo/tree/master/src/gsudo/ProcessRenderers"

    %% Click Events for Native Integration & Pseudo Console
    click NI1 "https://github.com/gerardog/gsudo/tree/master/src/gsudo/Native"
    click NI2 "https://github.com/gerardog/gsudo/tree/master/src/gsudo/PseudoConsole"

    %% Click Events for Build, Packaging, and Deployment
    click BD1 "https://github.com/gerardog/gsudo/tree/master/build"
    click BD2 "https://github.com/gerardog/gsudo/tree/master/build/Chocolatey"
    click BD3 "https://github.com/gerardog/gsudo/tree/master/build/Nuget"
    click BD4 "https://github.com/gerardog/gsudo/tree/master/build/Scoop"
    click BD5 "https://github.com/gerardog/gsudo/tree/master/.github/workflows"

    %% Click Events for Testing & Documentation
    click TD1 "https://github.com/gerardog/gsudo/blob/master/src/gsudo.Tests"
    click TD2 "https://github.com/gerardog/gsudo/blob/master/src/gsudo.Wrappers.Tests"
    click TD3 "https://github.com/gerardog/gsudo/tree/master/docs"
    click TD4 "https://github.com/gerardog/gsudo/tree/master/sample-scripts"

    %% Style Definitions
    classDef cli fill:#cce5ff,stroke:#333,stroke-width:2px;
    classDef parser fill:#e6ffe6,stroke:#333,stroke-width:2px;
    classDef elevation fill:#ffe6e6,stroke:#333,stroke-width:2px;
    classDef process fill:#fff0b3,stroke:#333,stroke-width:2px;
    classDef native fill:#f0e6ff,stroke:#333,stroke-width:2px;
    classDef build fill:#e6e6ff,stroke:#333,stroke-width:2px;
    classDef testing fill:#f5e6cc,stroke:#333,stroke-width:2px;
```
