fleet/cmd/fleetctl/delete.go

95 lines
2.1 KiB
Go
Raw Normal View History

2018-05-08 02:07:00 +00:00
package main
import (
"errors"
2018-05-08 02:07:00 +00:00
"fmt"
"os"
2018-05-08 02:07:00 +00:00
"github.com/fleetdm/fleet/v4/pkg/spec"
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
2021-06-26 04:46:51 +00:00
"github.com/fleetdm/fleet/v4/server/service"
"github.com/urfave/cli/v2"
2018-05-08 02:07:00 +00:00
)
func deleteCommand() *cli.Command {
var flFilename string
return &cli.Command{
2018-05-08 02:07:00 +00:00
Name: "delete",
Usage: "Specify files to declaratively batch delete osquery configurations",
UsageText: `fleetctl delete [options]`,
Flags: []cli.Flag{
&cli.StringFlag{
2018-05-08 02:07:00 +00:00
Name: "f",
EnvVars: []string{"FILENAME"},
2018-05-08 02:07:00 +00:00
Value: "",
Destination: &flFilename,
Usage: "A file to apply",
},
configFlag(),
contextFlag(),
debugFlag(),
2018-05-08 02:07:00 +00:00
},
Action: func(c *cli.Context) error {
if flFilename == "" {
return errors.New("-f must be specified")
}
b, err := os.ReadFile(flFilename)
2018-05-08 02:07:00 +00:00
if err != nil {
return err
}
fleet, err := clientFromCLI(c)
if err != nil {
return err
}
specs, err := spec.GroupFromBytes(b)
2018-05-08 02:07:00 +00:00
if err != nil {
return err
}
for _, query := range specs.Queries {
fmt.Printf("[+] deleting query %q\n", query.Name)
if err := fleet.DeleteQuery(query.Name); err != nil {
root := ctxerr.Cause(err)
switch root.(type) {
2018-05-08 02:07:00 +00:00
case service.NotFoundErr:
fmt.Printf("[!] query %q doesn't exist\n", query.Name)
continue
}
return err
}
}
for _, pack := range specs.Packs {
fmt.Printf("[+] deleting pack %q\n", pack.Name)
if err := fleet.DeletePack(pack.Name); err != nil {
root := ctxerr.Cause(err)
switch root.(type) {
2018-05-08 02:07:00 +00:00
case service.NotFoundErr:
fmt.Printf("[!] pack %q doesn't exist\n", pack.Name)
continue
}
return err
}
}
for _, label := range specs.Labels {
fmt.Printf("[+] deleting label %q\n", label.Name)
if err := fleet.DeleteLabel(label.Name); err != nil {
root := ctxerr.Cause(err)
switch root.(type) {
2018-05-08 02:07:00 +00:00
case service.NotFoundErr:
fmt.Printf("[!] label %q doesn't exist\n", label.Name)
continue
}
return err
}
}
return nil
},
}
}