mirror of
https://github.com/crowdsecurity/crowdsec.git
synced 2025-05-11 04:15:54 +02:00
custom duration type for "cscli machines prune"
This commit is contained in:
parent
11a13d64b7
commit
f5e9424b4a
2 changed files with 14 additions and 4 deletions
|
@ -9,11 +9,15 @@ import (
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/cstime"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/args"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/args"
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/ask"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/ask"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultPruneDuration = 10 * time.Minute
|
||||||
|
|
||||||
func (cli *cliMachines) prune(ctx context.Context, duration time.Duration, notValidOnly bool, force bool) error {
|
func (cli *cliMachines) prune(ctx context.Context, duration time.Duration, notValidOnly bool, force bool) error {
|
||||||
if duration < 2*time.Minute && !notValidOnly {
|
if duration < 2*time.Minute && !notValidOnly {
|
||||||
if yes, err := ask.YesNo(
|
if yes, err := ask.YesNo(
|
||||||
|
@ -67,12 +71,11 @@ func (cli *cliMachines) prune(ctx context.Context, duration time.Duration, notVa
|
||||||
|
|
||||||
func (cli *cliMachines) newPruneCmd() *cobra.Command {
|
func (cli *cliMachines) newPruneCmd() *cobra.Command {
|
||||||
var (
|
var (
|
||||||
duration time.Duration
|
|
||||||
notValidOnly bool
|
notValidOnly bool
|
||||||
force bool
|
force bool
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultDuration = 10 * time.Minute
|
duration := cstime.Duration(defaultPruneDuration)
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: "prune",
|
Use: "prune",
|
||||||
|
@ -84,12 +87,12 @@ cscli machines prune --not-validated-only --force`,
|
||||||
Args: args.NoArgs,
|
Args: args.NoArgs,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
||||||
return cli.prune(cmd.Context(), duration, notValidOnly, force)
|
return cli.prune(cmd.Context(), time.Duration(duration), notValidOnly, force)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.DurationVarP(&duration, "duration", "d", defaultDuration, "duration of time since validated machine last heartbeat")
|
flags.VarP(&duration, "duration", "d", "duration of time since validated machine last heartbeat")
|
||||||
flags.BoolVar(¬ValidOnly, "not-validated-only", false, "only prune machines that are not validated")
|
flags.BoolVar(¬ValidOnly, "not-validated-only", false, "only prune machines that are not validated")
|
||||||
flags.BoolVar(&force, "force", false, "force prune without asking for confirmation")
|
flags.BoolVar(&force, "force", false, "force prune without asking for confirmation")
|
||||||
|
|
||||||
|
|
|
@ -124,12 +124,19 @@ teardown() {
|
||||||
@test "cscli machines prune" {
|
@test "cscli machines prune" {
|
||||||
rune -0 cscli metrics
|
rune -0 cscli metrics
|
||||||
|
|
||||||
|
rune -1 cscli machines prune --duration foobar
|
||||||
|
assert_stderr 'Error: invalid argument "foobar" for "-d, --duration" flag: time: invalid duration "foobar"'
|
||||||
|
|
||||||
# if the fixture has been created some time ago,
|
# if the fixture has been created some time ago,
|
||||||
# the machines may be old enough to trigger a user prompt.
|
# the machines may be old enough to trigger a user prompt.
|
||||||
# make sure the prune duration is high enough.
|
# make sure the prune duration is high enough.
|
||||||
rune -0 cscli machines prune --duration 1000000h
|
rune -0 cscli machines prune --duration 1000000h
|
||||||
assert_output 'No machines to prune.'
|
assert_output 'No machines to prune.'
|
||||||
|
|
||||||
|
# duration takes days as well
|
||||||
|
rune -0 cscli machines prune --duration 1000d30m
|
||||||
|
assert_output 'No machines to prune.'
|
||||||
|
|
||||||
rune -0 cscli machines list -o json
|
rune -0 cscli machines list -o json
|
||||||
rune -0 jq -r '.[-1].machineId' <(output)
|
rune -0 jq -r '.[-1].machineId' <(output)
|
||||||
rune -0 cscli machines delete "$output"
|
rune -0 cscli machines delete "$output"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue