summaryrefslogtreecommitdiff
path: root/cmd/krino/check.go
blob: 1820bbeb071f720e2444f07781d643d45125aaf5 (plain) (blame)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package main

import (
	"fmt"
	"io"

	"krino/internal/engine"
	"krino/internal/xdg"
)

func init() { commands["check"] = cmdCheck }

// cmdCheck validates the configuration and lists each directory's rules,
// its current state, and the extractors available for content tests.
func cmdCheck(g *globals, args []string, stdout, stderr io.Writer) int {
	fs := flagSet("check", g)
	if code, ok := parse(fs, args, stdout, stderr); !ok {
		return code
	}
	e, errs := engine.Load(mainFile(g), fs.Args()...)
	if len(errs) > 0 {
		printDiags(stderr, errs)
		return 2
	}
	r := e.Check()

	fmt.Fprintf(stdout, "config: %s\n", xdg.Abbrev(r.MainFile))
	fmt.Fprintf(stdout, "log: %s\n", xdg.Abbrev(r.LogFile))
	if len(r.Dirs) == 0 {
		fmt.Fprintln(stdout, "no directories included; add one with: krino new NAME PATH")
	}
	for _, dr := range r.Dirs {
		fmt.Fprintf(stdout, "\n%s  %s\n", dr.Dir.Name, xdg.Abbrev(dr.Dir.Root))
		if dr.Missing {
			fmt.Fprintf(stdout, "  warning: %s is not a directory right now; it will be skipped\n", xdg.Abbrev(dr.Dir.Root))
		}
		if len(dr.Dir.Rules) == 0 {
			fmt.Fprintln(stdout, "  no rules yet")
		}
		for i, rule := range dr.Dir.Rules {
			fmt.Fprintf(stdout, "  %2d  %-16s %s\n", i+1, rule.Name, describeActions(rule.Conf))
		}
	}

	fmt.Fprintln(stdout, "\nextractors:")
	for _, tool := range r.Tools {
		path := "not installed"
		if tool.Path != "" {
			path = xdg.Abbrev(tool.Path)
		}
		fmt.Fprintf(stdout, "  %-9s  %s\n", tool.Name, path)
	}
	return 0
}