aboutsummaryrefslogtreecommitdiff
path: root/cmd/krino/check.go
blob: fcd507d0731252cfe335208b0d1ab2cecc9e3745 (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
// SPDX-License-Identifier: GPL-3.0-or-later

package main

import (
	"fmt"
	"io"
	"os"

	"krino/internal/config"
	"krino/internal/xdg"
)

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

// cmdCheck validates the configuration and lists each directory's rules.
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
	}
	cfg, errs := config.Load(mainFile(g), fs.Args()...)
	if len(errs) > 0 {
		printDiags(stderr, errs)
		return 2
	}
	fmt.Fprintf(stdout, "config: %s\n", xdg.Abbrev(cfg.Main.File))
	if len(cfg.Dirs) == 0 {
		fmt.Fprintln(stdout, "no directories included; add one with: krino new NAME PATH")
	}
	for _, d := range cfg.Dirs {
		fmt.Fprintf(stdout, "\n%s  %s\n", d.Name, xdg.Abbrev(d.Path))
		if fi, err := os.Stat(d.Path); err != nil || !fi.IsDir() {
			fmt.Fprintf(stdout, "  warning: %s is not a directory right now; it will be skipped\n", xdg.Abbrev(d.Path))
		}
		if len(d.Rules) == 0 {
			fmt.Fprintln(stdout, "  no rules yet")
		}
		for i, r := range d.Rules {
			fmt.Fprintf(stdout, "  %2d  %-16s %s\n", i+1, r.Name, describeActions(r))
		}
	}
	return 0
}