-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
cmd_prune.go
125 lines (108 loc) · 2.91 KB
/
cmd_prune.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// Prune history by removing old reports.
//
package main
import (
"context"
"flag"
"fmt"
"github.com/google/subcommands"
)
//
// The options set by our command-line flags.
//
type pruneCmd struct {
dbFile string
days int
environment string
unchanged bool
orphaned bool
prefix string
dangling bool
noop bool
verbose bool
}
//
// Run a prune
//
func runPrune(x pruneCmd) error {
//
// Remove yaml files that are not referenced in the database
//
if x.dangling {
if x.verbose {
fmt.Printf("Pruning yaml report files that are not referenced in the database from beneath %s\n", ReportPrefix)
}
return (pruneDangling(x.prefix, x.noop, x.verbose))
}
//
// Removing orphaned nodes?
//
if x.orphaned {
if x.verbose {
fmt.Printf("Pruning 'orphaned' reports from beneath %s\n", ReportPrefix)
}
return (pruneOrphaned(x.environment, x.prefix, x.verbose))
}
//
// Removing unchanged reports?
//
if x.unchanged {
if x.verbose {
fmt.Printf("Pruning 'unchanged' reports from beneath %s\n", ReportPrefix)
}
return (pruneUnchanged(x.environment, x.prefix, x.verbose))
}
//
// Otherwise just removing reports older than the given
// number of days.
//
if x.verbose {
fmt.Printf("Pruning reports older than %d days from beneath %s\n", x.days, ReportPrefix)
}
err := pruneReports(x.environment, x.prefix, x.days, x.verbose)
return err
}
//
// Glue
//
func (*pruneCmd) Name() string { return "prune" }
func (*pruneCmd) Synopsis() string { return "Prune/delete old reports." }
func (*pruneCmd) Usage() string {
return `prune [options]:
Remove old report-files from disk, and our database.
`
}
//
// Flag setup
//
func (p *pruneCmd) SetFlags(f *flag.FlagSet) {
f.BoolVar(&p.verbose, "verbose", false, "Be verbose in reporting output")
f.IntVar(&p.days, "days", 7, "Remove reports older than this many days.")
f.BoolVar(&p.unchanged, "unchanged", false, "Remove reports from hosts which had no changes.")
f.BoolVar(&p.orphaned, "orphaned", false, "Remove reports from hosts which are orphaned.")
f.StringVar(&p.dbFile, "db-file", "ps.db", "The SQLite database to use.")
f.StringVar(&p.prefix, "prefix", "./reports/", "The prefix to the local YAML hierarchy.")
f.BoolVar(&p.dangling, "dangling", false, "Remove yaml reports that are not referenced in the database.")
f.BoolVar(&p.noop, "noop", false, "Do not remove dangling yaml files, just pretend.")
f.StringVar(&p.environment, "environment", "", "If specified only prune this environment.")
}
//
// Entry-point.
//
func (p *pruneCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus {
//
// Setup the database, by opening a handle, and creating it if
// missing.
//
SetupDB(p.dbFile)
//
// Invoke the prune
//
err := runPrune(*p)
if err == nil {
return subcommands.ExitSuccess
}
fmt.Printf("Error pruning: %s\n", err.Error())
return subcommands.ExitFailure
}