### Configure gocover-ui with Command Line Flags (Bash) Source: https://context7.com/tschaefer/gocover-ui/llms.txt This example showcases various command-line flags available for `gocover-ui` to customize the HTML coverage report generation. It includes flags for specifying input/output paths, cleaning the output directory, controlling verbosity, and printing the version. ```bash # Full example with all flags gocover-ui \ -profile coverage.out \ -src . \ -out coverage \ -clean \ -quiet # Print version information gocover-ui -version ``` -------------------------------- ### Aggregate Coverage Statistics with coverage.Statistics (Go) Source: https://context7.com/tschaefer/gocover-ui/llms.txt This Go code snippet demonstrates how to use the `coverage.Statistics` function to aggregate coverage metrics from multiple files. It first parses coverage profiles, analyzes each file using `coverage.Analyze`, and then passes the resulting `FileMetrics` to `coverage.Statistics` to compute overall project-wide statistics, including total files, statements, covered statements, and coverage percentage. It requires the `golang.org/x/tools/cover` and `github.com/tschaefer/cover-ui/internal/coverage` packages. ```go package main import ( "fmt" "golang.org/x/tools/cover" "github.com/tschaefer/cover-ui/internal/coverage" ) func main() { profiles, _ := cover.ParseProfiles("coverage.out") module := "github.com/myorg/myproject" var files []*coverage.FileMetrics for _, profile := range profiles { metrics, err := coverage.Analyze(profile, module, ".") if err == nil { files = append(files, metrics) } } // Calculate aggregate statistics totalMetrics := coverage.Statistics(files) fmt.Printf("Project Coverage Summary:\n") fmt.Printf(" Total Files: %d\n", totalMetrics.TotalFiles) fmt.Printf(" Total Statements: %d\n", totalMetrics.TotalStmts) fmt.Printf(" Covered: %d\n", totalMetrics.CoveredStmts) fmt.Printf(" Coverage: %.2f%%\n", totalMetrics.CoveragePct) } ``` -------------------------------- ### Analyze Go Coverage Profile with coverage.Analyze (Go) Source: https://context7.com/tschaefer/gocover-ui/llms.txt This Go code snippet demonstrates how to use the `coverage.Analyze` function to process a Go coverage profile. It parses the profile, analyzes each file's coverage metrics (tracked lines, covered lines, partial lines, missed lines, coverage percentage, and statements), and prints the results. It requires the `golang.org/x/tools/cover` and `github.com/tschaefer/cover-ui/internal/coverage` packages. ```go package main import ( "fmt" "golang.org/x/tools/cover" "github.com/tschaefer/cover-ui/internal/coverage" ) func main() { // Parse Go coverage profile profiles, err := cover.ParseProfiles("coverage.out") if err != nil { panic(err) } module := "github.com/myorg/myproject" srcRoot := "." for _, profile := range profiles { // Analyze each file's coverage metrics, err := coverage.Analyze(profile, module, srcRoot) if err != nil { fmt.Printf("Warning: skipping %s: %v\n", profile.FileName, err) continue } fmt.Printf("File: %s\n", metrics.LocalPath) fmt.Printf(" Tracked Lines: %d\n", metrics.TrackedLines) fmt.Printf(" Covered Lines: %d\n", metrics.CoveredLines) fmt.Printf(" Partial Lines: %d\n", metrics.PartialLines) fmt.Printf(" Missed Lines: %d\n", metrics.MissedLines) fmt.Printf(" Coverage: %.2f%%\n", metrics.CoveragePct) fmt.Printf(" Statements: %d/%d covered\n", metrics.CoveredStmts, metrics.TotalStmts) } } ``` -------------------------------- ### Build Hierarchical File Tree with Coverage (Go) Source: https://context7.com/tschaefer/gocover-ui/llms.txt Constructs a nested tree structure from a flat list of file metrics. It organizes files by directory and aggregates coverage statistics for each directory node, providing a hierarchical view of code coverage. ```Go package main import ( "encoding/json" "fmt" "github.com/tschaefer/cover-ui/internal/coverage" "github.com/tschaefer/cover-ui/internal/tree" ) func main() { // Example file metrics (normally from coverage.Analyze) files := []*coverage.FileMetrics{ { LocalPath: "internal/handler/auth.go", TrackedLines: 50, CoveredLines: 45, PartialLines: 2, MissedLines: 3, TotalStmts: 60, CoveredStmts: 55, CoveragePct: 91.67, }, { LocalPath: "internal/handler/user.go", TrackedLines: 30, CoveredLines: 28, PartialLines: 1, MissedLines: 1, TotalStmts: 35, CoveredStmts: 33, CoveragePct: 94.29, }, { LocalPath: "cmd/main.go", TrackedLines: 20, CoveredLines: 18, PartialLines: 0, MissedLines: 2, TotalStmts: 25, CoveredStmts: 22, CoveragePct: 88.00, }, } // Build hierarchical tree with aggregated coverage root := tree.Build(files) // Tree structure: // / // ├── cmd/ // │ └── main.go (88.00%) // └── internal/ // └── handler/ // ├── auth.go (91.67%) // └── user.go (94.29%) jsonData, _ := json.MarshalIndent(root, "", " ") fmt.Println(string(jsonData)) } ``` -------------------------------- ### Generate HTML Coverage Report with gocover-ui Source: https://github.com/tschaefer/gocover-ui/blob/main/README.md This snippet demonstrates the command-line usage of gocover-ui to generate an HTML coverage report. It first runs Go tests to create a coverage profile, then uses gocover-ui to process this profile and generate an interactive report in HTML format. ```bash go test -coverprofile=coverage.out ./... gocover-ui -profile coverage.out -src . -out coverage open coverage/index.html ``` -------------------------------- ### File Tree API Source: https://context7.com/tschaefer/gocover-ui/llms.txt Creates a hierarchical tree structure from a flat list of file metrics, organizing files by directory and calculating aggregate coverage statistics for each directory node. ```APIDOC ## File Tree API ### tree.Build #### Description Creates a hierarchical tree structure from a flat list of file metrics, organizing files by directory and calculating aggregate coverage statistics for each directory node. #### Method `POST` (or internal function call) #### Endpoint N/A (This is an internal module function, not a direct HTTP endpoint) #### Parameters ##### Request Body - **files** ([]*coverage.FileMetrics) - Required - A slice of FileMetrics objects representing individual files. - **LocalPath** (string) - The local path of the file. - **TrackedLines** (int) - The number of lines tracked for coverage. - **CoveredLines** (int) - The number of lines covered. - **PartialLines** (int) - The number of lines partially covered. - **MissedLines** (int) - The number of lines missed. - **TotalStmts** (int) - The total number of statements. - **CoveredStmts** (int) - The number of statements covered. - **CoveragePct** (float64) - The overall coverage percentage. #### Request Example ```go package main import ( "encoding/json" "fmt" "github.com/tschaefer/cover-ui/internal/coverage" "github.com/tschaefer/cover-ui/internal/tree" ) func main() { files := []*coverage.FileMetrics{ { LocalPath: "internal/handler/auth.go", TrackedLines: 50, CoveredLines: 45, PartialLines: 2, MissedLines: 3, TotalStmts: 60, CoveredStmts: 55, CoveragePct: 91.67, }, { LocalPath: "internal/handler/user.go", TrackedLines: 30, CoveredLines: 28, PartialLines: 1, MissedLines: 1, TotalStmts: 35, CoveredStmts: 33, CoveragePct: 94.29, }, { LocalPath: "cmd/main.go", TrackedLines: 20, CoveredLines: 18, PartialLines: 0, MissedLines: 2, TotalStmts: 25, CoveredStmts: 22, CoveragePct: 88.00, }, } root := tree.Build(files) jsonData, _ := json.MarshalIndent(root, "", " ") fmt.Println(string(jsonData)) } ``` #### Response ##### Success Response (200) - **root** (TreeNode) - The root node of the hierarchical file tree. - **Name** (string) - The name of the directory or file. - **Coverage** (float64) - The aggregated coverage percentage for this node. - **Children** ([]TreeNode) - A slice of child nodes (subdirectories or files). ##### Response Example ```json { "Name": "/", "Coverage": 91.30, "Children": [ { "Name": "cmd", "Coverage": 88.00, "Children": [ { "Name": "main.go", "Coverage": 88.00, "Children": [] } ] }, { "Name": "internal", "Coverage": 92.98, "Children": [ { "Name": "handler", "Coverage": 92.98, "Children": [ { "Name": "auth.go", "Coverage": 91.67, "Children": [] }, { "Name": "user.go", "Coverage": 94.29, "Children": [] } ] } ] } ] } ``` ``` -------------------------------- ### Generate Per-File HTML Coverage Report (Go) Source: https://context7.com/tschaefer/gocover-ui/llms.txt Generates detailed HTML pages for individual Go source files, including line numbers, syntax highlighting, and color-coded coverage status. It requires coverage metrics and a directory to save the output assets. ```go package main import ( "github.com/tschaefer/cover-ui/internal/coverage" "github.com/tschaefer/cover-ui/internal/generator/file" ) func main() { metrics := &coverage.FileMetrics{ FileName: "github.com/myorg/myproject/internal/handler.go", LocalPath: "internal/handler.go", TrackedLines: 50, CoveredLines: 45, PartialLines: 3, MissedLines: 2, TotalStmts: 60, CoveredStmts: 55, CoveragePct: 91.67, PerLineStatus: []int{-1, 2, 2, 2, 1, 0, 2, 2, 2, 2, 1, 2}, // per-line status array } filesDir := "./coverage/tree" // Write CSS and JS assets for file detail pages if err := file.Assets(filesDir); err != nil { panic(err) } // Generate file detail page with syntax highlighting if err := file.Generate(metrics, filesDir); err != nil { panic(err) } // Creates: ./coverage/tree/internal/handler.html } ``` -------------------------------- ### Generate Index HTML Report with Coverage Data (Go) Source: https://context7.com/tschaefer/gocover-ui/llms.txt Generates the main index.html file for the coverage report. It includes a file list, coverage statistics table, and an interactive donut chart. It also handles the generation of necessary CSS and JavaScript assets. ```Go package main import ( "github.com/tschaefer/cover-ui/internal/coverage" "github.com/tschaefer/cover-ui/internal/generator/index" ) func main() { files := []*coverage.FileMetrics{ { FileName: "github.com/myorg/myproject/main.go", LocalPath: "main.go", TrackedLines: 100, CoveredLines: 85, PartialLines: 5, MissedLines: 10, TotalStmts: 120, CoveredStmts: 100, CoveragePct: 83.33, PerLineStatus: []int{-1, 2, 2, 1, 0, 2, 2, 2}, // -1=not tracked, 0=missed, 1=partial, 2=covered }, } outDir := "./coverage" module := "github.com/myorg/myproject" // Write CSS and JS assets first if err := index.Assets(outDir); err != nil { panic(err) } // Generate index.html with embedded file metadata and tree structure if err := index.Generate(files, outDir, module); err != nil { panic(err) } // Creates: ./coverage/index.html, ./coverage/style.css, ./coverage/script.js } ``` -------------------------------- ### HTML Generator APIs Source: https://context7.com/tschaefer/gocover-ui/llms.txt APIs for generating HTML-based code coverage reports, including the main index page and associated assets. ```APIDOC ## HTML Generator APIs ### index.Generate #### Description Generates the main index.html page with a file list, coverage statistics table, and interactive donut chart visualization showing coverage distribution across all files. #### Method `POST` (or internal function call) #### Endpoint N/A (This is an internal module function, not a direct HTTP endpoint) #### Parameters ##### Request Body - **files** ([]*coverage.FileMetrics) - Required - A slice of FileMetrics objects representing individual files. - **FileName** (string) - The fully qualified name of the file (e.g., "github.com/myorg/myproject/main.go"). - **LocalPath** (string) - The local path of the file. - **TrackedLines** (int) - The number of lines tracked for coverage. - **CoveredLines** (int) - The number of lines covered. - **PartialLines** (int) - The number of lines partially covered. - **MissedLines** (int) - The number of lines missed. - **TotalStmts** (int) - The total number of statements. - **CoveredStmts** (int) - The number of statements covered. - **CoveragePct** (float64) - The overall coverage percentage. - **PerLineStatus** ([]int) - An array representing the coverage status for each line (-1=not tracked, 0=missed, 1=partial, 2=covered). - **outDir** (string) - Required - The output directory where the HTML files and assets will be saved. - **module** (string) - Required - The Go module name. #### Request Example ```go package main import ( "github.com/tschaefer/cover-ui/internal/coverage" "github.com/tschaefer/cover-ui/internal/generator/index" ) func main() { files := []*coverage.FileMetrics{ { FileName: "github.com/myorg/myproject/main.go", LocalPath: "main.go", TrackedLines: 100, CoveredLines: 85, PartialLines: 5, MissedLines: 10, TotalStmts: 120, CoveredStmts: 100, CoveragePct: 83.33, PerLineStatus: []int{-1, 2, 2, 1, 0, 2, 2, 2}, }, } outDir := "./coverage" module := "github.com/myorg/myproject" if err := index.Assets(outDir); err != nil { panic(err) } if err := index.Generate(files, outDir, module); err != nil { panic(err) } } ``` #### Response ##### Success Response (200) - **Files Generated**: `index.html`, `style.css`, `script.js` in the specified `outDir`. ##### Response Example (No direct response body, but files are created in the output directory.) ### index.Assets #### Description Writes the necessary CSS and JavaScript assets to the specified output directory for the HTML report. #### Method `POST` (or internal function call) #### Endpoint N/A (This is an internal module function, not a direct HTTP endpoint) #### Parameters ##### Request Body - **outDir** (string) - Required - The output directory where the assets will be saved. ``` -------------------------------- ### Print Version Banner with Git Commit (Go) Source: https://context7.com/tschaefer/gocover-ui/llms.txt Displays the application's version banner, including release version and git commit hash. It respects the NO_COLOR environment variable to control terminal output formatting. ```go package main import ( "fmt" "github.com/tschaefer/cover-ui/internal/version" ) func main() { // Get version information release := version.Release() // Returns "dev" if not set at build time commit := version.Commit() // Returns git commit hash banner := version.Banner() // Returns ASCII art banner fmt.Printf("Version: %s\n", release) fmt.Printf("Commit: %s\n", commit) // Print full version info (colored if NO_COLOR not set) version.Print() } ``` -------------------------------- ### Module Reader API Source: https://context7.com/tschaefer/gocover-ui/llms.txt Parses the go.mod file from a specified path and extracts the module name, which is required to map coverage profile file names to local source paths. ```APIDOC ## Module Reader API ### module.Read #### Description Parses the go.mod file from a specified path and extracts the module name, which is required to map coverage profile file names to local source paths. #### Method `GET` (or internal function call) #### Endpoint N/A (This is an internal module function, not a direct HTTP endpoint) #### Parameters ##### Query Parameters - **path** (string) - Required - The path to the directory containing the go.mod file. #### Request Example ```go package main import ( "fmt" "github.com/tschaefer/cover-ui/internal/module" ) func main() { moduleName, err := module.Read(".") // Reads go.mod from the current directory if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Module: %s\n", moduleName) } ``` #### Response ##### Success Response - **moduleName** (string) - The name of the Go module. ##### Response Example ``` Module: github.com/myorg/myproject ``` ``` -------------------------------- ### Embedded Metadata and File Tree JSON in Go Templates Source: https://github.com/tschaefer/gocover-ui/blob/main/internal/generator/index/assets/index.html This snippet demonstrates how to embed JSON data for metadata and file trees within Go's text/template system. It utilizes Go's template syntax to define sections for title, subheader, content, and scripts, with the scripts section containing the actual JSON data. ```go {{define "title"}}{{.Module}}{{end}} {{define "subheader"}}{{.Module}}{{end}} {{define "content"}} {{end}} {{define "scripts"}} // Embedded metadata const files = {{.MetaJSON}}; const fileTree = {{.TreeJSON}}; {{end}} ``` -------------------------------- ### Read Module Name from go.mod (Go) Source: https://context7.com/tschaefer/gocover-ui/llms.txt Parses a go.mod file from a given directory to extract the module name. This is crucial for mapping coverage profile file names to their corresponding local source file paths. ```Go package main import ( "fmt" "github.com/tschaefer/cover-ui/internal/module" ) func main() { // Read module name from go.mod in current directory moduleName, err := module.Read(".") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Module: %s\n", moduleName) // Output: Module: github.com/myorg/myproject } ``` -------------------------------- ### Represent Line Coverage Status (Go) Source: https://context7.com/tschaefer/gocover-ui/llms.txt Defines the LineStatus enum for tracking individual line coverage (Missed, Partial, Covered) and provides human-readable string conversions. It's used to represent the state of each line in a source file. ```Go package main import ( "fmt" "github.com/tschaefer/cover-ui/internal/coverage" ) func main() { // LineStatus enum values statuses := []coverage.LineStatus{ coverage.Missed, // 0 - Line not executed coverage.Partial, // 1 - Line partially covered coverage.Covered, // 2 - Line fully covered } for _, status := range statuses { str, _ := status.String() fmt.Printf("Status %d: %s\n", status, str) } // Output: // Status 0: missed // Status 1: partial // Status 2: covered } ``` -------------------------------- ### Coverage Line Status Source: https://context7.com/tschaefer/gocover-ui/llms.txt Represents the coverage status of individual source lines with three possible states: Missed (0), Partial (1), or Covered (2). Provides human-readable string conversion. ```APIDOC ## coverage.LineStatus ### Description Represents the coverage status of individual source lines with three possible states: Missed (0), Partial (1), or Covered (2), providing human-readable string conversion. ### Enum Values - **Missed** (0): Line not executed - **Partial** (1): Line partially covered - **Covered** (2): Line fully covered ### Usage Example ```go package main import ( "fmt" "github.com/tschaefer/cover-ui/internal/coverage" ) func main() { statuses := []coverage.LineStatus{ coverage.Missed, coverage.Partial, coverage.Covered, } for _, status := range statuses { str, _ := status.String() fmt.Printf("Status %d: %s\n", status, str) } } ``` ### Output Example ``` Status 0: missed Status 1: partial Status 2: covered ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.