aboutsummaryrefslogtreecommitdiff
path: root/cmd/krino/common.go
blob: ea1e83ded23c4b7cdabdeb86570934d2f1441441 (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"
	"strings"

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

// mainFile is -c FILE, or the default krino.conf.
func mainFile(g *globals) string {
	if g.conf != "" {
		return xdg.Expand(g.conf)
	}
	return config.DefaultFile()
}

// usageError reports a command-line mistake and returns exit status 2.
func usageError(stderr io.Writer, msg string) int {
	fmt.Fprintf(stderr, "krino: %s\nrun 'krino -h' for help\n", msg)
	return 2
}

// printDiags prints config problems with ~ for the home directory, then a count.
func printDiags(stderr io.Writer, errs []*config.Diag) {
	for _, e := range errs {
		d := *e
		d.File = xdg.Abbrev(d.File)
		fmt.Fprintln(stderr, &d)
	}
	if len(errs) == 1 {
		fmt.Fprintln(stderr, "krino: 1 problem found")
	} else {
		fmt.Fprintf(stderr, "krino: %d problems found\n", len(errs))
	}
}

// describeActions renders a rule's actions briefly, as in: move PDF, stop.
func describeActions(r *config.Rule) string {
	var parts []string
	for _, a := range r.Actions {
		if a.Arg == "" {
			parts = append(parts, a.Kind.String())
		} else {
			parts = append(parts, a.Kind.String()+" "+a.Arg)
		}
	}
	if r.Stop {
		parts = append(parts, "stop")
	}
	return strings.Join(parts, ", ")
}