gitstack

grindlemire/go-tui code browser

1.9 KB Go 99 lines 2026-07-10 · 0592ab2 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/grindlemire/go-tui/internal/tuigen"
)

// runCheck implements the check subcommand.
// It parses and analyzes .gsx files without generating code.
// Useful for syntax checking and IDE integration.
func runCheck(args []string) error {
	verbose := false
	var paths []string

	// Parse arguments
	for _, arg := range args {
		if arg == "-v" || arg == "--verbose" {
			verbose = true
		} else {
			paths = append(paths, arg)
		}
	}

	// Default to current directory if no paths specified
	if len(paths) == 0 {
		paths = []string{"."}
	}

	// Collect all .gsx files
	files, err := collectGsxFiles(paths)
	if err != nil {
		return err
	}

	if len(files) == 0 {
		return fmt.Errorf("no .gsx files found")
	}

	if verbose {
		fmt.Printf("Checking %d .gsx file(s)\n", len(files))
	}

	// Check each file
	var errorCount int
	for _, inputPath := range files {
		if verbose {
			fmt.Printf("Checking %s\n", inputPath)
		}

		if err := checkFile(inputPath); err != nil {
			fmt.Fprintf(os.Stderr, "%v\n", err)
			errorCount++
			continue
		}
	}

	if errorCount > 0 {
		return fmt.Errorf("%d file(s) had errors", errorCount)
	}

	if verbose {
		fmt.Printf("All %d file(s) passed checks\n", len(files))
	}

	return nil
}

// checkFile parses and analyzes a single .gsx file.
func checkFile(inputPath string) error {
	// Read source file
	source, err := os.ReadFile(inputPath)
	if err != nil {
		return fmt.Errorf("reading file: %w", err)
	}

	// Get just the filename for error messages
	filename := filepath.Base(inputPath)

	// Parse source
	lexer := tuigen.NewLexer(filename, string(source))
	parser := tuigen.NewParser(lexer)

	file, parseErr := parser.ParseFile()
	if parseErr != nil {
		return parseErr
	}

	// Analyze (validates elements and attributes)
	analyzer := tuigen.NewAnalyzer()
	analyzer.SetPackageContext(loadPackageContext(inputPath))
	if err := analyzer.Analyze(file); err != nil {
		return err
	}

	return nil
}