-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.go
More file actions
77 lines (71 loc) · 2 KB
/
Copy pathcheck.go
File metadata and controls
77 lines (71 loc) · 2 KB
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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/Taka-S-dev/baton/internal/config"
)
// runCheck implements `baton check [project|path]`: it loads projects
// without starting the TUI and prints their diagnostics. Returns the
// process exit code — 0 when everything is clean, 1 otherwise — so
// scripts, CI, and AI agents can use it as a validation loop.
func runCheck(args []string) int {
dirs, err := checkTargets(args)
if err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
return 1
}
exit := 0
for _, dir := range dirs {
name := filepath.Base(dir)
p, err := config.LoadProject(dir)
if err != nil {
fmt.Printf("%s: ERROR %v\n", name, err)
exit = 1
continue
}
if len(p.Warnings) == 0 {
fmt.Printf("%s: OK (%d commands, %d workflows, %d lists)\n",
name, len(p.Config.AllCommands()), len(p.Workflows), len(p.Lists))
continue
}
exit = 1
for _, w := range p.Warnings {
fmt.Printf("%s: WARN %s\n", name, w)
}
}
return exit
}
// checkTargets resolves the check argument: no argument means every
// project in the projects directory; an existing directory path is
// checked as-is; anything else is a project name under the projects dir.
func checkTargets(args []string) ([]string, error) {
if len(args) > 0 {
arg := args[0]
if info, err := os.Stat(arg); err == nil && info.IsDir() {
return []string{arg}, nil
}
projectsDir, err := config.FindProjectsDir()
if err != nil {
return nil, err
}
dir := filepath.Join(projectsDir, arg)
if info, err := os.Stat(dir); err != nil || !info.IsDir() {
return nil, fmt.Errorf("project %q not found (looked for directory %s)", arg, dir)
}
return []string{dir}, nil
}
projectsDir, err := config.FindProjectsDir()
if err != nil {
return nil, err
}
names := config.ListProjects(projectsDir)
if len(names) == 0 {
return nil, fmt.Errorf("no projects found in %s", projectsDir)
}
dirs := make([]string, len(names))
for i, n := range names {
dirs[i] = filepath.Join(projectsDir, n)
}
return dirs, nil
}