|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +type commandEntry struct { |
| 13 | + Path string `json:"path"` |
| 14 | + Command string `json:"command"` |
| 15 | + Description string `json:"description,omitempty"` |
| 16 | +} |
| 17 | + |
| 18 | +func parseScriptHeaders(path string) (shortname, description string, err error) { |
| 19 | + f, err := os.Open(path) |
| 20 | + if err != nil { |
| 21 | + return "", "", fmt.Errorf("open %s: %w", path, err) |
| 22 | + } |
| 23 | + defer f.Close() |
| 24 | + |
| 25 | + scanner := bufio.NewScanner(f) |
| 26 | + for scanner.Scan() { |
| 27 | + line := scanner.Text() |
| 28 | + if !strings.HasPrefix(line, "#") { |
| 29 | + break |
| 30 | + } |
| 31 | + if v, ok := strings.CutPrefix(line, "# Shortname:"); ok { |
| 32 | + shortname = strings.TrimSpace(v) |
| 33 | + } else if v, ok := strings.CutPrefix(line, "# Description:"); ok { |
| 34 | + description = strings.TrimSpace(v) |
| 35 | + } |
| 36 | + } |
| 37 | + if err := scanner.Err(); err != nil { |
| 38 | + return "", "", fmt.Errorf("read %s: %w", path, err) |
| 39 | + } |
| 40 | + return shortname, description, nil |
| 41 | +} |
| 42 | + |
| 43 | +func scanDirectory(dir string) ([]commandEntry, error) { |
| 44 | + entries, err := os.ReadDir(dir) |
| 45 | + if err != nil { |
| 46 | + return nil, fmt.Errorf("read directory %s: %w", dir, err) |
| 47 | + } |
| 48 | + |
| 49 | + seen := map[string]string{} |
| 50 | + var cmds []commandEntry |
| 51 | + for _, e := range entries { |
| 52 | + if e.IsDir() || !strings.HasSuffix(e.Name(), ".sh") { |
| 53 | + continue |
| 54 | + } |
| 55 | + |
| 56 | + path := filepath.Join(dir, e.Name()) |
| 57 | + shortname, description, err := parseScriptHeaders(path) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + if shortname == "" || description == "" { |
| 62 | + continue |
| 63 | + } |
| 64 | + |
| 65 | + urlPath := "/" + shortname |
| 66 | + if prev, ok := seen[urlPath]; ok { |
| 67 | + return nil, fmt.Errorf("duplicate path %q: %s and %s", urlPath, prev, e.Name()) |
| 68 | + } |
| 69 | + seen[urlPath] = e.Name() |
| 70 | + |
| 71 | + cmds = append(cmds, commandEntry{ |
| 72 | + Path: urlPath, |
| 73 | + Command: "./" + filepath.Join("bin", e.Name()), |
| 74 | + Description: description, |
| 75 | + }) |
| 76 | + } |
| 77 | + |
| 78 | + if len(cmds) == 0 { |
| 79 | + return nil, fmt.Errorf("no valid detection scripts found in %s", dir) |
| 80 | + } |
| 81 | + |
| 82 | + return cmds, nil |
| 83 | +} |
| 84 | + |
| 85 | +func run() error { |
| 86 | + if len(os.Args) != 2 { |
| 87 | + return fmt.Errorf("usage: gendetections <scripts-dir>") |
| 88 | + } |
| 89 | + |
| 90 | + cmds, err := scanDirectory(os.Args[1]) |
| 91 | + if err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + enc := json.NewEncoder(os.Stdout) |
| 96 | + enc.SetIndent("", " ") |
| 97 | + if err := enc.Encode(cmds); err != nil { |
| 98 | + return fmt.Errorf("encode JSON: %w", err) |
| 99 | + } |
| 100 | + |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +func main() { |
| 105 | + if err := run(); err != nil { |
| 106 | + fmt.Fprintf(os.Stderr, "gendetections: %s\n", err) |
| 107 | + os.Exit(1) |
| 108 | + } |
| 109 | +} |
0 commit comments