Skip to content
Draft
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
87 changes: 87 additions & 0 deletions cmd/brief/html.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"context"
"flag"
"fmt"
"io"
"os"
"strings"

"github.com/git-pkgs/brief"
"github.com/git-pkgs/brief/detect"
"github.com/git-pkgs/brief/kb"
"github.com/git-pkgs/brief/remote"
"github.com/git-pkgs/brief/report"
)

func cmdHTML(args []string) {
fs := flag.NewFlagSet("brief html", flag.ExitOnError)
out := fs.String("o", "", "Output file (default stdout)")
keep := fs.Bool("keep", false, "Keep downloaded remote source")
depth := fs.Int("depth", -1, "Git clone depth (0 = full clone, default shallow)")
dir := fs.String("dir", "", "Directory to clone remote source into")
scanDepth := fs.Int("scan-depth", 0, "Max directory depth for language detection (0 = unlimited)")
skip := fs.String("skip", "", "Additional directories to skip, comma-separated")
tracked := fs.Bool("tracked", false, "Only consider files tracked by git")
_ = fs.Parse(args)

path := "."
if fs.NArg() > 0 {
path = fs.Arg(0)
}

src, err := remote.Resolve(context.Background(), path, remote.Options{
Keep: *keep,
Depth: *depth,
Dir: *dir,
})
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}

code := runHTML(src.Dir, *out, *scanDepth, *skip, *tracked)
src.Cleanup()
os.Exit(code)
}

func runHTML(dir, out string, scanDepth int, skip string, tracked bool) int {
knowledgeBase, err := kb.Load(brief.KnowledgeFS)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error loading knowledge base: %v\n", err)
return 1
}

engine := detect.New(knowledgeBase, dir)
engine.ScanDepth = scanDepth
engine.TrackedOnly = tracked
if skip != "" {
engine.SkipDirs = strings.Split(skip, ",")
}
r, err := engine.Run()
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error: %v\n", err)
return 1
}

var w io.Writer = os.Stdout
if out != "" {
f, ferr := os.Create(out) //nolint:gosec
if ferr != nil {
_, _ = fmt.Fprintf(os.Stderr, "error creating %s: %v\n", out, ferr)
return 1
}
defer func() { _ = f.Close() }()
w = f
}

if err := report.HTML(w, r); err != nil {
_, _ = fmt.Fprintf(os.Stderr, "error rendering HTML: %v\n", err)
return 1
}
if out != "" {
_, _ = fmt.Fprintf(os.Stderr, "wrote %s\n", out)
}
return 0
}
3 changes: 3 additions & 0 deletions cmd/brief/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ func main() {
case "outline":
cmdOutline(os.Args[2:])
return
case "html":
cmdHTML(os.Args[2:])
return
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/git-pkgs/purl v0.1.12
github.com/git-pkgs/registries v0.6.1
github.com/git-pkgs/spdx v0.1.4
github.com/stretchr/testify v1.11.1
golang.org/x/term v0.43.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down Expand Up @@ -199,7 +200,6 @@ require (
github.com/ssgreg/nlreturn/v2 v2.2.1 // indirect
github.com/stbenjam/no-sprintf-host-port v0.3.1 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/stretchr/testify v1.11.1 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
github.com/tetafro/godot v1.5.4 // indirect
github.com/timakin/bodyclose v0.0.0-20241222091800-1db5c5ca4d67 // indirect
Expand Down
Loading
Loading